[Tutor] (no subject)

Dave Angel davea at davea.name
Mon Apr 15 04:44:08 CEST 2013


On 04/14/2013 08:35 PM, Soliman, Yasmin wrote:
> Hi everyone. I just need to know why this programs tells me End is not defined, what can I o to fix this issue? Thanks in advance.
>
> hrList=[]
> while True:
>      heartrate= float(input('Enter heart rate as beats per min: '))
>      hrList.append(heartrate)
>
>      if heartrate=='End':
>          print '\nThank you for using this program! Bye.'
>          break
>
>      total_sum = 0;
>      length = len(hrList)
>
>      for i in range(0, length):
>          total_sum += hrList[i]
>
>      average = total_sum / length
>
>      print average
>


I can't see any place there where such an error might occur.  But I can 
see where you forgot the parens in the call to print().

You might get better advice by telling us the Python version, and the 
complete error message traceback.  That'll show us exactly what was 
executing when the exception happened.

Oh wait.  Perhaps you were running Python 2.x, rather than 3.x.  In that 
case, you should have used raw_input(), not input.  Input will eval 
whatever the user types, and if he types "End" he'll get an error:

Enter heart rate as beats per min: End
Traceback (most recent call last):
   File "soliman2.py", line 6, in <module>
     heartrate= float(input('Enter heart rate as beats per min: '))
   File "<string>", line 1, in <module>
NameError: name 'End' is not defined

Once you fix that, you'll get another error.

Enter heart rate as beats per min: End
Traceback (most recent call last):
   File "soliman2.py", line 6, in <module>
     heartrate= float(raw_input('Enter heart rate as beats per min: '))
ValueError: could not convert string to float: End

That's because you try to convert the string "End" into an integer 
before you check it for the terminating value.  You want to do the 
raw_input first, then the check for End, then convert to int, then 
append and print the stuff.


Alternatively, you could just tell the user to enter "0" when he wants 
to quit.





-- 
DaveA


More information about the Tutor mailing list