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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:23:25+00:00 2026-06-04T23:23:25+00:00

For a bit of python practice, I decided to work on a calculator tutorial.

  • 0

For a bit of python practice, I decided to work on a calculator tutorial. It was very basic, so I decided to give it exception handling in the event a user enters in garbage. While proper use of the program still works, punching in crap still causes it to crash, and entering
Here is my code:

loop = 1

choice = 0

while loop == 1:
    #print out the options you have
    print "Welcome to calculator.py"

    print "your options are:"

    print " "
    print "1) Addition"
    print "2) Subtraction"

    print "3) Multiplication"

    print "4) Division"
    print "5) Quit calculator.py"
    print " "

    choice = input("choose your option: ")
    try:
        if choice == 1:
            add1 = input("add this: ")
            add2= input("to this: ")
            print add1, "+", add2, "=", add1+ add2
        elif choice == 2:
            sub1 = input("Subtract this ")
            sub2 = input("from this")
            print sub1, "-", sub2, "=", sub1 - sub2
        elif choice == 3:
            mul1 = input("Multiply this: ")
            mul2 = input("with this: ")
            print mul1, "x", mul2, "=", mul1 * mul2
        elif choice == 4:
            div1 = input("Divide this: ")
            div2 = input("by this: ")
            if div2 == 0:
                print "Error! Cannot divide by zero!  You'll destroy the universe! ;)"
            else:

                print div1, "/", div2, "=", div1 * div2
        elif choice == 5:
            loop = 0
        else:
            print "%d is not valid input. Please enter 1, 2 ,3 ,4 or 5." % choice

    except ValueError:
        print "%r is not valid input.  Please enter 1, 2, 3, 4 or 5." % choice
    print "Thank you for using calculator.py!"

Now while I found an useable answer here: Error Handling Variables in a calculator program, Error handling numbers are fine

I was wondering why my code didn’t work. Does python want the exception handling in a function? That’s the vibe I’m getting from it.

  • 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-04T23:23:28+00:00Added an answer on June 4, 2026 at 11:23 pm

    In Python 2 (which is what you are using) input evaluates as Python code whatever the user enters. Because of this input can raise many different exceptions, but rarely a ValueError.

    Better would be to accept your input with the raw_input which returns a string, and then cast to the expected type. If the input is invalid it will then raise a ValueError:

    >>> x = int(raw_input("enter something: "))
    enter something: sdjf
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: 'sdjf'
    

    Note: In Python 3 input assumes the semantics of Python 2’s raw_input and raw_input goes away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been mucking around a bit with Python, and I've gathered that it's usually
I have been learning a bit of Python 2 and Python 3 and it
I'm working on my first object oriented bit of python and I have the
How to convert the following hex string to float (single precision 32-bit) in Python?
How can we find the system type (i.e. 32-bit or 64-bit) using Python...?
I'm a bit surprised by Python's extensive use of 'magic methods'. For example, in
I am a bit new to Python, but an experienced programmer. I am writing
I am running 32-bit Windows 7 and Python 2.7. I am trying to write
In Python, I can run a bit of code optionally if a given package
I'm having a bit of difficulty understanding how the python __init__ ( ) function

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.