2017-10-14 12 views
1

저는 Python3 코딩의 초보자입니다. 여기에 문제가 있습니다. 14 번 줄에서는 "n"을 "다시 시도 하시겠습니까?"라고 대답하는 부분에 "Thank you! Goodbye"라고 인쇄하여이 프로그램을 끝내려고했습니다. 그러나 그 아래에 "중단"을 삽입 했더라도 처음부터 다시 시작할 것이라고 밝혀졌습니다. 이제 제가 풀 수있는 유일한 해결책은 전체 프로그램을 sys.exit(0)으로 끝내는 것입니다. 그러나 전체 프로그램을 종료하기 때문에 이상적인 솔루션이라고 생각하지 않습니다.프로그램 (Python)을 종료하는 방법은 무엇입니까?

import sys 
while True: 

x=int(input("Enter the coins you expected=")) 
f=int(input("Enter first coin=")) 

while f!=1 and f!=5 and f!=10 and f!=25: 
    print("invalid number") 
    f=int(input("Enter first coin=")) 

if x>f: 
    while x>f: 
     n=input("Enter next coin=") 
     if not n: 
      print("Sorry-you only entered",f,"cents") 
      again=input("Try again (y/n)?=") 
      if again=="y": 
       True 
      elif again=="n": 
       print("Thank you, goodbye!") 
       sys.exit(0) 
      break 

     while int(n)!=1 and int(n)!=5 and int(n)!=10 and int(n)!=25: 
      print("invalid number") 
      n=input("Enter next coin=") 
     f=f+int(n) 
+0

귀하의 질문에 답변 되었습니까? – Adi219

+0

예, 제 질문에 답변 해주었습니다 :)! –

답변

1

이 당신의 전체 코드를 바꿉니다 : 나는 당신의 코드를 읽기 쉽게 만들어 부울 플래그 (Stay)를 사용하여 문제를 해결

import sys 

Stay = True 

while Stay: 

    x = int(input("Enter the coins you expected = ")) 
    f = int(input("Enter first coin = ")) 

    while f != 1 and f != 5 and f != 10 and f != 25: 
     f = int(input("Invalid number entered./nEnter first coin = ")) 

    while x > f and Stay: 

     n = input("Enter next coin = ") 

     if not n: 

      print("Sorry, you only entered " + str(f) + " cents") 
      again = input("Try again (y/n)?=") 

      if again == "n": 
       print("Thank you, goodbye!") 
       Stay = False 

     if Stay: 

      n = int(n) 
      while n != 1 and n != 5 and n != 10 and n != 25: 
       print("Invalid number entered.") 
       n = int(input("Enter next coin = ")) 
      f += n 

. 이것은 기본적으로 StayTrue이고 이라고 입력하면 StayFalse이되는 동안 프로그램이 실행됨을 의미합니다.