2014-11-20 73 views
1

這裏是一些代碼,不會由於不正確的statments「ELIF」被認爲是無效的語法

#Calculator 
print("This is a program that can be used as a calculator") 
print("Press 1 to add two numbers") 
print("Press 2 to subtract two numbers") 
print("Press 3 to divide two numbers") 
print("Press 4 to multiply two numbers") 

while True: 
    Selection=input("What do you want to choice") 
    if Selection in("1"): 
     add1=input("Input your first number") 
     add2=input("Input your second number") 
     resultAdd= int(add1+add2) 
     print ("Your result is ", resultAdd) 

     elif Selection in("2"): 
      subtract1= input("Input your first number") 
      subtract2= input("Input your second number") 
      resultSubtract= int(subtract1-subtract2) 
      print ("Your result is ", resultSubtract) 

      elif Selection in("3"): 
      divide1= input("Input your first number") 
      divide2= input("Input your second number") 
      resultDivide= int(divide1/divide2) 
      print ("Your result is ",resultDivide) 

      elif Selection in("4"): 
       multiply1=input("Input your first number") 
       multiply2=input("Input your second number") 
       resultMultiply= int (multiply1*multiply2) 
       print("Your result is ", resultMultilpy) 

       break 
      else: 
       print("Invalid respnse") 

我可能失去了一些東西很簡單,但也只是想知道我是什麼樣的工作做錯了

回答

2

這是你的對齊方式:你的elif不符合起始if

#Calculator 
print("This is a program that can be used as a calculator") 
print("Press 1 to add two numbers") 
print("Press 2 to subtract two numbers") 
print("Press 3 to divide two numbers") 
print("Press 4 to multiply two numbers") 

while True: 
    Selection=input("What do you want to choice") 
    if Selection in("1"): 
     add1=input("Input your first number") 
     add2=input("Input your second number") 
     resultAdd= int(add1+add2) 
     print ("Your result is ", resultAdd) 

    elif Selection in("2"): 
     subtract1= input("Input your first number") 
     subtract2= input("Input your second number") 
     resultSubtract= int(subtract1-subtract2) 
     print ("Your result is ", resultSubtract) 

    elif Selection in("3"): 
     divide1= input("Input your first number") 
     divide2= input("Input your second number") 
     resultDivide= int(divide1/divide2) 
     print ("Your result is ",resultDivide) 

    elif Selection in("4"): 
     multiply1=input("Input your first number") 
     multiply2=input("Input your second number") 
     resultMultiply= int (multiply1*multiply2) 
     print("Your result is ", resultMultilpy) 

     break 
    else: 
     print("Invalid respnse") 
+0

感謝您的幫助 – Lenard 2014-11-20 20:54:40