我是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
。有什么想法吗?
答案 0 :(得分: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
答案 1 :(得分:1)
你需要删除它:
print f
调用,或者之后移动它,或者缩进它,因为elif必须紧跟在if块之后,并且print语句按照它的方式缩进,它结束if块。
答案 2 :(得分: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"
答案 3 :(得分:-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华氏度
希望我能有所帮助。 ;)