【发布时间】:2019-02-24 14:18:44
【问题描述】:
我在 Python 中的温度转换程序遇到了一个令人困惑的问题,至少因为我是新手,所以让我感到困惑。我有两个地点,德国和美国,一个用户来自的国家和用户当前所在的国家。我只是想将温度从用户当前所在国家/地区的温标转换为用户所在国家/地区的温标。
例如,用户来自德国,但目前在美国。因此,在这种情况下,我希望程序将用户输入的温度从摄氏度转换为华氏度。
我的代码:
location = input("Where are you from?")
us = ("USA")
ger = ("Germany")
if location == ger:
print("You are from Germany")
elif location == us:
print("You are from the USA")
else:
print("Enter the country Germany or USA")
recentLoc = input("What is your location right now?")
if recentLoc == ger:
print("You are in Germany right now")
elif recentLoc == us:
print("You are in the USA right now")
else:
print("Please enter the country Germany or the USA")
temp = input("What is the temperature outdoor tomorrow?")
def convert_f():
f = float(fahrenheit)
f = (temp*9/5)+32
return(f)
def convert_c():
c = float(celsius)
c = (temp-32)*5/9
return(c)
if recentLoc == ger and location == us:
print("Temperature for tomorrow is " + float(c) + "Celsius or " + float(f) + "Fahrenheit")
elif recentLoc == us and location == ger:
print("Temperature for tomorrow is " + float(f) + "Fahrenheit or " + float(c) + "Celsius")
elif recentLoc == us and location == us:
print("Temperature for tomorrow is " + float(f) + "Fahrenheit")
elif recentLoc == ger and location == ger:
print("Temperature for tomorrow is " + float(c) + "Celsius")
else:
print("Please type in a number")
错误信息:
NameError: name 'f' is not defined
【问题讨论】:
-
f定义在哪里?
标签: python python-3.x function if-statement temperature