2017-10-14 6 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であり、'n'と入力したときにStayFalseになる間、プログラムが実行されることを意味します。

関連する問題