如何将特定字母添加到变量,然后使其成为字符串
How do I add a specific letter to a variable and then make it a string
我正在尝试制作刽子手游戏,但我不太确定如何将特定字母添加到变量中。
例如,将某人通过 pythons 的输入提示选择的字母添加到变量中。这是我正在处理的代码:
import random
import time
word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)
your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")
guesses = 7
time.sleep(1)
hidden_word = ""
for i in word_chosen:
hidden_word += "-"
while True:
n = 1
time.sleep(1)
print("Your word is: " + hidden_word)
print("___________")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
characters = (word_chosen)
time.sleep(1)
letters_correct = ""
characters_guessed = input("Type in the letters you think are in this word!")
for i in characters:
hidden_word == characters_guessed
if characters_guessed == characters:
print(hidden_word + characters)
else:
int(guesses) - int(n)
print("Nope, sorry.")
Python 字符串不支持项目分配,所以我会使用一个字符串数组,然后在你想打印单词时使用 "".join(hidden_word)
。您可以遍历所选单词,然后当该字母与输入字母匹配时,在 hidden_word
数组中填写该位置。
以下是我将如何调整您的代码以使游戏达到 运行:
import random
import time
word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)
your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")
guesses = 7
won = False
hidden_word = ["|"] * len(word_chosen)
while guesses > 0:
print("Your word is: " + "".join(hidden_word))
print("___________")
for _ in range(5):
print("|")
guessed = input("Type in the letters you think are in this word!")
for c in guessed:
for i in range(0, len(word_chosen)):
if word_chosen[i] == c:
hidden_word[i] = c
if "|" not in hidden_word:
won = True
break
guesses -= 1
if won:
print(f'Word found: {"".join(hidden_word)}')
else:
print(f'Nope, sorry - word was {word_chosen}')
因此,为了回答您最初的问题,我将输入的字符串分配给 guessed
。然后我遍历 guessed
中的每个字母,并对照 chosen_word
中的每个位置检查它。如果该位置的字母匹配,我将占位符 |
更改为所选字母。一旦没有占位符,则游戏获胜。
如果您想限制用户一次只能输入一个字符,我会以 this 线程为例,了解如何调整 input()
来处理这个问题。
我已经修改了你的代码@Noob 并使其工作。我没有使用字符串,而是列出了 chosen_word 、字符和 characters_guessed 。在这段代码中,您甚至可以一次输入多个单词。它会自动填充重复词,并会告诉用户他或她是赢了还是输了。
import random
import time
word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)
your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")
guesses = 7
time.sleep(1)
hidden_word = [" - " for i in range(len(word_chosen))]
while guesses>0:
time.sleep(1)
print("Your word is: "+ "".join(hidden_word))
print("___________")
for i in range(7):
print("|")
characters = list(word_chosen)
time.sleep(1)
letters_correct = ""
characters_guessed = input("Type in the letters you think are in this word!").split()
for j in characters_guessed:
if j not in characters:
guesses-=1
print(f"Wrong guess, {j} is not in the word")
else:
for i in characters:
if i==j:
find_position=0
for k in range(characters.count(i)):
hidden_word[characters.index(i,find_position)]=j
find_position+=characters.index(i,find_position)+1
if " - " not in hidden_word:
print("Congratulations, you have won the game.The word was {chosen_word}")
break
elif guesses==0:
print("Opps!! out of guesses. You have lost the game.")
我正在尝试制作刽子手游戏,但我不太确定如何将特定字母添加到变量中。 例如,将某人通过 pythons 的输入提示选择的字母添加到变量中。这是我正在处理的代码:
import random
import time
word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)
your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")
guesses = 7
time.sleep(1)
hidden_word = ""
for i in word_chosen:
hidden_word += "-"
while True:
n = 1
time.sleep(1)
print("Your word is: " + hidden_word)
print("___________")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
characters = (word_chosen)
time.sleep(1)
letters_correct = ""
characters_guessed = input("Type in the letters you think are in this word!")
for i in characters:
hidden_word == characters_guessed
if characters_guessed == characters:
print(hidden_word + characters)
else:
int(guesses) - int(n)
print("Nope, sorry.")
Python 字符串不支持项目分配,所以我会使用一个字符串数组,然后在你想打印单词时使用 "".join(hidden_word)
。您可以遍历所选单词,然后当该字母与输入字母匹配时,在 hidden_word
数组中填写该位置。
以下是我将如何调整您的代码以使游戏达到 运行:
import random
import time
word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)
your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")
guesses = 7
won = False
hidden_word = ["|"] * len(word_chosen)
while guesses > 0:
print("Your word is: " + "".join(hidden_word))
print("___________")
for _ in range(5):
print("|")
guessed = input("Type in the letters you think are in this word!")
for c in guessed:
for i in range(0, len(word_chosen)):
if word_chosen[i] == c:
hidden_word[i] = c
if "|" not in hidden_word:
won = True
break
guesses -= 1
if won:
print(f'Word found: {"".join(hidden_word)}')
else:
print(f'Nope, sorry - word was {word_chosen}')
因此,为了回答您最初的问题,我将输入的字符串分配给 guessed
。然后我遍历 guessed
中的每个字母,并对照 chosen_word
中的每个位置检查它。如果该位置的字母匹配,我将占位符 |
更改为所选字母。一旦没有占位符,则游戏获胜。
如果您想限制用户一次只能输入一个字符,我会以 this 线程为例,了解如何调整 input()
来处理这个问题。
我已经修改了你的代码@Noob 并使其工作。我没有使用字符串,而是列出了 chosen_word 、字符和 characters_guessed 。在这段代码中,您甚至可以一次输入多个单词。它会自动填充重复词,并会告诉用户他或她是赢了还是输了。
import random
import time
word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)
your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")
guesses = 7
time.sleep(1)
hidden_word = [" - " for i in range(len(word_chosen))]
while guesses>0:
time.sleep(1)
print("Your word is: "+ "".join(hidden_word))
print("___________")
for i in range(7):
print("|")
characters = list(word_chosen)
time.sleep(1)
letters_correct = ""
characters_guessed = input("Type in the letters you think are in this word!").split()
for j in characters_guessed:
if j not in characters:
guesses-=1
print(f"Wrong guess, {j} is not in the word")
else:
for i in characters:
if i==j:
find_position=0
for k in range(characters.count(i)):
hidden_word[characters.index(i,find_position)]=j
find_position+=characters.index(i,find_position)+1
if " - " not in hidden_word:
print("Congratulations, you have won the game.The word was {chosen_word}")
break
elif guesses==0:
print("Opps!! out of guesses. You have lost the game.")