当前位置: 动力学知识库 > 问答 > 编程问答 >

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
网友答案:

You are multiplying to a variable with zero value

分享给朋友:
您可能感兴趣的文章:
随机阅读: