简体   繁体   中英

Why is my While loop infinite

This while loop is giving me trouble as it simply will not stop, I'm trying to update elem to eventually be larger then char by using an exponent i but that simply doesn't happen and was wondering if there were any solutions.

i = 0
char = 20
elem = 2
while elem < char:
    elem**i
    i += 1

The problem is that you are not changing the value of elem , you are just repeatedly calculating elem**i , so when you compare elem to char it is always the same result. The simplest solution is to compare elem**i to char .

You probably meant:

while elem**i < char:
    i += 1

您正在乘以一个零值的变量

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM