# Pavan Kumpatla
# December 27, 2021
# I learned about global and local variable - scopes
# I didn't have any trouble
# Compute the area of a square
def square_area (side):
# area is a local variable in square_area
area = side ** 2
# side to the power of 2, stores
return area
# returns
# Global scope
square_area(2)
# calls function
# area is not within scope anymore and cannot be
# accessed from this global scope
print(area)
# prints - error
# 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
# error since "area" is from another function
return volume
# returns
# Global scope
s = 2
# stores
square_area(s)
# area was deleted when the local scope of square_area was destroyed
cube_volume(s)
# calls
# 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
# stores the side, which is to the power of 2
print("square area =", area)
# prints
# 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
# stores
print("rectangle area =", area)
# prints
# Global scope
square_area(2)
# calls function
rectangle_area(2, 3)
# calls function
# [ ] 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
# stores the side to the power of 2
return area
# returns
# Global scope
square_area(2)
# calls
# area is not within scope anymore and cannot be
# accessed from this global scope
print(square_area(2))
# prints
# [ ] 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
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 * 0.831467
# multiplies, stores
return value
# stores
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 * 0.739472
# multiplies, stores
return value
# stores
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
# multiplies, stores
return value
# stores
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()
# 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
# stores function
# rectangle area
area2 = rectangle_area(length, width) # defines area in its local scope
# stores function
# area difference
area = area2 - area1 # area is local in area_diff local scope
# subtracts, stores
return area
# returns
# Call the area_diff function
print("Area difference = ", area_diff(2, 2, 3))
# prints, calls function
# [ ] 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
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 * 0.831467
# multiplies
return value
# stores
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)
"""
value = amount * 0.889358
# multiplies
return value
# stores
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)
"""
value2 = amount * 0.831467
# multiplies
value = value2 * 0.889358
# multiplies
return value
# stores
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()
# Global variable
pi = 3.14
# stores
# Compute the area of a circle
def circle_area (radius):
# pi is accessible from this local scope
area = pi * radius ** 2
# stores
return area
# returns
# Global scope
a = circle_area(4)
# calls function, stores
print("circle area =", a)
# prints
# Global variable
vowels = 'AaEeIiOoUiYy'
# stores
# Count the number of vowels in a sentence
def count_vowels(sentence):
# vowels is accessible from this local scope
count = 0
# stores
for c in sentence:
# for c in sentence
if c in vowels:
# if c is in vowels
count = count + 1
# add 1 to count
return count
# returns
# Global scope
s = 'Monty Python'
# stores
print('Number of vowels in "{:s}" = {:d}'.format(s, count_vowels(s)))
# prints, formats
# Global variable
pi = 3.14
# stores
# Compute the area of a circle
def circle_area (radius):
# Define pi as a global variable in this scope
global pi
# pi is now global
pi = 3.14159265359 # More accurate pi
area = pi * radius ** 2
# multiplies, stores
return area
# returns
# Global scope
print("pi =", pi)
# prints
a = circle_area(4)
# calls function, store
print("More accurate circle area =", a)
# prints
print("Updated pi =", pi) # Global variable pi changed in circle_area
# prints
# String global variable
planet = 'Mercury'
# stores
# function to change the current planet
def planet_change(new_planet):
# Define planet as a global variable in this scope
global planet
# planet is now global
planet = new_planet
# stores
# Global scope
print("Planet =", planet)
# prints
planet_change('Mars')
# calls function
print("Planet =", planet)
# prints
# Global variable
pi = 3.14
# stores
# 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
# stores
area = pi * radius ** 2
# multiplies, stores
return area
# returns
# Global scope
print("pi =", pi)
# prints
a = circle_area(4)
# calls function, stores
print("More accurate circle area =", a)
# prints
print("Unchanged pi =", pi) # Global pi didn't change
# prints
# String global variable
planet = 'Mercury'
# stores
# Function to change the current planet
def planet_change(new_planet):
# creates new function with parameter
planet = new_planet # planet is a local variable
# stores
# Global scope
print("Planet = ", planet)
# prints
planet_change('Mars')
# calls function
print("Planet = ", planet)
# prints
# 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)
# String global variable
planet = 'Mercury'
# stores
# Function to change the current planet
def planet_change(new_planet):
# creates new function with parameter
planet = new_planet # planet is a local variable
# stores
print("Planet = ", planet)
# prints
planet_change('Mars')
# calls function
print("Planet = ", planet) # Global variable (planet) did not change
# prints
planet = "Earth" # Changing global variable (planet)
# reassigns
print("Planet = ", planet)
# prints
# [ ] 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 = XCHANGE_RATE * amount
# multiplies, stores
return value
# returns
print("Current exchange rate $1 USD = {} INR".format(XCHANGE_RATE))
amount = 220 #USD
inr = USD2INR(amount)
print("${} = {}".format(amount, inr))
# [ ] 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 = XCHANGE_RATE * amount
# multiplies, stores
return value
# returns
def change_rate():
"""
Change the exchange rate to 63.6782
args:
None
returns:
None
"""
global XCHANGE_RATE
# XCHANGE_RATE is now global
XCHANGE_RATE = 63.6782
# reassigns
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))
# [ ] 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]
# stores in list
def flip():
# creates new function
global NUMBERS
# NUMBERS is now global
NUMBERS = NUMBERS[-1::-1]
# stores
print("Before flipping, NUMBERS =", NUMBERS)
# prints
flip()
# calls function
print("After flipping, NUMBERS =", NUMBERS)
# prints