2014-09-15 1 views
-4

이 내가 쓴 간단한 계산기이지만 응용 프로그램 를 다시 시작하지 않습니다 마친 후이 내 코드입니다 : 그 나는 그들이 다시 시작 여부를하려는 경우 사용자를 물어보고 싶은 완료python에서 restart 명령을 추가하는 방법은 무엇입니까?

def add(x, y): 

return x + y 

def subtract(x, y): 

return x - y 

def multiply(x, y): 

return x * y 

def divide(x, y): 

return x/y 


print("Select from the list bellow which oporation you want the calculator to do.") 
print("A.Add") 
print("S.Subtract") 
print("M.Multiply") 
print("D.Divide") 

choice = input("Enter choice(a/s/m/d):") 
if choice != 'a' and choice != 's' and choice != 'm' and choice != 'd': 
    print (" the letter you intered is not in our lists!") 

num1 = int(input("Enter an interger as your first number: ")) 
num2 = int(input("Enter an integer as second number: ")) 
if choice == 'a': 
    print(num1,"+",num2,"=", add(num1,num2)) 

elif choice == 's': 
    print(num1,"-",num2,"=", subtract(num1,num2)) 

elif choice == 'm': 
    print(num1,"*",num2,"=", multiply(num1,num2)) 

elif choice == 'd': 
    print(num1,"/",num2,"=", divide(num1,num2)) 
else: 
    print("Invalid input") 
input("press enter to close") 

. 나는 작동하지 않는 루핑하면서 다른 사용.

+0

우리가 시도한 반복문을 보여 주시면 왜 작동하지 않았는지 이해하도록 도와 드리겠습니다. –

+1

들여 쓰기가 엉망입니다. – thebjorn

+1

* no *'while' 루프가 표시되므로 예상 루프가 작동하지 않는 이유를 알 수 없습니다. – chepner

답변

0

그냥 루프는 사용자가 그만두고 싶은 때까지

def main(): 
    print('Select from the list below which operation you want the calculator to do.') 
    print("A.Add") 
    print("S.Subtract") 
    print("M.Multiply") 
    print("D.Divide") 
    while True: 
     choice = input("Enter choice(a/s/m/d) or q to quit:") 
     if choice not in {"a", "s", "m", "d","q"}: 
      print (" the letter you entered is not in our lists!") 
      continue # if invalid input, ask for input again 
     elif choice == "q": 
      print("Goodbye.") 
      break 
     num1 = int(input("Enter an integer as your first number: ")) 
     num2 = int(input("Enter an integer as second number: ")) 
     if choice == 'a': 
      print("{} + {} = {}".format(num1, num2, add(num1, num2))) 
     elif choice == 's': 
      print("{} - {} = {}".format(num1, num2, subtract(num1, num2))) 

나는 당신의 출력을 인쇄 할 str.format을 사용 if choice not in {"a", "s", "m", "d","q"} 문 경우 긴 교체 membership를 테스트하는 in를 사용합니다.

사용자가 올바른 입력을 입력하지 않으면 프로그램이 손상되지 않도록 try/except 내부에 int 입력을 래핑 할 수 있습니다.

try: 
    num1 = int(input("Enter an interger as your first number: ")) 
    num2 = int(input("Enter an integer as second number: ")) 
except ValueError: 
    continue 

당신이 당신의 코멘트의 예처럼하고 싶은 경우이 대신

def main(): 
    print('Select from the list below which operation you want the calculator to do.') 
    print("A.Add") 
    print("S.Subtract") 
    print("M.Multiply") 
    print("D.Divide") 
    while True: 
     choice = raw_input("Enter choice(a/s/m/d)") 
     if choice not in {"a", "s", "m", "d","q"}: 
      print (" the letter you entered is not in our lists!") 
      continue 
     num1 = int(input("Enter an integer as your first number: ")) 
     num2 = int(input("Enter an integer as second number: ")) 
     if choice == 'a': 
      print("{} + {} = {}".format(num1, num2, add(num1, num2))) 
     elif choice == 's': 
      print("{} - {} = {}".format(num1, num2, subtract(num1, num2))) 
     inp = input("Enter 1 to play again or 2 to exit") 
     if inp == "1": 
      main() 
     else: 
      print("thanks for playing") 
      break 
-1

while 루프에서 사용자 입력을 처리하는 부분을 래핑해야합니다. 또한 선택 과정에서 루프를 중단하는 옵션이 필요합니다. 루프 종료를 처리하는 e의 입력 값을 추가했습니다. 끝에있는 첫 번째 if 문과 else 문은 중복되었으므로 약간 변경했습니다.

def add(x, y): 
    return x + y 

def subtract(x, y): 
    return x - y 

def multiply(x, y): 
    return x * y 

def divide(x, y): 
    return x/y 


while True: 
    print("Select from the list bellow which oporation you want the calculator to do.") 
    print("A.Add") 
    print("S.Subtract") 
    print("M.Multiply") 
    print("D.Divide") 
    print("E.Exit") 

    choice = input("Enter choice(a/s/m/d/e):") 
    if choice != 'a' and choice != 's' and choice != 'm' and choice != 'd' and choice != 'e': 
     print (" the letter you intered is not in our lists!") 
    else: 
     num1 = int(input("Enter an interger as your first number: ")) 
     num2 = int(input("Enter an integer as second number: ")) 
     if choice == 'a': 
      print(num1,"+",num2,"=", add(num1,num2)) 

     elif choice == 's': 
      print(num1,"-",num2,"=", subtract(num1,num2)) 

     elif choice == 'm': 
      print(num1,"*",num2,"=", multiply(num1,num2)) 

     elif choice == 'd': 
      print(num1,"/",num2,"=", divide(num1,num2)) 
     elif choice == 'e': 
      print("Goodbye") 
      break 
+0

'while true'는 유효한 구문이 아니며 사용자가 'e'를 누르면 코드가 종료되지 않습니다. –

0

:

if choice != 'a' and choice != 's' and choice != 'm' and choice != 'd' and choice != 'e': 
     print (" the letter you intered is not in our lists!") 
else: 
     num1 = int(input("Enter an interger as your first number: ")) 
     num2 = int(input("Enter an integer as second number: ")) 

사용이 :

if choice != 'a' and choice != 's' and choice != 'm' and choice != 'd' and choice != 'e': 
     print (" the letter you intered is not in our lists!") 
elif choice==e: 
     print("goodbye") 
     break 
else: 
     num1 = int(input("Enter an interger as your first number: ")) 
     num2 = int(input("Enter an integer as second number: ")) 

것은 삭제 :

elif choice == 'e': 
     print("Goodbye") 
     break