1652515987
パズルを解くことは、長い一日の後にリラックスして時間を過ごす方法です。それは心にも有益です。
さらに良いことに、パズルを解くスキルと問題解決スキルの向上には相関関係があります。
Wordleは、6回の試行で5文字の単語を推測するようにプレーヤーに挑戦する新しい単語パズルゲームです。
このチュートリアルでは、元のゲームと同じルールでWordleのような推測ゲームを作成します。Pythonでゲームをビルドします。この課題に取り組むことで、関数とwhileループの知識が向上し、zipメソッドに慣れることができます。
ゲームは以下で構成されます:
まず、ルールについてプレイヤーに通知する必要があります。これは、人々が適切にプレイする方法を知るために必要です。
「game_instruction」という名前の関数を作成することから始めます。
def game_instruction():
次に、命令を文字列として「print」関数に渡して、結果を表示します。記号( "✔❌❌✔➕")は二重引用符( "")でラップされるため、文字列をdocstrings( "" "" "")でラップします。また、各命令は( "\ n")タグを使用せずに新しい行に表示されます。
print("""Wordle is a single player game
A player has to guess a five letter hidden word
You have six attempts
Your Progress Guide "✔❌❌✔➕"
"✔" Indicates that the letter at that position was guessed correctly
"➕" indicates that the letter at that position is in the hidden word, but in a different position
"❌" indicates that the letter at that position is wrong, and isn't in the hidden word """)
各文は新しい行で始まり、コンソールにそのように表示されます。関数を呼び出して終了し、指示が画面に印刷されるようにします。
game_instruction()
エラーが発生した場合は、関数定義の最後にコロン(:)を付けるのを忘れているdef game_instruction()
か、コードが正しくフォーマットされていない可能性があります。ログに記録されたコンソールエラーに注意してください。
def game_instruction():
print("""Wordle is a single player game
A player has to guess a five letter hidden word
You have six attempts
Your Progress Guide "✔❌❌✔➕"
"✔" Indicates that the letter at that position was guessed correctly
"➕" indicates that the letter at that position is in the hidden word, but in a different position
"❌" indicates that the letter at that position is wrong, and isn't in the hidden word """)
game_instruction()
そして最後に、コードを実行してもコンソールに結果が表示されない場合は、おそらく関数の呼び出しを忘れていることを意味します。
プレイヤー向けのゲームの説明
次のステップは、ユーザーのエントリを操作し、それを非表示の単語と比較することです。それを行う能力は、ゲームに不可欠です。
「check_word」という関数を作成します。コードブロックで、「hidden word」という名前の変数を作成し、それを任意の5文字の単語に割り当てます。この隠された単語は、ユーザーが正しく推測しようとするものです。
def check_word():
hidden_word = "snail"
プレーヤーには6回の試行があるため、「attempt」という新しい変数を「6」の値に割り当て、whileステートメントを作成します。
ここではwhileループを使用するのが最適です。これは、ユーザーが正しい単語を推測するか、試行を使い果たすまでプロセスが実行されるためです。whileステートメントを実行するための条件は、試行回数が「0」より大きい場合です。
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
次に、ユーザーの入力がwhileループ内に作成され、条件が非表示の単語に対してチェックされます。ユーザーのエントリが非表示の単語と同じである場合、ループは終了し、ゲームは終了します。
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
フォーマット文字列(f "")は、"+"記号を使用せずに変数と文字列を結合するもう1つの方法です。
次に例を示します。
# Instead of,
print("you have" + attempt + " attempt(s) ,, \n") # '\n' is used for new line
# use this,
print(f"you have {attempt} attempt(s) ,, \n") # the variable to be printed is wrapped in curly braces
ユーザーのエントリが隠し単語と等しくない場合は、elseステートメントを導入すると、すべての条件が「else」ブロックでチェックされます。ユーザーがゲームをプレイすると、試行回数が1減少し、残りの試行回数がコンソールに出力されます。
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
else:
attempt = attempt - 1
print(f"you have {attempt} attempt(s) ,, \n ")
ユーザーの入力が非表示の単語と一致しない場合は、次の3つの条件を確認する必要があります。
ユーザーの入力と非表示の単語の両方の文字を比較するには、ステートメントとしてzip()関数の横にforループを含めます。
for i, j in zip(food, drink):
zip()関数は、リストやタプルなどの項目をループする組み込み関数です。同じサイズの複数の変数から値を抽出できます。
文字列の場合、zip()関数を単独で直接使用することはできません。文字列を格納する変数から文字を取得するために、「for」ループが含まれています。
次に例を示します。
ユーザーが5文字の単語を入力すると、5文字の単語を含む変数が作成されます。zip()を使用して2つの変数を同時にループすると、すべての要素が印刷され、ハイフンで区切られます。
コードブロック
user_entry = input("spell 5 letter word: ")
default_value = "shell"
for i, j in zip(user_entry, default_value):
print(i + " - " + j)
出力
コードに戻る:
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
else:
attempt = attempt - 1
print(f"you have {attempt} attempt(s) ,, \n ")
for char, word in zip(hidden_word, guess):
if word in hidden_word and word in char:
print(word + " ✔ ")
elif word in hidden_word:
print(word + " ➕ ")
else:
print(" ❌ ")
ここで何が起こっているのかを見てみましょう:
for char, word in zip(hidden_word, guess)
-このステートメントはhidden_word
、変数名char
でループスルーguess
し、変数名でループスルーすることを意味しますword
。非表示の単語のchar
すべての文字はによってアクセスされ、推測のすべての文字はによってアクセスされword
ます。
次に、前述の3つの条件がチェックされ、 word
(ユーザーの入力)とchar
(隠し単語)の両方の文字が比較されます。
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
else:
attempt = attempt - 1
print(f"you have {attempt} attempt(s) ,, \n ")
for char, word in zip(hidden_word, guess):
if word in hidden_word and word in char:
print(word + " ✔ ")
elif word in hidden_word:
print(word + " ➕ ")
else:
print(" ❌ ")
if attempt == 0:
print(" Game over !!!! ")
最後のステップは、関数を呼び出すことです。
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
else:
attempt = attempt - 1
print(f"you have {attempt} attempt(s) ,, \n ")
for char, word in zip(hidden_word, guess):
if word in hidden_word and word in char:
print(word + " ✔ ")
elif word in hidden_word:
print(word + " ➕ ")
else:
print(" ❌ ")
if attempt == 0:
print(" Game over !!!! ")
check_word()
すべてのコードブロックをまとめると、次のようになります。
def game_instruction():
print("""Wordle is a single player game
A player has to guess a five letter hidden word
You have six attempts
Your Progress Guide "✔❌❌✔➕"
"✔" Indicates that the letter at that position was guessed correctly
"➕" indicates that the letter at that position is in the hidden word, but in a different position
"❌" indicates that the letter at that position is wrong, and isn't in the hidden word """)
game_instruction()
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
else:
attempt = attempt - 1
print(f"you have {attempt} attempt(s) ,, \n ")
for char, word in zip(hidden_word, guess):
if word in hidden_word and word in char:
print(word + " ✔ ")
elif word in hidden_word:
print(word + " ➕ ")
else:
print(" ❌ ")
if attempt == 0:
print(" Game over !!!! ")
check_word()
出力:
よくやった!Pythonで単語パズルゲームの作成が完了しました。
出典:https ://www.freecodecamp.org/news/building-a-wordle-game/
1652515987
パズルを解くことは、長い一日の後にリラックスして時間を過ごす方法です。それは心にも有益です。
さらに良いことに、パズルを解くスキルと問題解決スキルの向上には相関関係があります。
Wordleは、6回の試行で5文字の単語を推測するようにプレーヤーに挑戦する新しい単語パズルゲームです。
このチュートリアルでは、元のゲームと同じルールでWordleのような推測ゲームを作成します。Pythonでゲームをビルドします。この課題に取り組むことで、関数とwhileループの知識が向上し、zipメソッドに慣れることができます。
ゲームは以下で構成されます:
まず、ルールについてプレイヤーに通知する必要があります。これは、人々が適切にプレイする方法を知るために必要です。
「game_instruction」という名前の関数を作成することから始めます。
def game_instruction():
次に、命令を文字列として「print」関数に渡して、結果を表示します。記号( "✔❌❌✔➕")は二重引用符( "")でラップされるため、文字列をdocstrings( "" "" "")でラップします。また、各命令は( "\ n")タグを使用せずに新しい行に表示されます。
print("""Wordle is a single player game
A player has to guess a five letter hidden word
You have six attempts
Your Progress Guide "✔❌❌✔➕"
"✔" Indicates that the letter at that position was guessed correctly
"➕" indicates that the letter at that position is in the hidden word, but in a different position
"❌" indicates that the letter at that position is wrong, and isn't in the hidden word """)
各文は新しい行で始まり、コンソールにそのように表示されます。関数を呼び出して終了し、指示が画面に印刷されるようにします。
game_instruction()
エラーが発生した場合は、関数定義の最後にコロン(:)を付けるのを忘れているdef game_instruction()
か、コードが正しくフォーマットされていない可能性があります。ログに記録されたコンソールエラーに注意してください。
def game_instruction():
print("""Wordle is a single player game
A player has to guess a five letter hidden word
You have six attempts
Your Progress Guide "✔❌❌✔➕"
"✔" Indicates that the letter at that position was guessed correctly
"➕" indicates that the letter at that position is in the hidden word, but in a different position
"❌" indicates that the letter at that position is wrong, and isn't in the hidden word """)
game_instruction()
そして最後に、コードを実行してもコンソールに結果が表示されない場合は、おそらく関数の呼び出しを忘れていることを意味します。
プレイヤー向けのゲームの説明
次のステップは、ユーザーのエントリを操作し、それを非表示の単語と比較することです。それを行う能力は、ゲームに不可欠です。
「check_word」という関数を作成します。コードブロックで、「hidden word」という名前の変数を作成し、それを任意の5文字の単語に割り当てます。この隠された単語は、ユーザーが正しく推測しようとするものです。
def check_word():
hidden_word = "snail"
プレーヤーには6回の試行があるため、「attempt」という新しい変数を「6」の値に割り当て、whileステートメントを作成します。
ここではwhileループを使用するのが最適です。これは、ユーザーが正しい単語を推測するか、試行を使い果たすまでプロセスが実行されるためです。whileステートメントを実行するための条件は、試行回数が「0」より大きい場合です。
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
次に、ユーザーの入力がwhileループ内に作成され、条件が非表示の単語に対してチェックされます。ユーザーのエントリが非表示の単語と同じである場合、ループは終了し、ゲームは終了します。
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
フォーマット文字列(f "")は、"+"記号を使用せずに変数と文字列を結合するもう1つの方法です。
次に例を示します。
# Instead of,
print("you have" + attempt + " attempt(s) ,, \n") # '\n' is used for new line
# use this,
print(f"you have {attempt} attempt(s) ,, \n") # the variable to be printed is wrapped in curly braces
ユーザーのエントリが隠し単語と等しくない場合は、elseステートメントを導入すると、すべての条件が「else」ブロックでチェックされます。ユーザーがゲームをプレイすると、試行回数が1減少し、残りの試行回数がコンソールに出力されます。
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
else:
attempt = attempt - 1
print(f"you have {attempt} attempt(s) ,, \n ")
ユーザーの入力が非表示の単語と一致しない場合は、次の3つの条件を確認する必要があります。
ユーザーの入力と非表示の単語の両方の文字を比較するには、ステートメントとしてzip()関数の横にforループを含めます。
for i, j in zip(food, drink):
zip()関数は、リストやタプルなどの項目をループする組み込み関数です。同じサイズの複数の変数から値を抽出できます。
文字列の場合、zip()関数を単独で直接使用することはできません。文字列を格納する変数から文字を取得するために、「for」ループが含まれています。
次に例を示します。
ユーザーが5文字の単語を入力すると、5文字の単語を含む変数が作成されます。zip()を使用して2つの変数を同時にループすると、すべての要素が印刷され、ハイフンで区切られます。
コードブロック
user_entry = input("spell 5 letter word: ")
default_value = "shell"
for i, j in zip(user_entry, default_value):
print(i + " - " + j)
出力
コードに戻る:
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
else:
attempt = attempt - 1
print(f"you have {attempt} attempt(s) ,, \n ")
for char, word in zip(hidden_word, guess):
if word in hidden_word and word in char:
print(word + " ✔ ")
elif word in hidden_word:
print(word + " ➕ ")
else:
print(" ❌ ")
ここで何が起こっているのかを見てみましょう:
for char, word in zip(hidden_word, guess)
-このステートメントはhidden_word
、変数名char
でループスルーguess
し、変数名でループスルーすることを意味しますword
。非表示の単語のchar
すべての文字はによってアクセスされ、推測のすべての文字はによってアクセスされword
ます。
次に、前述の3つの条件がチェックされ、 word
(ユーザーの入力)とchar
(隠し単語)の両方の文字が比較されます。
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
else:
attempt = attempt - 1
print(f"you have {attempt} attempt(s) ,, \n ")
for char, word in zip(hidden_word, guess):
if word in hidden_word and word in char:
print(word + " ✔ ")
elif word in hidden_word:
print(word + " ➕ ")
else:
print(" ❌ ")
if attempt == 0:
print(" Game over !!!! ")
最後のステップは、関数を呼び出すことです。
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
else:
attempt = attempt - 1
print(f"you have {attempt} attempt(s) ,, \n ")
for char, word in zip(hidden_word, guess):
if word in hidden_word and word in char:
print(word + " ✔ ")
elif word in hidden_word:
print(word + " ➕ ")
else:
print(" ❌ ")
if attempt == 0:
print(" Game over !!!! ")
check_word()
すべてのコードブロックをまとめると、次のようになります。
def game_instruction():
print("""Wordle is a single player game
A player has to guess a five letter hidden word
You have six attempts
Your Progress Guide "✔❌❌✔➕"
"✔" Indicates that the letter at that position was guessed correctly
"➕" indicates that the letter at that position is in the hidden word, but in a different position
"❌" indicates that the letter at that position is wrong, and isn't in the hidden word """)
game_instruction()
def check_word():
hidden_word = "snail"
attempt = 6
while attempt > 0:
guess = str(input("Guess the word: "))
if guess == hidden_word:
print("You guessed the words correctly! WIN 🕺🕺🕺 ")
break
else:
attempt = attempt - 1
print(f"you have {attempt} attempt(s) ,, \n ")
for char, word in zip(hidden_word, guess):
if word in hidden_word and word in char:
print(word + " ✔ ")
elif word in hidden_word:
print(word + " ➕ ")
else:
print(" ❌ ")
if attempt == 0:
print(" Game over !!!! ")
check_word()
出力:
よくやった!Pythonで単語パズルゲームの作成が完了しました。
出典:https ://www.freecodecamp.org/news/building-a-wordle-game/