为什么当我已经设置了规则时它会弹出错误?
why does it pop an error when I already set a rule in while?
我已经设置了如果体重和身高不是整数,请用户重新输入。但它弹出:“ValueError:以 10 为底的 int() 的无效文字”。感谢您的帮助
while type(weight) != int and type(height) != int and weight not in weightrange and height not in heightrange:
weight = int(input("Enter a weight number please (kg): "))
height = int(input("Enter a height number please (cm): "))
我要插话了,因为我认为你走错了路。你必须记住,事情是按顺序执行的,从上到下。你不能“建立规则”并希望将来会应用该规则。 Python 只是行不通。有些语言有,Python 没有。
while True:
weight = input("Enter a weight number please (kg): ")
if weight.isdigit():
weight = int(weight)
if weight in weightrange:
break
else:
print( "Weight not in range.")
else:
print( "Please enter an integer.")
while True:
height = input("Enter a height number please (cm): ")
if height.isdigit():
height = int(height)
if height in heightrange:
break
else:
print( "Height not in range.")
else:
print( "Please enter an integer.")
如果你真的这样做,你可能会创建一个函数来避免输入两次。
就这么办,
while True:
try:
weight = int(input("Enter a weight number please (kg): "))
height = int(input("Enter a height number please (cm): "))
break
except:
print("Invalid Input")
您也可以修改它以适合您的身高和体重范围。
我已经设置了如果体重和身高不是整数,请用户重新输入。但它弹出:“ValueError:以 10 为底的 int() 的无效文字”。感谢您的帮助
while type(weight) != int and type(height) != int and weight not in weightrange and height not in heightrange:
weight = int(input("Enter a weight number please (kg): "))
height = int(input("Enter a height number please (cm): "))
我要插话了,因为我认为你走错了路。你必须记住,事情是按顺序执行的,从上到下。你不能“建立规则”并希望将来会应用该规则。 Python 只是行不通。有些语言有,Python 没有。
while True:
weight = input("Enter a weight number please (kg): ")
if weight.isdigit():
weight = int(weight)
if weight in weightrange:
break
else:
print( "Weight not in range.")
else:
print( "Please enter an integer.")
while True:
height = input("Enter a height number please (cm): ")
if height.isdigit():
height = int(height)
if height in heightrange:
break
else:
print( "Height not in range.")
else:
print( "Please enter an integer.")
如果你真的这样做,你可能会创建一个函数来避免输入两次。
就这么办,
while True:
try:
weight = int(input("Enter a weight number please (kg): "))
height = int(input("Enter a height number please (cm): "))
break
except:
print("Invalid Input")
您也可以修改它以适合您的身高和体重范围。