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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:21:51+00:00 2026-06-17T12:21:51+00:00

Here is the code for a small program I just wrote to test some

  • 0

Here is the code for a small program I just wrote to test some new things I learned.

while 1:
    try:
        a = input("How old are you? ")
    except:
        print "Your answer must be a number!"
        continue

    years_100 = 100 - a
    years_100 = str(years_100)
    a = str(a)
    print "You said you were "+a+", so that means you still have "+years_100+" years"
    print "to go until you are 100!"
    break
while 2:
    try:
        b = str(raw_input('Do you want to do it again? If yes enter "yes", otherwise type "no" to stop the script.'))
    except:
        print 'Please try again. Enter "yes" to do it again, or "no" to stop.'
        continue

    if b == "yes":
            print 'You entered "yes". Script will now restart... '
    elif b == "no":
            print 'You entered "no". Script will now stop.' 
            break

It works fine for the for loop. If you type something other than a number, it will tell you only numbers are allowed.

However, in the 2nd loop, it asks you to enter yes or no, but if you enter in something different, it just restarts the loop instead of printing the message after

except:

What did I do wrong and how do I fix it so it displays the message I told it to?

  • 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-17T12:21:51+00:00Added an answer on June 17, 2026 at 12:21 pm

    You do not get an exception, because you are always enter a string when using raw_input(). Thus str() on the return value of raw_input() will never fail.

    Instead, add an else statement to your yes or no tests:

    if b == "yes":
            print 'You entered "yes". Script will now restart... '
    elif b == "no":
            print 'You entered "no". Script will now stop.' 
            break
    else:
        print 'Please try again. Enter "yes" to do it again, or "no" to stop.'
        continue
    

    Note that you should never use a blanket except statement; catch specific exceptions. Otherwise, you’ll mask unrelated problems, making it harder for you to find those problems.

    Your first except handler should only catch NameError, EOFError and SyntaxError for example:

    try:
        a = input("How old are you? ")
    except (NameError, SyntaxError, EOFError):
        print "Your answer must be a number!"
        continue
    

    as that’s what input() would throw.

    Also note that input() takes any python expression. If I enter "Hello program" (with the quotes), no exception would be raised, but it is not a number either. Use int(raw_input()) instead, and then catch ValueError (what would be thrown if you entered anything that’s not an integer) and EOFError for raw_input:

    try:
        a = int(raw_input("How old are you? "))
    except (ValueError, EOFError):
        print "Your answer must be a number!"
        continue
    

    To use the second loop to control the first, make it a function that returns True or False:

    def yes_or_no():
        while True:
            try:
                cont = raw_input('Do you want to do it again? If yes enter "yes", otherwise type "no" to stop the script.'))
            except EOFError:
                cont = ''  # not yes and not no, so it'll loop again.
            cont = cont.strip().lower()  # remove whitespace and make it lowercase
            if cont == 'yes':
                print 'You entered "yes". Script will now restart... '
                return True
            if cont == 'no':
                print 'You entered "no". Script will now stop.' 
                return False
            print 'Please try again. Enter "yes" to do it again, or "no" to stop.'
    

    and in the other loop:

    while True:
        # ask for a number, etc.
    
        if not yes_or_no():
            break  # False was returned from yes_or_no
        # True was returned, we continue the loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wrote a small program which takes the input from user and shows
here is a small peice off code that i wrote. But I am not
This is a pretty small program, so I just used the autogenerated code for
I wrote this small code just to see how an iterator actually gets invalidated
I have a small program I am working on. I Just put a try
I have a small code snippet for deleting element in linked list. Here is
Here's my code, I know there's a small mistake somewhere but being a noob
Here a small piece of code for testing and explain the problem. I have
Here is code from MSDN . I don't understand why the work isn't just
I wrote a small CLI program to delete specific Safari cookies for me. Functionally

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.