将温度(华氏度和摄氏度)转换为 Python 3

Translating temperature (Fahrenheit and Celsius) in Python 3

所以我有这个简单的程序,用户可以使用它来判断他们来自哪里以及他们目前所在的位置(为此仅使用美国和英国)。我希望该程序将用户当前所在国家/地区的常用温标转换为用户所在国家/地区的常用温标。我正在努力想出一个合理的解决方案,所以我愿意接受任何建议,谢谢。

到目前为止,这是我的代码:

location = input("Where are you from?\n")
uk = ("the UK")
us = ("the USA")
if location == uk:
print("You are from the UK.\n")
elif location == us:
print("You are from the USA.\n")
else:
print("Sadly, I cannot help you.\n")
locationNow = input("Where are you currently at?\n")
if locationNow == uk:
print("You are currently in the UK.\n")
elif locationNow == us:
print("You are currently in the US.\n")
else:
print("Sadly I cannot help you.\n")
temp = input("What is the temperature for tomorrow?\n")

你应该只使用转换方程式,并按照我下面的内容做一些事情。

temp = input("What is the temperature for tomorrow?\n")

def c2f():
    C_to_F = (temp × 9/5) + 32
def f2c():
    F_to_C = (temp - 32) × 5/9

F_or_C = input('Is your temperature in F or C?')

if F_or_C == F:
    f2c(temp)
    return(temp)

elif F_or_C == C:
    c2f(temp)
    return(temp)

要扩展 FanMan 提供的答案,您可以这样做:

def c2f():
    C_to_F = (temp × 9/5) + 32
    return(C_to_F)

def f2c():
    F_to_C = (temp - 32) × 5/9
    return(F_to_C)


temp = input("What is the temperature for tomorrow?\n")

if locationNow == uk & location == us:
    print('The temperature will be ' + str(temp) + 'C or ' + str(c2f(temp)) + 'F')
elif locationNow == us & location == uk:
    print('The temperature will be ' + str(temp) + 'F or ' + str(f2c(temp)) + 'C')
elif locationNow == us & location == us:
    print('The temperature will be ' + str(temp) + 'F')
else:
    print('The temperature will be ' + str(temp) + 'C')