Search code examples
pythonstringinputintegerraw-input

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.


Solution

  • 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"