当我运行此命令时,可以输入很好的名称,但是当我输入高度时,会出现无法排序的类型错误。

floatHeight = 0
floatWeight = 0
strName = ""

strName = input("What is your name? ")



while floatHeight <= 1 or floatHeight >= 3:
    floatHeight = input("What is your height in metres? ")
while floatWeight <= 10 or floatWeight >= 400:
    floatWeight = input("What os your weight in kilograms? ")
print(floatWeight)
print(floatHeight)

最佳答案

您需要将输入的值转换为float

while floatHeight <= 1 or floatHeight >= 3:
    floatHeight = float(input("What is your height in metres? "))
while floatWeight <= 10 or floatWeight >= 400:
    floatWeight = float(input("What os your weight in kilograms? "))


否则floatHeightfloatWeight将是一个字符串。幸运的是,在Python 3中,您无法再比较字符串和浮点数。

07-27 13:33