def level_selection(): user_input = raw_input("Choose the difficulty of quize in number or string: (1.Easy 2.Medium 3.Hard)").lower() game_level = ["easy","medium","hard"] max = 4 if user_input in game_level: print ("Debug user_input = " + user_input) #debug return user_input elif user_input in str(range(max)): return game_level[int(user_input) - 1] else: print("Incorrect level! Try again!") print ("Debug user_input = " + user_input) #debug level_selection() #chosse the difficulty of game level_answer #output:the list of questions and answers of selected level game_level = level_selection() #ここのgame_levelがNoneになっています。 print ("game_level " + str(game_level))
上記のプログラムを動かし、一発で正しい値をuser_inputで入力すると下の通り問題なく作動します。
しかし、一度誤った値を入力してから正しい値を入力しても、なぜかreturnの戻り値がNone型になってしまうのです。⤵︎
現にstrでstring型に直さなければ、エラーが出ます。
def level_selection(): user_input = raw_input("Choose the difficulty of quize in number or string: (1.Easy 2.Medium 3.Hard)").lower() game_level = ["easy","medium","hard"] max = 4 if user_input in game_level: print ("Debug user_input = " + user_input) #debug return user_input elif user_input in str(range(max)): return game_level[int(user_input) - 1] else: print("Incorrect level! Try again!") print ("Debug user_input = " + user_input) #debug level_selection() #chosse the difficulty of game level_answer #output:the list of questions and answers of selected level game_level = level_selection() print ("game_level " + game_level) #str()を取りました。
↓
どなたか教えていただけますと大変嬉しく思います。
よろしくお願いいたします。
コードやログはテキストで書いて下さい。
![guest](/img/icon/icnUserSample.jpg)
回答2件
あなたの回答
tips
プレビュー