I'm trying to write a code in Python three that converts temperatures and distances from one unit to another. I'm told that I need to use functions and that the user will input either F (Fahrenheit), C (Celsius), or K (Kelvin) and the temperature will be converted from whatever they choose to F, C or K. So I wrote a function for each scenario. A function for if it's converted from F, another if it's converted from C, and another if it's converted from K. I also did this for inches, feet, yards, and miles.
So after I wrote all of the functions I wrote an if, elif, and else statement which I want to call each function depending on what the user types. So if the user inputs "F" I want it to call the convert_from_F() function, or if they put "K" I want it to use the convert_from_K(), or if the user types "inch" I want it to use the convert_from_inch() function. I thought that the way to do this would be to use the if, elif, and else statement. However, no matter what I type I'm always given the error message:
NameError: name 'F' is not defined
I would have thought that the iterations would continue past each statement if it found that the user didn't input that particular instruction. It also doesn't work if I (as the user) put 'F.' I'm not sure how to fix this code. What am I missing from it?
Also, I'm using an online python 3 compiler to write my program: OnlineGDB
def convert_from_F():
F = float(input("Please input the temperature in F: "))
print("Temperature in F is", F)
print("Temperature in C is", (F-32)*5/9)
print("Temperature in K is", (F-32)*5/9 + 273.15)
def convert_from_C():
C = float(input("Please input the temperature in C: "))
print("Temperature in F is", (C*9/5) + 32)
print("Temperature in C is", C)
print("Temperature in K is", C+273.15)
def convert_from_K():
K = float(input("Please input the temperature in K: "))
print("Temperature in F is", (K-273.15)*9/5 + 32)
print("Temperature in C is", K-273.15)
print("Temperature in K is", K)
def convert_from_inch():
inch = float(input("Please input the distance in inches: "))
print("Distance in inches is:", inch)
print("Distance in feet is:", inch/12)
print("Distance in yards is:", inch/36)
print("Distance in miles is:", inch/63360)
def convert_from_ft():
ft = float(input("Please input the distance in feet: "))
print("Distance in inches is:", ft*12)
print("Distance in feet is:", ft)
print("Distance in yards is:", ft/3)
print("Distance in miles is:", ft/5280)
def convert_from_yd():
yd = float(input("Please input the distance in yards: "))
print("Distance in inches is:", yd*36)
print("Distance in feet is:", yd*3)
print("Distance in yards is:", yd)
print("Distance in miles is:", yd*1760)
def convert_from_mi():
mi = float(input("Please input the distance in miles: "))
print("Distance in inches is:", mi*63360)
print("Distance in feet is:", mi*5280)
print("Distance in yards is:", mi*1760)
print("Distance in miles is:", mi)
print("To convert distance input inch, ft, yd, or mi. To convert \
temperatures please input F, C, or K. ")
user_input = input("Your input: ")
def user_conversion():
if user_input == F or f:
convert_from_F()
elif user_input == C or c:
convert_from_C()
elif user_input == K or k:
convert_from_K()
elif user_input == inch:
convert_from_inch()
elif user_input == ft:
convert_from_ft
elif user_input == yd:
convert_from_yd()
elif user_input == mi:
convert_from_mi()
else:
print("Invalid input.")
print(user_conversion())
Edit: I saw that this was marked as a possible duplicate. The duplicate question doesn't help me, I wasn't trying to make this into a set, I was trying to figure out how I could make an if statement run. The solutions given for my problem aren't like the ones for the other question. However, I'm new to Python so it's entirely possible that I can't make the connections that more experienced people can make. The answers I received here did help me.