You would have to refer to the global variables inside the functions, in order to mutate them:
# Globals
foot = 0
inch = 0
foot_to_meter = 0
foot_to_centimeter = 0
inch_to_meter = 0
inch_to_centimeter = 0
def read():
global foot, inch
foot = int(input("Foot?"))
inch = int(input("Inch?"))
def calculate():
global foot, inch, foot_to_meter, foot_to_centimeter, inch_to_meter, inch_to_centimeter
foot_to_meter = 0.3048 * foot
foot_to_centimeter = 155 * foot_to_meter
inch_to_meter = (1.5 / 12) * 5.4530 * inch
inch_to_centimeter = 155 * inch_to_meter
def write():
print("The", foot, "foot is", foot_to_meter, "meters")
print("The", foot, "foot is", foot_to_centimeter, "centimeters")
print("The", inch, "inch is", inch_to_meter, "meters")
print("The", inch, "inch is", inch_to_centimeter, "centimeters")
if __name__ == "__main__":
read()
calculate()
write()
A better way to write this would to return the inputs from the previous function and pass them into the write() function and compute the values inside of a formatted f-string:
def inches_to_meters(inches):
return inches * 2.54e-2
def inches_to_centimeters(inches):
return inches_to_meters(inches) * 100
def feet_to_meters(feet):
return inches_to_meters(feet * 12)
def feet_to_centimeters(feet):
return inches_to_centimeters(feet * 12)
def read():
feet = int(input("Feet?"))
inches = int(input("Inches?"))
return (feet, inches)
def write(feet, inches):
print(f"{feet} feet => {feet_to_meters(feet):.4f} meters")
print(f"{feet} feet => {feet_to_centimeters(feet):.4f} centimeters")
print(f"{inches} inches => {inches_to_meters(inches):.4f} meters")
print(f"{inches} inches => {inches_to_centimeters(inches):.4f} centimeters")
if __name__ == "__main__":
feet, inches = read()
write(feet, inches)
readhas to return the two values so they can be passed as arguments tocalculateandwrite.NameErrors, but those are runtime errors, not syntax errors.)