Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8006603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:34:01+00:00 2026-06-04T17:34:01+00:00

Just a quick question because I really can’t find a simple solution to my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:34:03+00:00Added an answer on June 4, 2026 at 5:34 pm

    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"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick question, because I can't seem to find anything. Is there any way to
Just a quick question, does anyone know if there are restrictions on creating Performance
just a quick question. I'm having a problem with divs with onclick javascript within
Just a quick question. Is there any performance difference between using PDO::fetchAll() and PDO::fetch()
A solution to get quick exposure: Since my website just got lunched 3 days,
Is there any way that I can find the container pointed to by an
Just a quick question about ReadWriteLocks in Java (specifically the ReentrantReadWriteLock implementation) as I
Just a quick question. Say a call a method like so mysql_pconnect(server,tator_w,password) or die(Unable
Just a quick question about how parameters are passed in Java... ... if ((index
Just a quick question about best practice in MVC development. Let's say that I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.