2011-03-08 28 views
0

我的問題是,正如你可以看到的標題,錯誤處理。特別是對於變量和符號,這些似乎總是導致NameError並關閉程序。錯誤處理計算器程序中的變量,處理錯誤的數字是好的

我,以及與教程計算器嘗試網上,計算器程序:

#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 your 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!" 

的「其他人」和一些其他的東西都是我自己的。

任何反饋將是有幫助的,這些類型的情況的未來技巧將是太棒了。對於需要幫助這個腦子的東西,你可以看到我是一個初學者。

回答

0

我的反饋如下:

  1. print " "可以只用print所取代。
  2. elif choice == a不起作用,因爲a未定義。
  3. 由於所有的操作需要2個操作數,爲什麼不叫他們op1op2
  4. 使用raw_input而不是inputinputeval用戶輸入。例如,您可以輸入1+2進行選擇,它將被評估爲3.
  5. 處理空白輸入,EOFError和KeyboardInterrupt。
  6. 處理除零異常。
  7. 不需要loop變量。只是在退出條件下休息。

這裏是我的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!"