2017-11-09 20 views
0

私のプログラムを実行すると、以下のメニューが実行されますが、ユーザがオプションを選択する必要がある部分については、プログラムは「無限ループを有していてもよく、私は、ループを終了するために何ができるし、他のユーザーが提案してきたように、ユーザが自分のオプションPythonの無限ループのためタイムアウトになる方法

from operator import itemgetter 
    high_scores = {"Benson ":100, "fizzlepop":300,"Mikey123":200} 

def main(): 
    choice = None 
    while choice != "0": 

     print(
     """ 
     Hi scores system 
     0 - Quit 
     1 - Look up someone's high score 
     2 - Add a score 
     3 - Update a score 
     4 - Display all high-scores 
     """ 
    ) 
     #After this point, the program begins to go into a time out as it seems to be going into an endless loop 
     choice = input("Choice: ") 
     #exit 
     if choice == "0": 
      print("Goodbye") 

     #look up a score 
     elif choice == "1": 
      player = input("Whose score would you like to look at?") 
      if player in high_scores: 
       score = high_scores[player] 
       print("\n", player, "'s score is ", score) 
      else: 
       print("Sorry, player ", player, "not in system") 
     elif choice == "2": 
      #Your answer to question 2 here-> this is where the user should be able to add a new name and score to the program 
      name= input("\nEnter new player name: ") 
      score= input("\nEnter score: ") 
      high_scores[name] = score 
      print("\n",name, "has been added") 

     #edit a high score 
     elif choice == "3": 
      #Your answer to question 3 here 
      print("Complete this part of the program") 

     #display all high scores 
     elif choice == "4": 
      for key, value in sorted(high_scores.items(), key=itemgetter(1), reverse = True): 
       print(key, value) 

    if __name__ == "__main__": 
     main() 


    PythonDictionaryScoreProgramIncomplete.py 
+2

あなたはPython 2を使用していますか? –

+0

入力後に 'repr(choice)'を使って 'print'を試して、その内容を確認してください。 –

+0

どのようなOSですか?どのようなPythonのバージョン?どのようにあなたのプログラムを実行していますか? (私は、そのメッセージがPython自体から来ているとは認識していません。) –

答えて

0

に入ることができるように、変更してみてくださいメニューをアクティブ:

choice = input("Choice: ") 

To:

choice = str(input("Choice: ")) 
choice = str(input("Choice: ")).lstrip().strip() # Might be better in case you get an input like "0 " or " 0" etc 

それが解決するかどうか確認してください。

関連する問題