#Conner Kahn
#12/17/21
#I learend about global and local variable scopes

# Compute the area of a square
def square_area (side):
# area is a local variable in square_area
area = side ** 2
return area
# Global scope
square_area(2)
# area is not within scope anymore and cannot be
# accessed from this global scope
print(area)

NameError: name 'area' is not defined

# Compute the area of a square
def square_area (side):
# area is a local variable in square_area
area = side ** 2
return area
# Compute the volume of a cube
def cube_volume (side):
# cube volume = area of base X side
volume = area * side # area is not defined within this scope
return volume
# Global scope
s = 2
square_area(s)
# area was deleted when the local scope of square_area was destroyed
cube_volume(s)

NameError: name 'area' is not defined

# Compute the area of a square
def square_area (side):
# area is a local variable in square_area
# area does not conflict with the variable area in rectangle_area
area = side ** 2
print("square area =", area)
# Compute the area of a rectangle
def rectangle_area (length, width):
# area is a local variable in rectangle_area
# area does not conflict with the variable area in square_area
area = length * width
print("rectangle area =", area)
# Global scope
square_area(2)
rectangle_area(2, 3)

```
square area = 4
rectangle area = 6
```

# [ ] Fix the program below so it displays the area of a square with a side = 2
# Compute the area of a square
def square_area (side):
# area is a local variable in square_area
area = side ** 2
print(area)
return area
# Global scope
square_area(2)
# area is not within scope anymore and cannot be
# accessed from this global scope

```
4
```

# [ ] Fix the program below so it displays the area of a square with side = 2
# and the volume of a cube with side = 2
# Global scope
s = 2
# Compute the area of a square
def square_area (side):
# area is a local variable in square_area
global area
area = side ** 2
return area
# Compute the volume of a cube
def cube_volume (side):
# cube volume = area of base X side
volume = area * side # area is not defined within this scope
return volume
square_area(s)
# area was deleted when the local scope of square_area was destroyed
cube_volume(s)

# [ ] The program below converts US Dollars to Euros, British Pounds, and Japanese Yen
# Complete the functions USD2EUR, USD2GBP, USD2JPY so they all return the correct value
def USD2EUR(amount):
"""
Convert amount from US Dollars to Euros.
Use 1 USD = 0.831467 EUR
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in Euros (float)
"""
value = amount * .831467
return value
def USD2GBP(amount):
"""
Convert amount from US Dollars to British Pounds.
Use 1 USD = 0.739472 GBP
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in British Pounds (float)
"""
value = amount * .73942
return value
def USD2JPY(amount):
"""
Convert amount from US Dollars to Japanese Yen.
Use 1 USD = 112.656 JPY
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in Japanese Yen (float)
"""
value = amount * 112.656
return value
def main():
amount = float(input("Enter amount in USD: $"))
# In Euros
eur = USD2EUR(amount)
# In British Pounds
gbp = USD2GBP(amount)
# In Japanese Yen
jpy = USD2JPY(amount)
print("${:.2f} USD = {:.2f} EUR, {:.2f} GBP, {:.2f} JPY".format(amount, eur, gbp, jpy))
if __name__ == '__main__':
main()

```
$45.00 USD = 37.42 EUR, 33.27 GBP, 5069.52 JPY
```

# Compute the area of a square
def square_area (side):
# area is a local variable in square_area
# area does not conflict with the variable area in rectangle_area or area_diff
area = side ** 2
return area
# Compute the area of a rectangle
def rectangle_area (length, width):
# area is a local variable in rectangle_area
# area does not conflict with the variable area in square_aream or area_diff
area = length * width
return area
# Compute the area difference between a square and a rectangle
def area_diff (side, length, width):
# square area
area1 = square_area(side) # defines area in its local scope
# rectangle area
area2 = rectangle_area(length, width) # defines area in its local scope
# area difference
area = area2 - area1 # area is local in area_diff local scope
return area
# Call the area_diff function
print("Area difference = ", area_diff(2, 2, 3))

```
Area difference = 2
```

# [ ] The program below converts US Dollars to British Pounds. However, the conversion rate is unknown
# Complete the functions USD2EUR, EUR2GBP, and USD2GBP so they all return the correct value
# You should use USD2EUR and EUR2GBP in USD2GBP, do not try to find out the conversion rate
cf1 = .831467
cf2 = .889358
def USD2EUR(amount):
"""
Convert amount from US Dollars to Euros.
Use 1 USD = 0.831467 EUR
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in Euros (float)
"""
global cf1
cf1 = .831467
value = amount * cf1
return value
def EUR2GBP(amount):
"""
Convert amount from Euros to British Pounds.
Use 1 EUR = 0.889358 GBP
args:
amount: Euros amount (float)
returns:
value: the equivalent of amount in GBP (float)
"""
global cf2
cf2 = .889358
value = amount * cf2
return value
def USD2GBP(amount):
"""
Convert amount from US Dollars to British Pounds.
The conversion rate is unknown, you have to use USD2EUR and EUR2GBP
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in British Pounds (float)
"""
#TODO: Your code goes here
value = amount *cf1*cf2
return value
def main():
amount = float(input("Enter amount in USD: $"))
# In British Pounds
gbp = USD2GBP(amount)
print("${:.2f} USD = {:.2f} GBP".format(amount, gbp))
if __name__ == '__main__':
main()

```
$10.00 USD = 7.39 GBP
```

# Global variable
pi = 3.14
# Compute the area of a circle
def circle_area (radius):
# pi is accessible from this local scope
area = pi * radius ** 2
return area
# Global scope
a = circle_area(4)
print("circle area =", a)

