开发者

Error Handling Variables in a calculator program, Error handling numbers are fine

开发者 https://www.devze.com 2023-02-15 05:49 出处:网络
My problem is, as you can see by the title, error handling. Specifically with variables and symbols, those always seem to cause a NameError and shut down the program.

My problem is, as you can see by the title, error handling. Specifically with variables and symbols, those always seem to cause a NameError and shut down the program.

My, well experimented with calculator from a tutorial online, calculator program:

#Calculator Program
#this variable tells the loop whether it should loop or not.
# 1 means loop. anything else means don't loop.
loop = 1
#this variable holds the user's choice in the menu:
choice = 0
while loop == 1:

    #print what 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 you开发者_StackOverflowr option: ")
    if choice == 1:
        add1 = input("Add this: ")
        add2 = input ("to this: ")
        print add1, "+", add2, "=", add1 + add2
    elif choice == 2:
        sub2 = input("Subtract this: ")
        sub1 = input("from this: ")
        print sub1, "-", sub2, "=", sub1 - sub2
    elif choice == 3:
        mul1 = input("Multiply this: ")
        mul2 = input("with this: ")
        print mul1, "*", mul2, "=", mul1 * mul2
    elif choice == 4:
        div1 = input("Divide this: ")
        div2 = input("by this: ")
        print div1, "/", div2, "=", div1 / div2
    elif choice == a:
        print "Not a valid input, please reconsider."        
    elif choice == 5:
        loop = 0

    else:
        print "Not a valid input, please reconsider."
        print " "

print "Thank you for using calculator.py!"

The "else" and couple other things are my own.

any feedback would be helpful and future tips for these type of situations would be fantastic. For needing help on this brain stumper you can see i am a beginner.


My feedback as follows :

  1. print " " can be replaced with just print.
  2. elif choice == a does not work since a is not defined.
  3. Since all the operations take 2 operands, why not call them op1 and op2
  4. Use raw_input instead of input. input will eval the user input. For example, you can enter 1+2 for choice and it will be evaluated as 3.
  5. Handle blank input, EOFError and KeyboardInterrupt.
  6. Handle divide by zero exception.
  7. No need for loop variable. Just break on the exit condition.

Here's my vesion :

#!/usr/bin/python

def get_choice():
    print "your options are:"
    print

    print "1) Addition"
    print "2) Subtraction"
    print "3) Multiplication"
    print "4) Division"
    print "5) Quit calculator.py"
    print

    choice = raw_input("Choose your option: ")
    try:
        choice = int(choice)
    except ValueError:
        choice = 0
    return choice

def main():
    print "Welcome to calculator.py"
    while True:
        choice = get_choice()
        if choice == 5:
            return
        if choice == 0:
            print "Not a valid input, please reconsider."
            print
            continue
        try:
            op1 = int(raw_input("Enter first operand  : "))
            op2 = int(raw_input("Enter second operand : "))
        except ValueError:
            print "Invalid operand"
            continue
        if choice == 1:
            print op1, "+", op2, "=", op1 + op2
        elif choice == 2:
            print op1, "-", op2, "=", op1 - op2
        elif choice == 3:
            print op1, "*", op2, "=", op1 * op2
        elif choice == 4:
            if op2 == 0:
                print "Cannot divide by zero"
            else:
                print op1, "/", op2, "=", op1 / op2

if __name__ == '__main__':
    try:
        main()
    except (EOFError, KeyboardInterrupt):
        pass
    print
    print "Thank you for using calculator.py!"
0

精彩评论

暂无评论...
验证码 换一张
取 消