在 Python 3 中转换摄氏度和华氏度

Posted

技术标签:

【中文标题】在 Python 3 中转换摄氏度和华氏度【英文标题】:Converting celsius and fahrenheit in Python 3 【发布时间】: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 定义在哪里? 【参考方案1】:

您的定义语句尚未运行,但不需要简单地交换

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)

f = (temp*9/5)+32
c = (temp-32)*5/9

【讨论】:

代码中没有fahrenheitcelsius 变量。再次仔细检查代码。【参考方案2】:

您的代码中有几个错误。这是一个有效的解决方案。我没有显示我没有触及的代码的初始部分。

# User input here
# if else statements here

recentLoc = input("What is your location right now?")

temp = float(input("What is the temperature outdoor tomorrow?"))

def convert_f(temp): # The function modified
    f = (temp*9/5)+32
    return(str(f))

def convert_c(temp): # The function modified
    c = (temp-32)*5/9 
    return(str(c))

if recentLoc == ger and location == us:
    print("Temperature for tomorrow is " + convert_c(temp) + "Celsius or " + convert_f(temp) + "Fahrenheit")
elif recentLoc == us and location == ger:
    print("Temperature for tomorrow is " + convert_f(temp) + "Fahrenheit or " + convert_c(temp) + "Celsius")
elif recentLoc == us and location == us:
    print("Temperature for tomorrow is " + convert_f(temp) + "Fahrenheit")
elif recentLoc == ger and location == ger:
    print("Temperature for tomorrow is " + convert_c(temp) + "Celsius")
else:
    print("Please type in a number")

【讨论】:

【参考方案3】:

您只定义了转换函数,但没有调用它们。

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(temp):
    temp = float(temp)
    f = (temp*9/5)+32
    return(f)

def convert_c(temp):
    temp = float(temp)
    c = (temp-32)*5/9
    return(c)

if recentLoc == ger and location == us:
    print("Temperature for tomorrow is " + temp + "Celsius or " + str(convert_f(temp)) + " Fahrenheit")
elif recentLoc == us and location == ger:
    print("Temperature for tomorrow is " + temp + "Fahrenheit or " + str(convert_c(temp)) + " Celsius")
elif recentLoc == us and location == us:
    print("Temperature for tomorrow is " + temp + "Fahrenheit")
elif recentLoc == ger and location == ger:
    print("Temperature for tomorrow is " + temp + "Celsius")
else:
    print("Please type in a number")

【讨论】:

【参考方案4】:

您的代码中有一些错误:

    您定义的两个函数convert_fconvert_c 不能使用华氏度或摄氏度,因为您没有在任何地方定义它们。我猜你想提供这些值作为参数。
def convert_f(fahrenheit):
    f = float(fahrenheit)
    f = (f*9/5)+32
    return(f)

def convert_c(celsius):
    c = float(celsius)
    c = (c-32)*5/9
    return(c)
    在最后几行中,您使用了convert_fconvert_c 的返回值的名称。这些是从未创建的,因为您从不调用函数,即使调用了它们也不能像那样访问。返回值的名称在函数之外失去了所有意义。你可以做的是这样的事情:
temp = float(temp)

if recentLoc == ger and location == us:
    print("Temperature for tomorrow is :.2f Celsius or :.2f Fahrenheit".format(temp, convert_f(temp)))
elif recentLoc == us and location == ger:
    print("Temperature for tomorrow is :.2f Fahrenheit or :.2f Celsius".format(temp, convert_c(temp)))
elif recentLoc == us and location == us:
    print("Temperature for tomorrow is :.2f Fahrenheit".format(temp))
elif recentLoc == ger and location == ger:
    print("Temperature for tomorrow is :.2f Celsius".format(temp))
else:
    # Technicaly this is printed when either recentLoc or location are neither ger or us
    print("Please type in a number")    

我使用tempconvert_fconvert_c 的输出来打印输出。此外,您不能添加字符串和浮点数。您可以通过str() 将浮点数转换为字符串,例如:"This is a float " + str(float(5)) + "!"。这有点hacky,不被认为是很棒的代码。在上面的代码中,我使用了format() 函数,它不仅为您提供更清晰、更易读的代码,它还可以进行一些格式化,例如在上面的代码中,任何浮点数都只给出了 2 个精度点,而不是全部计算出来的。

    代码开头的问题有点破。您正确检查输入是德国还是美国,如果不是这种情况,则打印错误消息,但之后不要重复您的问题。我建议使用简单的 while 循环,并在得到正确答案时使用 break。
location = ""

while location != us and location != ger:
    location = input("Where are you from?")

    if location == ger:
        print("You are from Germany")
        break
    elif location == us:
        print("You are from the USA")
        break
    else:
        print("Enter the country Germany or USA")

recentLoc = ""

while recentLoc != us and recentLoc != ger:
    recentLoc = input("What is your location right now?")

    if recentLoc == ger:
        print("You are in Germany right now")
        break
    elif recentLoc == us:
        print("You are in the USA right now")
        break
    else:
        print("Please enter the country Germany or the USA")


while 1:
    try:
        temp = input("What is the temperature outdoor tomorrow?")
        temp = float(temp)
        break
    except ValueError:
        print("That's not a number!")

希望对你有所帮助...

【讨论】:

以上是关于在 Python 3 中转换摄氏度和华氏度的主要内容,如果未能解决你的问题,请参考以下文章

利用Python完成华氏度和摄氏度的转换

Python温度Fa和Ce转换

[Python3 练习] 001 温度转换1

[Python3 练习] 002 温度转换2

如何用 python编写华氏摄氏度的相互转换?

python3 练手实例3 摄氏温度与华氏温度转换