Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Infinite loop not working
#1
Hello, I am trying to get this loop to work in which a user inputs several numbers and you get the total and the mean of these numbers.

The program asks the user to input a number, but when I type in 'done', which should break the loop, I get an error. Or whenever I type in anything that isn't a number.

the code is below:


[Python]

num=0
tot=0.0

while True:
sval = input ('Enter a number: ')
if sval == 'done':
break

try:
fval=float(sval)
except:
print 'Not a number, try again'
continue


num=num+1
tot=tot+fval

print(tot,num,tot/num)

[Python]


the error I get is this:


Enter a number: 56
Enter a number: 56
Enter a number: 56
Enter a number: done
Traceback (most recent call last):
File "Ex5.5.py", line 5, in <module>
sval = input ('Enter a number: ')
File "<string>", line 1, in <module>
NameError: name 'done' is not defined
Reply
#2
Your second python tag should look like this: [/python]
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
The error makes me think you are using Python 2.*. Before Python 3 the input method evaluated the string. Like a combination of fval = float(input('Enter a number')). When you typed "done" it tried to evaluate this too, searching for "done" in the current namespace and not finding it.

To make your program work you need to use raw_input instead of input. You will also have a problem with the print at the bottom of the program. In Python 2.* print was not a function, no parenthesis.

You should update to Python 3.*
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  while loop not working-I am using sublime text editor mma_python 4 1,147 Feb-05-2023, 06:26 PM
Last Post: deanhystad
Shocked Why this code does not go into an infinite loop? 2367409125 2 887 Dec-02-2022, 08:22 PM
Last Post: deanhystad
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 1,176 Aug-22-2022, 02:57 AM
Last Post: deanhystad
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,494 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Infinite loop problem Zirconyl 5 2,998 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  using 'while loop' output going into infinite loop... amitkb 2 1,971 Oct-05-2020, 09:18 PM
Last Post: micseydel
  Why is the while loop not working? mcoliver88 5 3,125 Aug-18-2020, 03:27 PM
Last Post: deanhystad
  Loop not working Nonameface 8 2,943 Jul-19-2020, 12:27 PM
Last Post: snippsat
  for loop script over telnet in Python 3.5 is not working abhijithd123 1 2,902 May-10-2020, 03:22 AM
Last Post: bowlofred
  How to keep a loop containing a web import working in python executable? coder1384 3 2,888 Feb-22-2020, 06:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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