while True:
    try:
        wd = int(input('How wide would you like your letter to be? (5 - 20)' + ': '))
        break
    except ValueError:
        print ("Oops!  That was no valid number.  Try again...")

If the user inserts a number that is not 5 - 20, I want the program to shut down, this value error only restarts the program if the user enters a letter or symbol.... how can i fix this?
thanks!


If the user inserts a number that is not 5 - 20, I want the program to shut down, this value error only restarts the program if the user enters a letter or symbol.... how can i fix this?
thanks!

Like this?

Although if the user inputs something like "30", this code itself will still go with it, so you might want to add code to do something about that.

while True:
    try:
        wd = int(input('How wide would you like your letter to be? (5 - 20)' + ': '))
        break
    except ValueError:
        print ("Oops!  That was no valid number.  Try again...")
        break

to do that, you can write something like,
(Python 3 syntax)

while True:
    try:
        wd = int(input('How wide would you like your letter to be? (5 - 20): '))
        if not (5 <= wd <= 20):
            raise Exception("Number is not within 5 - 20 range.")
        break
    except ValueError:
        print("Oops!  That was no valid number.  Try again...")
        break
    except Exception as e:
        print(e)
        break
#if you don't want the program to shutdown every time
#the user inputs something wrong, then
#remove some of the breaks

Here's python 2 code:

while True:
    try:
        wd = int(input('How wide would you like your letter to be? (5 - 20): '))
        if not (5 <= wd <= 20):
            raise Exception("Number is not within 5 - 20 range.")
        break
    except NameError,ValueError:
        print "Oops!  That was no valid number.  Try again..."
        break
    except Exception, e:
        print e
        break
#if you don't want the program to shutdown every time
#the user inputs something wrong, then
#remove some of the breaks

Edited 6 Years Ago by jcao219: n/a

One more way.

while True:
    try:
        wd = int(input('How wide would you like your letter to be? (5-20)' + ': '))
        if wd <=20 and wd >= 5:
            print ('You have answered: %s' % wd)
            break
        else:
             print('please enter a value between(5-20)')
    except (ValueError,NameError,SyntaxError):
        print ("Oops! That was no valid number. Try again...")

OK, let's enter this beauty contest ;) I am working still in python2, so sorry to you python3 users!

## My style of coding
def letter_width(a,b):
    wd=-1 ## initial value which is not in value range

    try:
        ## Taking care of value range we want "while with purpose"
        while not (a <= wd <= b):
            wd = int(raw_input('How wide would you like your letter to be? (%i - %i): ' % (a,b)))
        else: return wd
    except ValueError:
        print("Oops!  That was no valid number.  Try again...")
        return letter_width(a,b)  
    except Exception as e:
        print(e)

wd=letter_width(5,20)
print "Letter width is set to %i" % wd

Also the previous code does not work well for non-numeric values as it uses input with it's eval functionality and gives confusing error (but maybe difference between python2 and python3 input functions?):

How wide would you like your letter to be? (5 - 20): u
name 'u' is not defined

Edited 6 Years Ago by pyTony: n/a

This question has already been answered. Start a new discussion instead.