Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loops goes on forever
#1
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
Reply
#2
Please fix indentation and re-post
Reply
#3
Hi Larzo Smile

The indentation seems to work OK in idle
kind regards
al
Reply
#4
(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.
Reply
#5
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
dang @buran, beat me to it  Smile
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
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
Reply
#8
A better way would be:

green1=int(raw_input ('enter a number'))
rather than in your call to "light()"
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#9
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  generating random string unique forever Skaperen 5 2,324 Aug-20-2021, 07:15 AM
Last Post: Gribouillis
  Increase Numbers forever and need reset? ATARI_LIVE 4 2,320 Oct-23-2020, 01:55 PM
Last Post: ATARI_LIVE
  python multiprocessing import Pool, cpu_count: causes forever loop | help to remove Hassibayub 0 1,853 Jun-18-2020, 05:27 PM
Last Post: Hassibayub

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020