0

i = 1
while i < 10000 and i > 0 and 1 :
print " still going ..."
i = 2 * i

Hi i am new to Python and completley useless at it. Can somebody please explain to me why this code prints "Still going" 14 times. I have tried to work it out but i just can't see how. Any help would be very much appreciated. Thanks

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by 15uck
0

You don't need the and 1 at the end of the while line, and you should indent anything that's within a loop.
You can see things more clearly if you print the variables out in each step:

i = 1
while i < 10000 and i > 0 :
    print " still going ...",i
    i = 2 * i

Edited by lagspike: n/a

0

Because you are multiplying i by 2 each time it comes around :-O

I think what you need is

i += 1

instead :P


edit: if you run the code lagspike gave you its clear to see! always print out strings that are giving you trouble to see the problem!

Edited by 15uck: : D smiley came out as LOL =/

0

Thank you both. It makes perfect sense. I'm sorry if the question and post seemed a bit thick lol. But thank you for your help

0

Thank you both. It makes perfect sense. I'm sorry if the question and post seemed a bit thick lol. But thank you for your help

its not thick at all :twisted: everyone has to start somewhere :cool:

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.