在Python 3中转换温度(华氏和摄氏)

时间:2018-09-10 20:14:14

标签: python-3.x if-statement conditional-statements temperature

因此,我有一个简单的程序,用户可以使用它来告诉他们来自哪里以及现在在哪里(仅使用美国和英国)。我希望该程序将用户当前所在国家/地区的正常温度标度转换为用户所在国家/地区的正常温度标度。我为此付出了很多努力,因此我很愿意提出任何建议,谢谢。

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

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")

2 个答案:

答案 0 :(得分:0)

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

predis

答案 1 :(得分:-1)

要扩展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')