我的代码在While循环中卡住了(Python)

pod7payv  于 5个月前  发布在  Python
关注(0)|答案(1)|浏览(80)

我希望循环检查值y是整数或不是除法后,如果是,然后退出循环,如果不是连续的,直到它是,但它是在循环卡住了,我不知道为什么,请帮助我。
这是我的代码:
第一个月

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))

字符串

0md85ypi

0md85ypi1#

如果你打印出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循环继续。这是一个众所周知的固有limitation of floating point arithmetic.如果你想用小数实现任意精度,你可以使用使用十进制库。

相关问题