Python Forum

Full Version: loops goes on forever
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all, Smile
python 2.7
The following code uses input to get 3 numbers then adds them up.I then placed it into a while loop
thinking that it would repeat the process three times,but it doesn't stop at three and just keeps going
on and on,as if it was in an infinite loop Sad if I remove the while loop it works once but fine.Any help much appreciated(very new to Python)


ding =0
while ding <3:

def light(green1,green2,green3):
    return green1+green2+green3
green1=input ('enter a number')
green2= input ('enter another number')
green3 = input ('now enter a third number')
print light(green1,green2,green3)       

ding +=1
Please fix indentation and re-post
Hi Larzo Smile

The indentation seems to work OK in idle
kind regards
al
(May-01-2017, 08:27 PM)alsimm Wrote: [ -> ]The indentation seems to work OK in idle
As it is in your post, you will get Indentation error. Indented block is expected after the while ding <3: line (i.e. line 2)

Given the description of your problem I would guess that your code actually looks like this:

ding =0
while ding <3:

    def light(green1,green2,green3):
        return green1+green2+green3
    green1=input ('enter a number')
    green2= input ('enter another number')
    green3 = input ('now enter a third number')
    print light(green1,green2,green3)

ding +=1
and it goes forever, because last line is actually out of the loop. so you need to indent it one level, so that it's within the loop, changing the value of ding.
In addition - you dont need to define the light function in the loop (i.e. each time you iterate in the loop, you define the def again. Also, in python 2.7 you should not use input, but raw_input instead and then convert the user input from str to int. in python 2 input will evaluate/execute the user input as regular python code and that's dangerous.
If that is the case, then your "while" statement does nothing, and in fact should generate an error.
You will get an error that "light" is undefined.
Also, (and I don't use Python 2.x) but I believe  "input" should be "raw_input"
Keep in mind that "input" and "raw_input" store the value as a string, so at some point you need to convert to int or float.

As a thought for you to ponder, it's not a good idea to shadow variables as it create unforeseen problems. In this case green1, green2 and green3.  You should consider changing either the variables outside the "def" or in the "def" to something different, ie grn or grean.
dang @buran, beat me to it  Smile
Hi all,
Great! thanks a lot for the replies,as a result I ended up with the following code that workd fine Smile

ding =0
while ding <3:
 
    def light(green1,green2,green3):
        return green1+green2+green3
    green1=raw_input ('enter a number')
    green2= raw_input ('enter another number')
    green3 = raw_input ('now enter a third number')
    ding +=1
    print light(int(green1),int(green2),int(green3))
 
kind regards
al
A better way would be:

green1=int(raw_input ('enter a number'))
rather than in your call to "light()"
You should define your function outside of the while loop. It's just cluttered up there, and there's no reason to redefine it every iteration.