Jertise
Jertise

Reputation: 35

Input strings and integers

Just a quick question because I really can't find a simple solution to my problem. Is there a way to get a user input that is meant to be a integer, but when a string is entered the program will not break and instead displays "Error"

I've been trying to work around it by converting strings to integers and vice-versa, but I constantly get "invalid literal for int() with base 10" error, or when it displays "Error" it does so in an infinite loop.

Here's my code just to help clear the question

choice = input("Enter your choice: ")

while choice != 3:
    if choice == 1:
        get_songs()
        print
        main()
    elif choice == 2:
        read_songs()
        print
        main()
    else:
        print "Invalid choice"

So essentially I want the else operation to work for strings as well as for an integer that is greater than 3 or less than 1.

Upvotes: 0

Views: 38816

Answers (4)

jpaugh
jpaugh

Reputation: 7035

Here's something: I dislike making the user enter "" around the strings.

ch = raw_input("Enter choice: ")
#ch *is* a string at this point
if ch.isdigit():
   choice = int(ch)
else:
    print "Invalid choice"

Edit (from comments):

isdigit may not handle locale encoding correctly. In Python 3, you can use isdecimal instead.  — J.F. Sebastian

Upvotes: 0

Karl Knechtel
Karl Knechtel

Reputation: 61643

but I constantly get "invalid literal for int() with base 10" error

You get an exception, specifically a ValueError. You can catch the exception using an except block. For more info, refer to whatever language tutorial you've been using thus far, or try Google for except block Python.

when it displays "Error" it does so in an infinite loop.

When you detect that the input isn't correct, you need to get new input before you try the loop again. Put the input-getting stuff inside your loop. Do not use recursion (calling main() from within main) in addition to the loop; you're only going to confuse yourself this way. Because you don't get a value for choice until you're inside the loop, it's easier to explicitly break out of the loop when you find the appropriate choice value, instead of trying to control the loop with it (i.e. testing for it in the while condition).

We can also use continue to simplify the loop structure: instead of doing all the work in the try block, we limit that to the part where we extract a number. We use continue in the except block to skip the rest of the loop when we don't have an actual number, and only do the rest of the loop when we do. (After all, maybe the code we call for choice == 1 or choice == 2 could raise ValueError for some totally different reason, and we'd want to do something different about that.)

while True:
    try:
        choice = int(raw_input("Give me a number"))
    except ValueError:
        print "Could you at least give me an actual number?"
        continue

    if choice == 1:
        do_something()
    elif choice == 2:
        do_something_else()
    elif choice == 3:
        break
    else:
        print "Try a different number"

Upvotes: 9

Joel Cornett
Joel Cornett

Reputation: 24788

In your code, the while loop should encapsulate the input() line. The following is a clearer alternative, however:

Create a function which gets user input:

def getInteger(prompt):
    while True:
        userIn = input(prompt)
        try:
            return int(userIn)
        except ValueError:
            print "Error"

Upvotes: 0

waitingkuo
waitingkuo

Reputation: 93964

Since that choice always != 3, you will get an infinite loop. You should get input again if it jumps into the "else" condition.

Upvotes: 0

Related Questions