Python Forum
loops goes on forever - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: loops goes on forever (/thread-3145.html)



loops goes on forever - alsimm - May-01-2017

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



RE: loops goes on forever - Larz60+ - May-01-2017

Please fix indentation and re-post


RE: loops goes on forever - alsimm - May-01-2017

Hi Larzo Smile

The indentation seems to work OK in idle
kind regards
al


RE: loops goes on forever - buran - May-01-2017

(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.


RE: loops goes on forever - sparkz_alot - May-01-2017

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.


RE: loops goes on forever - sparkz_alot - May-01-2017

dang @buran, beat me to it  Smile


RE: loops goes on forever - alsimm - May-02-2017

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


RE: loops goes on forever - sparkz_alot - May-02-2017

A better way would be:

green1=int(raw_input ('enter a number'))
rather than in your call to "light()"


RE: loops goes on forever - nilamo - May-02-2017

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.