我正在尝试设置一个“华氏到摄氏度”和“摄氏到华氏度”计算器,允许用户选择是否将摄氏度转换为华氏度或反之亦然,但我得到了一个无效的语法错误,我不太清楚原因。The code I'm using/The 'Invalid Syntax Error' 我知道这个问题可能很基本,我对python还比较陌生。你知道吗
CelsiusorFarenheit = input("Would you like Celsius to Farenheit or Farenheit
to Celcius?")
if CelsiusorFarenheit == "celsius" or "Celsius":
def Celsius():
centToFaren = int(input("Enter a temperature in Celsius: "))
Farenheit= 9.0/5.0 * centToFaren+32
print ("Temperature:", centToFaren, "Celsius =", Farenheit, "F")
else CelsiusorFarenheit == "farenheit" or "Farenheit":
def Farenheit():
farenToCent = int(Input("Enter a temperature in Farenheit: "))
Celcius=(farenToCent-32)*5.0/9.0
print("Temperature:", centToFaren, "Fahrenheit =", Celcius, "C")
我注意到上面代码中有几点。你知道吗
要解决语法错误,需要将
else
更新为elif
,后跟条件。你知道吗为了进一步改进代码2点,您可以如下所示对变量调用
lower()
并将比较更新为长格式,如下所示。你知道吗
您正在将一个条件放入
else
块中,这是一个语法错误。else
块在没有其他条件匹配时执行,因此它们不能有条件。您可以将其更改为elif
语句或删除条件。所以改变这一行:对此,请执行以下操作之一:
或者这个:
相关问题 更多 >
编程相关推荐