我希望循环检查除法后 y 值是否为整数,如果是,则退出循环,如果不是继续,直到它是,但它卡在循环中,我不知道为什么,请帮助我。
她是我的代码:
your text
print("Welcome to Straircase Calculater")
print("")
print("")
x = float(input("Enter Floor Height = "))
riser = 15.0
y = x/riser
checkint = y.is_integer()
while checkint == False :
riser += 0.1
y = x/riser
checkint = y.is_integer()
print("Number of Steps = " + str(y))
print("Step Riser = " + str(riser))
如果打印出立管的值,您将看到类似这样的内容:
Welcome to Straircase Calculater
Enter Floor Height = 20
15.1
15.2
15.299999999999999
15.399999999999999
15.499999999999998
15.599999999999998
15.699999999999998
15.799999999999997
15.899999999999997
15.999999999999996
...
15.999999999999996
不是整数,因此 while 循环继续进行。这是浮点运算众所周知的固有限制。如果您想使用小数实现任意精度,可以使用 decimal 库。