如何在为用户输入变量分配变量时修复代码中的“无效语法”错误?

问题描述 投票:1回答:1

在第3行,名为temp_unit的变量不被视为变量,并且显示无效语法。

temp = int(input("what is the Temperature?:")
temp_unit = input("Celcius or Farenheit?:")
if temp_unit == "C" or temp_unit=="c":
    print("The given temperature in Fahrenheit is "+(9/5)*(temp+32))
else:
    print("The given temperature in Celcius is "+(5/9)*(temp-32))
python variables syntax
1个回答
0
投票

你需要添加括号,所以它应该是这样的:

temp=int(input("what is the Temperature?:"))
temp_unit= input("Celcius or Farenheit?:")
if temp_unit == "C" or temp_unit=="c":
    print("The given temperature in Fahrenheit is "+(9/5)*(temp+32))
else:
    print("The given temperature in Celcius is "+(5/9)*(temp-32))
© www.soinside.com 2019 - 2024. All rights reserved.