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

Recommended Answers

All 4 Replies

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

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!

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

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.