質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

1680閲覧

pythonのクイズブログラムを動かしたいです。

believe

総合スコア91

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/03/23 07:56

編集2022/03/23 11:46

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)

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

believe

2022/03/23 08:19

django-admin --version 2.1
believe

2022/03/23 08:49

実行結果 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'←戻り地がないためエラーになる
guest

回答2

0

ベストアンサー

print ("\n") の戻り値は None で、クラスは NoneType です。NoneType と str の + はできません。
自分で書いておられるように、print関数の引数の中で + すればいいですよ。

投稿2022/03/23 10:39

編集2022/03/23 10:39
shiracamus

総合スコア5406

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

believe

2022/04/04 09:43

回答ありがとうございました。
guest

0

回答してくださってありがとうございました。

投稿2022/03/23 11:48

believe

総合スコア91

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

morimorinoki

2022/04/04 05:24

ここはコメント欄ではなく、回答蘭です。 お礼は回答してくださった方のコメント欄に書きましょう。 また、解決したのならベストアンサーはshiracamusさんです。 こちらのベストアンサーを外し、shiracamusさんの回答をベストアンサーにしてください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問