this error keeps plaguing me
with the code:

option = input('What would you like to do?  ')
        if option == 1:
            newExpence = addExpence(totalBudget)
            totalBudget = totalBudget - newExpence
        elif option == 2:
            removedExpence = removeExpence(totalBudget)
            totalBudget = totalBudget + removedExpence
        elif option == 3:
            newRevenue = getNewRevenue()
            totalBudget = totalBudget + newRevenue
        elif option == 4:
            removeRevenue = doremoveRevenue(totalBudget)
            totalBudget = totalBudget - removeRevenue
        elif option == 5:
            print "Thank you for using our program"
        else:
            print "Input not recognized, please try again"

if my user accidentally hits say "r" my whole program crashes instead of printing "Input not recognized, please try again" nowhere in my program are letters acceptable, how can i limit user input to only using decimal numbers?

Recommended Answers

All 2 Replies

i read that using

option = raw_input('What would you like to do?  ')

would be a better option. i tried it and inputting "r" does not crash the program, but inputting 1,2,3,4 or 5 also return "Input not recognized, please try again"

Like this is it can be done without exception handling.
Here it will loop until user enter a valid choice or quit.
User can type in everthing and it will not break out in a error.
Remeber raw_input() return a string for everthing that user input.

print 'Enter a choice | Q to quit'
while True:
    option = raw_input('What would you like to do?  ').lower()
    if option == '1':
        print '1 is you choice'
    elif option == '2':
        print '2 is you choice'
    elif option == 'q':
        break
    else:
        print 'Not a correct choose: %s,try again' % option

One to show exception handling,use int(raw_input()) is always better than input() for python 2.x
Python 3.x has only input(),that work in the same was as raw_input() in python 2.x
Try type in a letter here an it will not break out out in a error,we catch the error and give user a message.

try:
    option = int(raw_input('What would you like to do?  '))
except ValueError:
    print 'Please only numbers,try again'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.