我有这段代码,但当Y变为10时,它会一直运行循环,为什么?我是python的初学者,所以请不要认为我很愚蠢或者别的什么:)
def destiny_calculator():
mass = str(input("enter the mass of your material (it could be in gram, milligram, kilogram or ton "))
if mass.isdigit() == False:
try:
float(mass)
except Exception:
x = False
y = 0
while x == False or y != 10:
print("Please enter JUST a number")
mass = str(input("enter the mass of your material (it could be in gram, milligram, kilogram or ton - "))
y = y + 1
print (y)
if y == int(10):
print("Too many errors")
exit()
else:
pass
else:
pass发布于 2020-07-08 07:02:04
由于您使用的是or,因此代码只检查其中一个条件是否满足。因此,只要x == False为真,代码就会运行。如果您希望代码在y == 10时结束,那么应该改用and操作符。这只会在两个条件都为真的情况下运行代码,并且只要其中一个条件为假(例如y == 10),代码就不会运行。
https://stackoverflow.com/questions/59095335
复制相似问题