python - 在为用户输入分配变量时如何修复代码中的 "invalid syntax"错误?

标签 python variables syntax

在第 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))

最佳答案

您需要添加括号,因此应该是这样的:

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 - 在为用户输入分配变量时如何修复代码中的 "invalid syntax"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55345004/

相关文章:

python - 使用自定义谓词对 numpy 数组进行排序

python - 迭代行并另存为 csv

无法定义变量

mysql - 此 MySQL 语法无效但有效

python - 调整顶部和右侧联合边缘图的轴大小以匹配中心图与 matplotlib

python - Httpresponse 字符串 python

Java继承(if中的局部变量/boolean 值)

python - 清空变量而不销毁它

language-agnostic - 语法糖与功能

syntax - 使用 Write 命令创建新语法 - 出现不需要的空格问题