def gukan(count):
while count!=100:
print(count)
count=count+1;
gukan(0)
My question is: When I try to increment by 3 or 9 instead of 1 in count=count+1
I get an infinite loop - why is that?
This question is related to
python
When you change count = count + 1
to count = count + 3
or count = count + 9
, count
will never be equal to 100. At the very best it'll be 99. That's not enough.
What you've got here is the classic case of infinite loop: count
is never equal to 100 (sure, at some point it'll be greater than 100, but your while
loop doesn't check for this condition) and the while
loop goes on and on.
What you want is probably:
while count < 100: # Or <=, if you feel like printing a hundred.
Not:
while count != 0: # Spaces around !=. Makes Guido van Rossum happy.
Now the loop will terminate when count >= 100
.
Take a look at Python documentation.
The answers here have pointed out that because after incrementing count it doesn't equal exactly 100, then it keeps going as the criteria isn't met (it's likely you want <
to say less than 100).
I'll just add that you should really be looking at Python's builtin range
function which generates a sequence of integers from a starting value, up to (but not including) another value, and an optional step - so you can adjust from adding 1 or 3 or 9 at a time...
0-100 (but not including 100, defaults starting from 0 and stepping by 1):
for number in range(100):
print(number)
0-100 (but not including and makes sure number doesn't go above 100) in steps of 3:
for number in range(0, 100, 3):
print(number)
I would guess it makes an infinite loop bc you skip the number 100. If you set the critera to be less than 101 it should do the trick :)
def gukan(count):
while count<100:
print(count)
count=count+3;
gukan(0)
Because the condition is never true.
i.e. count !=100 never executes when you put count=count+3 or count =count+9.
try this out..while count<100
When you use count = count + 3 or count = count + 9 instead of count = count + 1, the value of count will never be 100, and hence it enters an infinite loop.
You could use the following code for your condition to work
while count < 100:
Now the loop will terminate when count >= 100.
consider the following:
def gukan(count):
while count < 100:
print(count)
count=count+3;
gukan(0) #prints ..., 93, 96, 99
def gukan(count):
while count < 100:
print(count)
count=count+9;
gukan(0) # prints ..., 81, 90, 99
you should use count < 100
because count
will never reach the exact number 100 if you use 3 or 9 as the increment, thus creating an infinite loop.
Good luck!~ :)
The first line defines the variable. The second line loops it to 100, the third adds 1 to a and the 4th divides a by 3 and if there is no remainder (0) it will print that number otherwise it will print a blank line.
a = (0)
for i in range(0,100):
a = a + 1
if a % 3 == 0:
print(a)
else:
print("")
because if you change your code with
def gukan(count):
while count!=100:
print(count)
count=count+3;
gukan(0)
count reaches 99 and then, at the next iteration 102.
So
count != 100
never evaluates true and the loop continues forever
If you want to count up to 100 you may use
def gukan(count):
while count <= 100:
print(count)
count=count+3;
gukan(0)
or (if you want 100 always printed)
def gukan(count):
while count <= 100:
print(count)
count=count+3;
if count > 100:
count = 100
gukan(0)
Your count never equals the value 100 so your loop will continue until that is true
Replace your while clause with
def gukan(count):
while count < 100:
print(count)
count=count+3;
gukan(0)
and this will fix your problem, the program is executing correctly given the conditions you have given it.
x=1 while x<=100: print(x) x=x+3
Source: Stackoverflow.com