TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'について
前提・実現したいこと
pythonのクイズブログラムを動かしたいです。
python
1# Here are the three steneces I came up with so that I could create the quiz. 2easy_string = "Coding in __1__ can be __2__, but once you learn simple __3__ it can become __4__." 3medium_string = "Python is just one __1__ in coding. You can create different __2__ with different __1__s. It just takes __3__ and __4__." 4hard_string = "Using __1__ statements help create __2__ in your __3__. __2__ are useful when you need to keep checking a statement until it reaches the __4__ requirement. Good __4__ help computers complete all sorts of tasks." 5 6# Here are the words I decided to omit to create quiz answers. 7easy_answers = ["python", "difficult", "rules", "easy"] 8medium_answers = ["language", "programs", "effort", "determination" ] 9hard_answers = ["If", "loops", "code", "programs"] 10 11# This is the difficulty selection, it will take your input and pull the corresponding string and answers from the lists above. 12def level_selector(): 13 easy = ["easy", "Easy", "EASY"] 14 medium = ["medium", "Medium", "MEDIUM"] 15 hard = ["hard", "Hard", "HARD"] 16 user_input = input("Select your level of difficulty: easy / medium / hard" + "\n") 17 if user_input in easy: 18 print ("\n") + easy_string 19 return game_proper (easy_string, easy_answers) 20 elif user_input in medium: 21 print ("\n") + medium_string 22 return game_proper (medium_string, medium_answers) 23 elif user_input in hard: 24 print ("\n") + hard_string 25 return game_proper (hard_string, hard_answers) 26 else: 27 print ("Try again.") + ("\n") 28 level_selector () 29 30# This will slowly fill each part of the string with the answers the user chooses and then check them for accuracy. If it is correct it will move on to the next blank to fill in. It will trigger when it reaches the correct number between the __'s. 31def game_proper(string, answers): 32 index = 0 33 while index < len(answers): 34 user_input = raw_input("What is the answer to __" + str(index + 1) + "__ ?" + "\n") 35 if user_input.lower() == answers[index].lower(): 36 print ("Congratulations!") + "\n" 37 index += 1 38 string = string.replace("__" + str(index) + "__", user_input) 39 print (string) 40 else: 41 print ("Incorrect answer.") + "\n" + string 42 print ("Good game!") 43 44# This is the actual game, it pulls in the definitions from the other areas and runs them in order. 45def play_game(): 46 level_selector() 47 yes = ['yes', 'Y', 'YES', 'Yes','y'] 48 no = ['no', 'N', 'NO', 'No', 'n'] 49 user_input = input("Play Again?: yes / no ") 50 if user_input in yes: 51 play_game() 52 else: 53 print ("Thanks for playing!") 54 55play_game() 56
この部分の型が一致していないのが原因だと思うのですが解決できません。
print ("\n") + easy_string
対応したこと
python3では、printがステートメントから関数に変わっているエラーなので、 print() に直しました。
環境 debian
追記
returnを適切に設置されていないのが原因のような気がします。
戻り値(return)のない関数の実行結果は「値None, タイプNoneType」になるみたいです。
修正しました。
print ("\n") + easy_string
↓
print ("\n" + easy_string)
よろしくお願いします。
django-admin --version
2.1
実行結果
Coding in __1__ can be __2__, but once you learn simple __3__ it can become __4__.
What is the answer to __1__ ?
python
Congratulations!
Traceback (most recent call last):
File "Quiz.py", line 55, in <module>
play_game()
File "Quiz.py", line 46, in play_game
level_selector()
File "Quiz.py", line 19, in level_selector
return game_proper (easy_string, easy_answers)
File "Quiz.py", line 36, in game_proper
print ("Congratulations!") + "\n"
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'←戻り地がないためエラーになる
回答2件
あなたの回答
tips
プレビュー