# Anna Krupnova
# November 27, 2022
# In this lesson, I learned how to use global and local functions
# In this lesson, I haven't strugled with anything

# 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
area = square_area(2)
# area is not within scope anymore and cannot be
# accessed from this global scope
print(area)

```
4
```

# Compute the area of a square
def square_area (side):
# area is a local variable in square_area
area = side ** 2
return area
area = square_area(2)
# 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
# area was deleted when the local scope of square_area was destroyed
cube_volume(s)

# 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
return area
# [ ] Global scope
square_area(2)
# [ ] area is not within scope anymore and cannot be
# [ ] accessed from this global scope
print(area)

```
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
# [ ] Compute the area of a square
def square_area (side):
# area is a local variable in square_area
area = side ** 2
return area
print(square_area(2))
# [ ] 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)

```
4
```

# [ ] The program below creates 3 functions that convert US Dollars to Euros, British Pounds, and Japanese Yen
# [ ] Complete the functions USD2EUR, USD2GBP, USD2JPY so they all return the correct value using the given args and returns.
# [ ] Retrieve an amount of dollars from the user and call each function from a separate print statement
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)
"""
#TODO: Your code goes here
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)
"""
#TODO: Your code goes here
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)
"""
#TODO: Your code goes here
value = amount * 112.656
return value
def main():
amount = float(input("$:"))
GBP = USD2GBP(amount)
JPY = USD2JPY(amount)
EUR = USD2EUR(amount)
print("${:.2f} USD = {:.2f} EUR, {:.2f} JPY, {:.2f} GBP".format(amount, EUR, JPY, GBP))
if __name__ == '__main__':
main()

```
$435.00 USD = 361.69 EUR, 49005.36 JPY, 321.65 GBP
```

# 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
```

# 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)
"""
#TODO: Your code goes here
value = amount * XCHANGE_RATE
return value
print("Current exchange rate $1 USD =", XCHANGE_RATE, "rupees")
amount = 220 #USD
inr = USD2INR(amount)
print("$",amount,"=",inr,"rupees")

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

# [ ] 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)
"""
#TODO: Your code goes here
value = amount * XCHANGE_RATE
return value
def change_rate():
"""
Change the exchange rate to 63.6782
args:
None
returns:
None
"""
#TODO: Your code goes here
global XCHANGE_RATE
XCHANGE_RATE = 63.6782
print("Current exchange rate $1 USD =", XCHANGE_RATE, "rupees")
amount = 220 #USD
inr = USD2INR(amount)
print("$",amount,"=",inr,"rupees")
print()
change_rate()
print("After changing the exchange rate $1 USD =", XCHANGE_RATE, "rupees")
inr = USD2INR(amount)
print("$",amount,"=",inr,"rupees")

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

# [ ] 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::-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]
```