Python - 刽子手
Python - Hangman
我刚写了一个运行程序来玩 Hangman 游戏,但有一个问题一直困扰着我,我一直没弄明白。
程序正确评估所选字符是否在要猜测的单词中,以及它在单词中的位置(即使该字符出现不止一次)。尽管如此,在玩游戏时,需要多转一圈来评估你是否完全猜对了这个词,或者你是否已经没有生命了。
比如说,你必须猜出单词 'hello'。我希望编程为 运行,例如:
第 1 回合:'h'。转弯 2:'e'。转3:'l'。转4:'o'。程序应该在这里停止,但它需要另一个角色(任何)来识别游戏已经完成。
这是我的代码:
def play_game():
# Pick word randomly from list
random_num = random.randint(0, (len(words_list) - 1))
current_word = list(words_list[random_num].upper())
print(current_word)
print('The word has', str(len(current_word)), 'characters')
current_letter = input('Pick a letter: ').upper() #User input
picked_letters = []
correct_ones = []
for i in range(len(current_word)):
correct_ones.append('_ ')
lifes = 3
while lifes != 0:
# Check if all characters have been already guessed
if current_word == correct_ones:
print('You won!')
break
# Check if character has already been chosen
elif current_letter in picked_letters:
print('You already chose this letter!')
current_letter = input('Pick another letter: ').upper()
continue
# Check if character is in word
if current_letter in current_word:
index_list = []
for i in range(len(current_word)): #Get indexes of character in word
if current_word[i] == current_letter:
index_list.append(i)
picked_letters.append(current_letter) #Append to keep track of chosen characters
for i in index_list:
correct_ones[i] = current_letter.upper() #Append to correct position
print('Correct!')
for i in correct_ones:
print(i + ' ', end='')
current_letter = input('Pick another letter: ').upper()
# Incorrect character
else:
picked_letters.append(current_letter)
lifes -= 1
print('Incorrect')
print('You have', str(lifes), 'lifes left')
current_letter = input('Pick another letter: ').upper()
continue
play_game()
如果您在我的代码中看到任何不好的做法,请随时告诉我!
提前致谢!
您在循环结束时要求用户输入。
所以当你输入最后一个正确的字符时,你再次要求用户输入:
print('Correct!')
for i in correct_ones:
print(i + ' ', end='')
current_letter = input('Pick another letter: ').upper()
要解决此问题,您可以将在循环开始时所做的检查移至此处。因为在游戏开始时,没有给出任何输入,所以永远不会正确猜出这个词(除非真正的词是空的,但这种情况不应该发生!)。而且因为只有猜对了一个字符才能让你获胜,所以在那里检查它是有意义的。
希望这对您有所帮助,玩得开心:)
我对您的代码进行了一些更改。现在它按预期工作了。
def play_game():
# Pick word randomly from list
random_num = random.randint(0, (len(words_list) - 1))
current_word = list(words_list[random_num].upper())
print(current_word)
print('The word has', str(len(current_word)), 'characters')
picked_letters = []
correct_ones = []
for i in range(len(current_word)):
correct_ones.append('_ ')
lifes = 3
while lifes != 0:
# Check if all characters have been already guessed
if current_word == correct_ones:
print('You won!')
break
current_letter = input('Pick a letter: ').upper()
# Check if character has already been chosen
if current_letter in picked_letters:
print('You already chose this letter!')
continue
# Check if character is in word
if current_letter in current_word:
index_list = []
for i in range(len(current_word)): # Get indexes of character in word
if current_word[i] == current_letter:
index_list.append(i)
picked_letters.append(current_letter) # Append to keep track of chosen characters
for i in index_list:
correct_ones[i] = current_letter.upper() # Append to correct position
print('Correct!')
for i in correct_ones:
print(i + ' ', end='')
# Incorrect character
else:
picked_letters.append(current_letter)
lifes -= 1
print('Incorrect')
print('You have', str(lifes), 'lifes left')
continue
play_game()
这是一个快速循环,可以添加您的猜测并将您的单词变成“-”。
while tries > 0:
hiddenphrase = ""
for char in word:
if char in guessed:
hiddenphrase = hiddenphrase + char
elif char in letters:
hiddenphrase = hiddenphrase + "-"
else:
hiddenphrase = hiddenphrase + char
我刚写了一个运行程序来玩 Hangman 游戏,但有一个问题一直困扰着我,我一直没弄明白。
程序正确评估所选字符是否在要猜测的单词中,以及它在单词中的位置(即使该字符出现不止一次)。尽管如此,在玩游戏时,需要多转一圈来评估你是否完全猜对了这个词,或者你是否已经没有生命了。
比如说,你必须猜出单词 'hello'。我希望编程为 运行,例如:
第 1 回合:'h'。转弯 2:'e'。转3:'l'。转4:'o'。程序应该在这里停止,但它需要另一个角色(任何)来识别游戏已经完成。
这是我的代码:
def play_game():
# Pick word randomly from list
random_num = random.randint(0, (len(words_list) - 1))
current_word = list(words_list[random_num].upper())
print(current_word)
print('The word has', str(len(current_word)), 'characters')
current_letter = input('Pick a letter: ').upper() #User input
picked_letters = []
correct_ones = []
for i in range(len(current_word)):
correct_ones.append('_ ')
lifes = 3
while lifes != 0:
# Check if all characters have been already guessed
if current_word == correct_ones:
print('You won!')
break
# Check if character has already been chosen
elif current_letter in picked_letters:
print('You already chose this letter!')
current_letter = input('Pick another letter: ').upper()
continue
# Check if character is in word
if current_letter in current_word:
index_list = []
for i in range(len(current_word)): #Get indexes of character in word
if current_word[i] == current_letter:
index_list.append(i)
picked_letters.append(current_letter) #Append to keep track of chosen characters
for i in index_list:
correct_ones[i] = current_letter.upper() #Append to correct position
print('Correct!')
for i in correct_ones:
print(i + ' ', end='')
current_letter = input('Pick another letter: ').upper()
# Incorrect character
else:
picked_letters.append(current_letter)
lifes -= 1
print('Incorrect')
print('You have', str(lifes), 'lifes left')
current_letter = input('Pick another letter: ').upper()
continue
play_game()
如果您在我的代码中看到任何不好的做法,请随时告诉我!
提前致谢!
您在循环结束时要求用户输入。 所以当你输入最后一个正确的字符时,你再次要求用户输入:
print('Correct!')
for i in correct_ones:
print(i + ' ', end='')
current_letter = input('Pick another letter: ').upper()
要解决此问题,您可以将在循环开始时所做的检查移至此处。因为在游戏开始时,没有给出任何输入,所以永远不会正确猜出这个词(除非真正的词是空的,但这种情况不应该发生!)。而且因为只有猜对了一个字符才能让你获胜,所以在那里检查它是有意义的。 希望这对您有所帮助,玩得开心:)
我对您的代码进行了一些更改。现在它按预期工作了。
def play_game():
# Pick word randomly from list
random_num = random.randint(0, (len(words_list) - 1))
current_word = list(words_list[random_num].upper())
print(current_word)
print('The word has', str(len(current_word)), 'characters')
picked_letters = []
correct_ones = []
for i in range(len(current_word)):
correct_ones.append('_ ')
lifes = 3
while lifes != 0:
# Check if all characters have been already guessed
if current_word == correct_ones:
print('You won!')
break
current_letter = input('Pick a letter: ').upper()
# Check if character has already been chosen
if current_letter in picked_letters:
print('You already chose this letter!')
continue
# Check if character is in word
if current_letter in current_word:
index_list = []
for i in range(len(current_word)): # Get indexes of character in word
if current_word[i] == current_letter:
index_list.append(i)
picked_letters.append(current_letter) # Append to keep track of chosen characters
for i in index_list:
correct_ones[i] = current_letter.upper() # Append to correct position
print('Correct!')
for i in correct_ones:
print(i + ' ', end='')
# Incorrect character
else:
picked_letters.append(current_letter)
lifes -= 1
print('Incorrect')
print('You have', str(lifes), 'lifes left')
continue
play_game()
这是一个快速循环,可以添加您的猜测并将您的单词变成“-”。
while tries > 0:
hiddenphrase = ""
for char in word:
if char in guessed:
hiddenphrase = hiddenphrase + char
elif char in letters:
hiddenphrase = hiddenphrase + "-"
else:
hiddenphrase = hiddenphrase + char