0

我是 Python 新手,如果要将华氏温度转换为摄氏温度,我正在尝试制作一个 Python 程序。这是程序:

x = (raw_input("would you like to convert fahrenheit(f) or celsius(c)?"))
if x == "f":
 y = (raw_input("what is the fahrenheit?"))
 f = (int(y) - 32) * 5.0 / 9
print f

if x == "c":
 n = (raw_input("what is the celsius?"))
 z = (int(n) *9) / 5 + 32
 print "and in fahrenheit, that is:"
 print z

我试图更改if x == "c"elif x == "c",但它给了我一个TextError. 有任何想法吗?

4

4 回答 4

3

只是缩进print

x = raw_input("would you like to convert fahrenheit(f) or celsius(c)?")
if x == "f":
    y = raw_input("what is the fahrenheit?")
    f = (int(y) - 32) * 5.0 / 9
    print f
elif x == "c":
    n = raw_input("what is the celsius?")
    z = (int(n) * 9) / 5.0 + 32
    print "and in fahrenheit, that is:"
    print z
于 2013-05-20T03:19:12.157 回答
1

一个简单的方法可以是:

x = (raw_input("would you like to convert fahrenheit(f) or celsius(c)? "))
if x == "f":
    y = (raw_input("what is the fahrenheit?"))
    f = (int(y) - 32) * 5.0 / 9
    print "and in celsius, that is: ",
    print f
elif x == "c":
    y = (raw_input("what is the celsius?"))
    f = (int(y) *9) / 5.0 + 32
    print "and in fahrenheit, that is: ",
    print f
else
    print "Error"
于 2013-05-20T03:29:33.813 回答
1

您需要删除它:

print f

调用,或移动它,或缩进它,因为 elif 必须紧跟在 if 块之后,并且 print 语句按原样缩进,它结束了 if 块。

于 2013-05-20T03:17:04.420 回答
-1

做以下兄弟(包括“def cel_fah(C):”以及)

def cel_fah(C):

'''
Takes in temps in celsius and gives them out in fahrenheit
'''

F=abs(C*9/5+32)

print(f'{C}°celsius is equal to {F}° fahrenheit')

然后你可以像我现在一样轻松地调用函数并得到结果(你可以在 () 中传递任何摄氏度的数字)

{对于度数符号(°),您可以使用 alt+248}

cel_fah(0)

0° 摄氏度等于 32.0° 华氏度

希望我能有所帮助。;)

于 2018-06-24T12:05:21.297 回答