Posts: 4
Threads: 2
Joined: May 2017
Hello all,
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  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
Posts: 12,039
Threads: 487
Joined: Sep 2016
Please fix indentation and re-post
Posts: 4
Threads: 2
Joined: May 2017
Hi Larzo
The indentation seems to work OK in idle
kind regards
al
Posts: 8,167
Threads: 160
Joined: Sep 2016
May-01-2017, 08:41 PM
(This post was last modified: May-01-2017, 08:49 PM by buran.)
(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.
Posts: 1,298
Threads: 38
Joined: Sep 2016
May-01-2017, 09:01 PM
(This post was last modified: May-01-2017, 09:01 PM by sparkz_alot.)
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
Posts: 1,298
Threads: 38
Joined: Sep 2016
dang @ buran, beat me to it
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
Posts: 4
Threads: 2
Joined: May 2017
Hi all,
Great! thanks a lot for the replies,as a result I ended up with the following code that workd fine
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
Posts: 1,298
Threads: 38
Joined: Sep 2016
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
Posts: 3,458
Threads: 101
Joined: Sep 2016
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.
|