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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:18:59+00:00 2026-06-08T02:18:59+00:00

I’m experimenting with exception handling / error trapping and was wondering why the below

  • 0

I’m experimenting with exception handling / error trapping and was wondering why the below code doesn’t work. I’m using python 2.7. I understand the difference between input() and raw_input() and understand that raw_input() has been renamed to input() in Python 3.0. If I enter an integer then the code keeps looping until I enter a string. I get the below error message when entering a string. Is there a way around this or is this just one of those python quirks?

  File "<some_directory_path_goes_here>", line 30, in <module>
    enterAge = input('Enter your age as an integer: ')
  File "<string>", line 1, in <module>
NameError: name '<user_entered_string_goes_here>' is not defined

In python 2.7 it would seem to me that the code should still work.

from types import IntType
age = 0
while True:
    enterAge = input('Enter your age as an integer: ')
    try:
        if type(enterAge) is IntType:
            num = enterAge
            age = age + num
            print str(age) + ' is old!'

    except TypeError:
        print 'You did\'t enter an integer'
        break
  • 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-08T02:19:01+00:00Added an answer on June 8, 2026 at 2:19 am

    The idea behind try–except is that you don’t check all the necessary conditions beforehand. In your case you don’t need to check for types inside try. Because of the if statement the exception will not be raised when it should.

    Also, you should definitely use raw_input() on Python 2 and keep in mind that it always returns a str. What can differ is the result of int(enterAge):

    In [1]: int('4')
    Out[1]: 4
    
    In [2]: int('4f')
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    /home/lev/<ipython-input-2-523c771e7a8e> in <module>()
    ----> 1 int('4f')
    
    ValueError: invalid literal for int() with base 10: '4f'
    

    This is what you need to try in the try block and catch the ValueError.

    Edit: Apparently I need to clarify the answer a little, so I’ll show how in my opinion the code should look:

    age = 0
    while True:
        enterAge = raw_input('Enter your age as an integer: ')
        try:
            age += int(enterAge)
            print age, 'is old!'
    
        except ValueError:
            print "You didn't enter an integer"
            break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.