```
circle area = 50.24
```

# Global variable
vowels = 'AaEeIiOoUiYy'
# Count the number of vowels in a sentence
def count_vowels(sentence):
# vowels is accessible from this local scope
count = 0
for c in sentence:
if c in vowels:
count = count + 1
return count
# Global scope
s = 'Monty Python'
print('Number of vowels in "{:s}" = {:d}'.format(s, count_vowels(s)))

```
Number of vowels in "Monty Python" = 4
```

# Global variable
pi = 3.14
# Compute the area of a circle
def circle_area (radius):
# Define pi as a global variable in this scope
global pi
pi = 3.14159265359 # More accurate pi
area = pi * radius ** 2
return area
# Global scope
print("pi =", pi)
a = circle_area(4)
print("More accurate circle area =", a)
print("Updated pi =", pi) # Global variable pi changed in circle_area

```
pi = 3.14
More accurate circle area = 50.26548245744
Updated pi = 3.14159265359
```

# String global variable
planet = 'Mercury'
# function to change the current planet
def planet_change(new_planet):
# Define planet as a global variable in this scope
global planet
planet = new_planet
# Global scope
print("Planet =", planet)
planet_change('Mars')
print("Planet =", planet)

```
Planet = Mercury
Planet = Mars
```

# Global variable
pi = 3.14
# Compute the area of a circle
def circle_area (radius):
# Assigning a value to pi without (global) makes it a local variable
pi = 3.14159265359 # more accurate pi
area = pi * radius ** 2
return area
# Global scope
print("pi =", pi)
a = circle_area(4)
print("More accurate circle area =", a)
print("Unchanged pi =", pi) # Global pi didn't change

```
pi = 3.14
More accurate circle area = 50.26548245744
Unchanged pi = 3.14
```

# String global variable
planet = 'Mercury'
# Function to change the current planet
def planet_change(new_planet):
planet = new_planet # planet is a local variable
# Global scope
print("Planet = ", planet)
planet_change('Mars')
print("Planet = ", planet)

```
Planet = Mercury
Planet = Mercury
```

# Global variable
pi = 3.14
# Compute the area of a circle
def circle_area (radius):
# pi is accessible from this local scope
area = pi * radius ** 2
return area
# Global scope
# pi is changed before it is used in circle_area
pi = 0
a = circle_area(4)
print("pi =", pi)
print("Wrong circle area =", a)

```
pi = 0
Wrong circle area = 0
```

# String global variable
planet = 'Mercury'
# Function to change the current planet
def planet_change(new_planet):
planet = new_planet # planet is a local variable
print("Planet = ", planet)
planet_change('Mars')
print("Planet = ", planet) # Global variable (planet) did not change
planet = "Earth" # Changing global variable (planet)
print("Planet = ", planet)

```
Planet = Mercury
Planet = Mercury
Planet = Earth
```

# [ ] The following program converts an amount from US Dollars to Indian Rupees using the XCHANGE_RATE variable
# Complete the function USD2INR so it performs the conversion
XCHANGE_RATE = 63.6856 # = 1 USD
def USD2INR(amount):
"""
Convert amount from US Dollars to Indian Rupees.
Use XCHANGE_RATE
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in Indian Rupees (float)
"""
value = amount * XCHANGE_RATE
return value
print("Current exchange rate $1 USD = {} INR".format(XCHANGE_RATE))
amount = 220 #USD
inr = USD2INR(amount)
print("${} = {}".format(amount, inr))

```
Current exchange rate $1 USD = 63.6856 INR
$220 = 14010.832
```

# [ ] The following program calculates the equivalent of $220 USD in Indian Rupees,
# then updates the exchange rate and performs the conversion again
# Complete the functions USD2INR and change_rate so they function according to the specifications below
XCHANGE_RATE = 63.6856 # = 1 USD
def USD2INR(amount):
"""
Convert amount from US Dollars to Indian Rupees.
Use XCHANGE_RATE
args:
amount: US dollar amount (float)
returns:
value: the equivalent of amount in Indian Rupees (float)
"""
value = amount * XCHANGE_RATE
return value
def change_rate():
"""
Change the exchange rate to 63.6782
args:
None
returns:
None
"""
global XCHANGE_RATE
XCHANGE_RATE = 63.6782
print("Current exchange rate $1 USD = {} INR".format(XCHANGE_RATE))
amount = 220 #USD
inr = USD2INR(amount)
print("${} = {}".format(amount, inr))
print()
change_rate()
print("After changing the exchange rate $1 USD = {} INR".format(XCHANGE_RATE))
inr = USD2INR(amount)
print("${} = {}".format(amount, inr))

```
Current exchange rate $1 USD = 63.6856 INR
$220 = 14010.832
After changing the exchange rate $1 USD = 63.6782 INR
$220 = 14009.204
```

# [ ] In the following program, the function `flip()` is designed to reverse the order of the elements in NUMBERS
# Fix the `UnboundLocalError` exception without changing the expression (NUMBERS = NUMBERS[-1:0:-1])
NUMBERS = [1, 2, 3, 4, 5, 6]
def flip():
global NUMBERS
NUMBERS = NUMBERS[-1::-1]
print("Before flipping, NUMBERS =", NUMBERS)
flip()
print("After flipping, NUMBERS =", NUMBERS)

```
Before flipping, NUMBERS = [1, 2, 3, 4, 5, 6]
After flipping, NUMBERS = [6, 5, 4, 3, 2, 1]
```