在Python 3中翻译温度(华氏温度和摄氏温度)

问题描述 投票:0回答:2

所以我有这个简单的程序,用户可以用它来告诉他们来自哪里以及他们目前在哪里(仅使用美国和英国)。我希望该程序能够将温度从用户当前所在国家的通常温度范围转换为通常的温度范围,直至它们来自的国家。我正在努力想出一个合理的解决方案,所以我愿意接受任何建议,谢谢。

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

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")
python-3.x if-statement conditional-statements temperature
2个回答
-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')

0
投票

你应该只使用转换方程式并做一些我在下面的内容。

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)
© www.soinside.com 2019 - 2024. All rights reserved.