2016-07-27 11 views
2

簡単な質問ですが、それは私をナットにしています。なぜ私はこのコードを実行すると、それだけでそれ自体を繰り返すのですか?そして、私のインデントは正当なもので、何らかの理由でこれを投稿するのに4倍のスペースが必要でした。Python whileループ自体が繰り返され続ける

ハイスコア

0 - 出口 1 - ショースコア

ソースコード:

scores = [] 

choice = None 
while choice != "0": 

    print(
    """ 
    High Scores 

    0 - Exit 
    1 - Show Scores 
    """ 
    ) 

    choice = input("choice: ") 
    print() 

    if choice == "0": 
     print ("exiting") 

    elif choice == "1": 
     score = int(input("what score did you get?: ")) 
     scores.append(score) 

    else: 
     print ("no") 

    input ("\n\nPress enter to exit") 
+0

はあなたは、Python 3を実行している_sure_ていますか? –

+0

sayan 98は私のwhileループの下での私のアイデンティティでした。ありがとうございました –

+0

@JacobMooreが喜んでお手伝いします! – sayan

答えて

1

これは、適切な字下げを使用していないためです。実行するwhileループのコードをインデントしてください。while choice != 0

また、@ wookie919が間違って表示されているので間違いはありません。文字列は入力ではなくIntであるためです。しかし、入力を文字列としてint()のようにラップして型キャストできます。int(input("Choice .. "))

希望しました。

0

あなたが整数を文字列に比較しているためです。 0の代わりに"0"と入力してください。または、の代わりに0と比較するようにプログラムを変更してください。

+0

OPはintと文字列を比較しますか? –

+0

@ Two-BitAlchemist OPのコードを試した後、それは教育的な推測です。 – wookie919

-1
scores = [] 

choice = None 
while choice != "Exit": 

    print("High Scores\n0 - Exit\n1 - Show Scores") 

    choice = input("choice: ") 

    if choice == "0": 
     print ("exiting") 

    elif choice == "1": 
     try: 
      score = int(input("What score did you get?: ")) 
      scores.append(score) 
     except: 
      print('INVALID INPUT') 
    else: 
     print('INVALID INPUT') 

    choice = input ("\n\nPress enter to exit: ") 
    choice = "Exit" 
+0

あなたは私の他の答えも見るべきです。エラー処理の方が優れています。 –

+0

将来、このステッパーツールで問題となるコードを実行する必要があります。それはあなたを助けるでしょう: http://www.pythontutor.com/visualize.html#mode=edit –

-1

また、私の最後の答えよりも優れている、これを行うことができます:

scores = [] 

def user_choice(): 
    choice = None 
    while choice != "Exit": 

     print("High Scores\n0 - Exit\n1 - Show Scores") 

     choice = input("Choice: ") 

     if choice == "0": 
      print ("Exiting...") 
      return None 

     elif choice == "1": 
      try: 
       score = int(input("\nWhat score did you get?: ")) 
       scores.append(score) 
       go_on = input("\nEnter 'y' to go back or anything else to exit: ") 
       if go_on == "y": 
        user_choice() 
       else: 
        print('Exiting...') 
        choice = "Exit" 
      except: 
       print('\nINVALID INPUT\n') 
       user_choice() 
     else: 
      print('\nINVALID INPUT\n') 
      user_choice() 


     choice = "Exit" 

user_choice() 
関連する問題