I';我在获取If_Elif处理Python代码时遇到了问题
我是python的初学者,在使这个过程正常运行方面没有成功。有人能帮忙吗? If和Elif构成无效语法, 我该怎么办 我正在使用Python3.7.2 Shell IDLE 1。将温度华氏度转换为摄氏度,将摄氏度转换为华氏度I';我在获取If_Elif处理Python代码时遇到了问题,python,Python,我是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
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")
可以使用
pass
在python中创建空块
Edit:刚刚意识到最终的print语句应该是一条错误消息,因此如果您只是修复最后一部分的格式,我认为它可以工作
temp=input(“输入您想要转换的温度”(如45F、102C等):”)
度=整数(温度[:-1])
i_约定=温度[-1]
如果i_convention.upper()=“C”:
结果=整数(圆形((9度)/5+32))
o_convention=“华氏度”
打印(“o_约定中的温度为”,结果为“度”)
elif i_约定.upper()=“F”:
结果=整数(圆形((度-32)*5/9))
o_convention=“摄氏度”
打印(“o_约定中的温度为”,结果为“度”)
其他:
打印(“结果有效期”)
您需要在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")
我建议将input语句包装到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
也许
python
标记应该在这里,而不是c
?欢迎使用堆栈溢出!您使用的标记不适合此问题。请参加考试、复习和发帖。请记住,在提问时,至少要阅读您正在使用的标签上的鼠标盖。如果您发布了实际的错误消息,这将非常有用。我猜这是因为else
块只包含注释。看见