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 7689421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:09:45+00:00 2026-05-31T20:09:45+00:00

When I enter a letter for one of the numbers I get an error:

  • 0

When I enter a letter for one of the numbers I get an error:

    Traceback (most recent call last):
      File "/Users/rodchar/Documents/Hello World Katie/testing.py", line 7, in <module>
        x = input("What is your first number?")
      File "<string>", line 1, in <module>
    NameError: name 's' is not defined



x = 0
y = 0

isValid = False

while isValid == False:
    x = input("What is your first number?")

    try:
        float(x)
        isValid = True
    except:
        isValid = False

    y = input("What is your second number?")

    try:
        float(y)
        isValid = True
    except:
        isValid = False


print "The answer is: %s" % (x+y)
  • 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-05-31T20:09:46+00:00Added an answer on May 31, 2026 at 8:09 pm

    Firstly, at least in Python 2.7, input() is not what you want for getting user input. You actually want raw_input(). (This is, indeed, confusing, and has been fixed in Python 3.)

    Secondly, as @jamylak said, the statement giving the exception is not inside a try/catch block.

    Thirdly, when you catch exceptions using except:, you should really be catching a specific type of exception (except ValueError:) instead of any and all exceptions. Catching all exceptions is bad because it masks bugs that raise exceptions you didn’t anticipate.


    A better way to write this would be:

    • Apply the “Don’t Repeat Yourself” principle (DRY) – the code for getting x and getting y from the user is very similar. Instead of writing the same thing twice, write it once and reuse it.
    • Wrap the “get a float from the user” functionality up in a named function.
    • Catch only the specific type of exception that you are prepared to handle.

    Tested on Python 2.7:

    def get_float_from_user (prompt):
        while True:
            try:
                user_input = float(raw_input(prompt))
                return user_input
            except ValueError:
                print ("Not a floating point number. Please try again.")
    
    x = get_float_from_user("Enter x.")
    y = get_float_from_user("Enter y.")
    
    print (x+y)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We would like to mimic taking user input one letter at a time (intended
I have a text box in which when I type one letter say 's'
I have one textbox basically the user is going to enter in a 9
I want to make users to enter this pattern in textboxes in asp.net mvc3:
I've got an application which i want users to be able to enter alphanumeric
I have a JTextField for users to enter a serial number. The form of
I have an activity with one EditText where I need to input numbers only.
OK Regex is one of the most confusing things to me. I'm trying to
I'm trying to put together a small web app where users enter in the
I have a small form where users enter a deposit in a dollar amount.

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.