gpt4 book ai didi

python - 不可排序类型 int 和 string

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:00 25 4
gpt4 key购买 nike

当我运行这个程序时,我可以很好地输入名称,但是当我输入高度时,我收到不可排序的类型错误。

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 中,你不能再比较字符串和 float 了。

关于python - 不可排序类型 int 和 string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36397310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com