NameError: name 'C' is not defined` error Converting Temperature
NameError: name 'C' is not defined` error Converting Temperature
我正在编写这个 Python 代码,将华氏度转换为摄氏度,第二个代码将摄氏度转换为华氏度。我以相同的方式编写每个代码,使用 (if/else) 但每当我尝试使第一个条件为真时。例如,当温度 F = -400 时,我得到一个 NameError: name 'C' is not defined
错误。
我尝试更改 C 的行位置,使其在最后一个打印行之前运行。但仍然没有运气。将 C 转换为 F 的第二个代码部分运行时没有出现此错误。我尝试将 C 的方程式放在最后一行打印之后,但仍然出现相同的错误。
这是否是我在第一部分中遗漏的东西,可能会阻止我使第一个条件为真?
我是 运行 Python 2.7.10 并在 Mac OS X El Capitan (10.11)
上使用终端
将华氏度转换为摄氏度:
F = int(raw_input("Enter Temperature In Fahrenheit:"))
if F >= (-459.67):
print "Temperature in absolute zero cannot be achieved"
else:
C = F - 32 * (0.555556)
print "The temperature is %.1f" 'C' % C
将摄氏度转换为华氏度:
C = int(raw_input("Enter Temperature In Celsius:"))
if C <= (-273.15):
print "Temperature in absolute zero cannot be achieved"
else:
print "The temperature is %.1f" 'F' % F
F = C * (1.8) + 32
正如人们所说,有很多错误。一种解决方案是:
F = float(raw_input("Enter Temperature In Fahrenheit:"))
if F <= (-459.67):
print "Temperature in absolute zero cannot be achieved"
else:
C = F - 32 * (0.555556)
print "The temperature is %.1f" 'C' % C
# ----------------------------------------------------------
C = float(raw_input("Enter Temperature In Celsius:"))
if C <= (-273.15):
print "Temperature in absolute zero cannot be achieved"
else:
F = C * (1.8) + 32
print "The temperature is %.1f" 'F' % F
我正在编写这个 Python 代码,将华氏度转换为摄氏度,第二个代码将摄氏度转换为华氏度。我以相同的方式编写每个代码,使用 (if/else) 但每当我尝试使第一个条件为真时。例如,当温度 F = -400 时,我得到一个 NameError: name 'C' is not defined
错误。
我尝试更改 C 的行位置,使其在最后一个打印行之前运行。但仍然没有运气。将 C 转换为 F 的第二个代码部分运行时没有出现此错误。我尝试将 C 的方程式放在最后一行打印之后,但仍然出现相同的错误。
这是否是我在第一部分中遗漏的东西,可能会阻止我使第一个条件为真?
我是 运行 Python 2.7.10 并在 Mac OS X El Capitan (10.11)
上使用终端将华氏度转换为摄氏度:
F = int(raw_input("Enter Temperature In Fahrenheit:"))
if F >= (-459.67):
print "Temperature in absolute zero cannot be achieved"
else:
C = F - 32 * (0.555556)
print "The temperature is %.1f" 'C' % C
将摄氏度转换为华氏度:
C = int(raw_input("Enter Temperature In Celsius:"))
if C <= (-273.15):
print "Temperature in absolute zero cannot be achieved"
else:
print "The temperature is %.1f" 'F' % F
F = C * (1.8) + 32
正如人们所说,有很多错误。一种解决方案是:
F = float(raw_input("Enter Temperature In Fahrenheit:"))
if F <= (-459.67):
print "Temperature in absolute zero cannot be achieved"
else:
C = F - 32 * (0.555556)
print "The temperature is %.1f" 'C' % C
# ----------------------------------------------------------
C = float(raw_input("Enter Temperature In Celsius:"))
if C <= (-273.15):
print "Temperature in absolute zero cannot be achieved"
else:
F = C * (1.8) + 32
print "The temperature is %.1f" 'F' % F