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

```
4
```

# 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
area = side **2
volume = area * side # area is not defined within this scope
return volume
# Global scope
s = 2
print(square_area(s))
# area was deleted when the local scope of square_area was destroyed
print(cube_volume(s))

```
4
8
```

# 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
# just needed to add a 2 at the bottom
# [ ] 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
a=square_area(2)
# [ ] area is not within scope anymore and cannot be
# [ ] accessed from this global scope
print(a)

```
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
# [ ] Compute the volume of a cube
def cube_volume (side):
# cube volume = area of base X side
area = side ** 2
volume = area * side # area is not defined within this scope
return volume
# Global scope
s = 2
print(square_area(s))
# area was deleted when the local scope of square_area was destroyed
print(square_area(s))

```
4
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):
value=0.831467
return amount*value
print(USD2EUR(1))
def USD2GBP(amount):
value=amount*0.739472
return value
print(USD2GBP(1))
def USD2JPY(amount):
value=amount*112.656
return value
print(USD2JPY(1))

```
0.831467
0.739472
112.656
```

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

# 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):
value=amount*XCHANGE_RATE
return value
"""
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
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):
value=amount*XCHANGE_RATE
return value
def change_rate():
global XCHANGE_RATE
XCHANGE_RATE=63.6782
print("Current exchange rate $1 USD =", XCHANGE_RATE, "rupees")
amount = 220 #USD
inr = (amount)
print("$",amount,"=",inr,"rupees")
print()
change_rate()
print("After changing the exchange rate $1 USD =", XCHANGE_RATE, "rupees")
inr = (amount)
print("$",amount,"=",inr,"rupees")

```
Current exchange rate $1 USD = 63.6856 rupees
$ 220 = 220 rupees
After changing the exchange rate $1 USD = 63.6782 rupees
$ 220 = 220 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]
```