我是python的初学者,但我没有成功使此过程正常运行。谁能帮忙吗? If和Elif构成无效的语法, 我该怎么办?
我正在使用Python3.7.2 Shell IDLE
1。将华氏温度转换为摄氏温度,并将摄氏温度转换为华氏温度
temp = input("Input the temperature you like to convert? (e.g.,45F, 102C etc.) : ")
degree = int(temp[:-1])
i_convention = temp[-1]
if i_convention.upper() == "C":
result = int(round((9 * degree) / 5+32))
o_convention = "Fahrenheit"
print("The temperature in", o_convention, "is", result, "degrees.")
elif i_convention.upper() =="F":
result = int(round((degree - 32) * 5 / 9))
o_convention ="Celsius"
print("The temperature in", o_convention, "is", result, "degrees.")
else:
#Nul
print("Pas de resultat valide")
答案 0 :(得分:0)
您可以使用pass
在python中创建一个空块。
编辑:刚刚意识到最终的打印语句应该是一条错误消息,因此,如果您只修复最后一部分的格式,我认为它是可行的。
temp = input("Input the temperature you like to convert? (e.g.,45F, 102C etc.) : ")
degree = int(temp[:-1])
i_convention = temp[-1]
if i_convention.upper() == "C":
result = int(round((9 * degree) / 5+32))
o_convention = "Fahrenheit"
print("The temperature in", o_convention, "is", result, "degrees.")
elif i_convention.upper() =="F":
result = int(round((degree - 32) * 5 / 9))
o_convention ="Celsius"
print("The temperature in", o_convention, "is", result, "degrees.")
else:
print("Pas de resultat valide")
答案 1 :(得分:0)
您需要在else:
块中放置一些内容,Python解释器才能正常运行。
如果您从用户那里获得的输入对您的脚本无效,我建议将一些错误处理传递给else:
块。否则,请尝试将代码重构为如下形式:
temp = input("Input the temperature you like to convert? (e.g.,45F, 102C etc.) : ")
degree = int(temp[:-1])
i_convention = temp[-1]
if i_convention.upper() == "C":
result = int(round((9 * degree) / 5+32))
o_convention = "Fahrenheit"
print("The temperature in", o_convention, "is", result, "degrees.")
else:
result = int(round((degree - 32) * 5 / 9))
o_convention ="Celsius"
print("The temperature in", o_convention, "is", result, "degrees.")
print("Pas de resultat valide")
我建议将输入语句包装到while循环中以验证用户输入。
temp = None
while temp[-1].upper() != "C" && temp[-1].upper() != "F"
temp = temp = input("Input the temperature you like to convert? (e.g.,45F, 102C etc.) : ")
# Put if, else statements below