0
try:
     num=float(num)
except:
     print "Invalid input"
     continue

this part of my code seems to be bugging, but when i remove the try and except everything works smoothly, so this seems to be the problem.

i want to convert the input inside a while loop to an integer, if the input isn't an integer it'll display an error and just continue with the loop and ask again. however, it doesn't continue the loop and just keeps printing "Invalid input" forever. how come it isn't continuing the loop?

here is the entire code, in case something else might be wrong:

c=0
num2=0
num=raw_input("Enter a number.")
while num!=str("done"):
     try:
          num=float(num)
     except:
          print "Invalid input"
          continue
     c=c+1
     num2=num+num2      
     num=raw_input("Enter a number.")
avg=num2/c
print num2, "\t", c, "\t", avg
2
  • 1
    continue means "skip the rest of this iteration of the loop". It doesn't mean "keep going"; code keeps going automatically. Commented Nov 1, 2016 at 22:52
  • It would help if you described what should happen when an invalid input is given. Should the whole program just exit? Is it supposed to wait for the next input? Commented Nov 1, 2016 at 22:58

3 Answers 3

3

You can solve the problem by moving the variable assignments into the try block. That way, the stuff you want to skip is automatically avoided when an exception is raised. Now there is no reason to continue and the next prompt will be displayed.

c=0
num2=0
num=raw_input("Enter a number.")
while num!=str("done"):
     try:
          num=float(num)
          c=c+1
          num2=num+num2      
     except:
          print "Invalid input"
     num=raw_input("Enter a number.")
avg=num2/c
print num2, "\t", c, "\t", avg

You can tighten this a bit further by removing the need to duplicate the prompt

c=0
num2=0
while True:
    num=raw_input("Enter a number. ")
    if num == "done":
        break
    try:
        num2+=float(num)
        c=c+1
    except:
        print "Invalid input"
avg=num2/c
print num2, "\t", c, "\t", avg
Sign up to request clarification or add additional context in comments.

Comments

1

continue means return back to while, and as num never changes, you will be stuck in an infinite loop.

If you want to escape the loop when that exception occurs then use the term break instead.

1 Comment

Sorry about earlier, now you're above 50. Have a nice day.
0
# this function will not stop untill no exception trown in the try block , it will stop when no exception thrown and return the value 
def get() : 
    i = 0 
    while (i == 0 ) : 
        try:
            print("enter a digit str : ") 
            a = raw_input()
            d = int(a)
        except:
            print 'Some error.... :( '
        else:
            print 'Everything OK'
            i = 1 
    return d 
print(get())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.