【发布时间】:2017-01-31 07:28:16
【问题描述】:
Fahr = input("Insert temperature in Fahrenheit: " )
while type(Fahr) != float or type(Fahr) != int:
Fahr = input("Error: Insert temperature in Fahrenheit: " )
Celcius = ((Fahr) - 32) * 5/9
print("Your imput: " + Fahr + " Fahrenheit" )
print("This is equal to: " + Celcius + " Celcius" )
我需要确保用户只插入 INT 或浮点数。 当我执行此代码时,即使我插入 INT 或浮点数,它也会自动进入 while 循环。 这段代码有什么问题,应该怎么办?
【问题讨论】:
-
Fahr将始终是一个字符串 (str),这就是input()返回的内容。 -
我相信解决方案是使用
and运算符而不是or运算符。一个数字不能同时是整数和浮点数。 -
这是 Python 2 还是 Python 3?
input()两者之间有不同的功能,我假设是 python 3。