簡體   English   中英

Python中的劊子手游戲

[英]Hangman game in Python

我是學習 Python 的新手,目前正在嘗試編寫我的第一段正確的代碼,以模擬劊子手游戲。 有一個問題我無法解決。 我的代碼如下所示:

import random
word_lst = ["green","red","blue"]
selected_word = random.choice(word_lst)
blank_lst = []
for element in range(len(selected_word)):
    blank_lst.append("_")
choices_left = 10
selected_word_lst = list(selected_word)
while blank_lst != selected_word_lst and choices_left != 0:
    print(blank_lst)
    print(choices_left)
    letter_selection = input("Choose a letter ")
    if letter_selection in selected_word_lst:
        print("correct selection")
        for element in selected_word_lst:
            letter_selection_pos = selected_word_lst.index(letter_selection)
            blank_lst[letter_selection_pos] = letter_selection
    else:
        print("incorrect selection")
        choices_left -= 1
if blank_lst == selected_word_lst:
    print("You win")
elif choices_left == 0:
    print("You lose")

明顯的問題是,對於帶有重復字母的單詞(我的小測試代碼中的綠色),即使您正確地 select 該字母程序只會用正確選擇的字母替換其中一個空格。

任何有關如何使其工作的提示將不勝感激。

使用enumerate selected_word_list中的字母; 那么您將不必使用index來查找它們的位置。

index() 方法僅返回該值第一次出現的索引,因此您只能獲得第一個空白填充。 您可以修改for循環以使用 enumerate 填充字母。

我相信這行得通。

import random

# word_lst = ["green", "red", "blue"]
word_lst = ["green"]
selected_word = random.choice(word_lst)
blank_lst = []
for element in range(len(selected_word)):
    blank_lst.append("_")
choices_left = 10
selected_word_lst = list(selected_word)
while blank_lst != selected_word_lst and choices_left != 0:
    print(blank_lst)
    print(choices_left)
    letter_selection = input("Choose a letter ")
    if letter_selection in selected_word_lst:
        print("correct selection")
        for pos in range(len(selected_word_lst)):
            if selected_word_lst[pos] == letter_selection:
                blank_lst[pos] = letter_selection
    else:
        print("incorrect selection")
        choices_left -= 1
if blank_lst == selected_word_lst:
    print("You win")
elif choices_left == 0:
    print("You lose")

當使用index function 時,你只會得到第一次出現,這樣我檢查每個字母並替換所有字母。

.index(letter_selection)僅返回列表中元素的第一個找到的索引。

相反,你應該像這樣循環它,使用這樣的enumerate

if letter_selection in selected_word_lst:
    print("correct selection")
    for c, element in enumerate(selected_word_lst):
        print(element)
        if element == letter_selection:
            blank_lst[c] = letter_selection

我建議使用字典來標記字母的所有位置:

import random
from collections import defaultdict

word_lst = ["green","red","blue"]
selected_word = random.choice(word_lst)
blank_lst = ["_" for _ in range(len(selected_word))]

choices_left = 10
selected_word_lst = list(selected_word)

selected_word_map = defaultdict(list)

# store all indexes of the letter in word
for index, letter in enumerate(selected_word):
    selected_word_map[letter].append(index)

while blank_lst != selected_word_lst and choices_left != 0:
    print(blank_lst)
    print(choices_left)
    letter_selection = input("Choose a letter ")
    if letter_selection in selected_word_map:
        print("correct selection")
        # update all indexes of the given letter
        for letter_selection_pos in selected_word_map[letter_selection]:
            blank_lst[letter_selection_pos] = letter_selection
    else:
        print("incorrect selection")
        choices_left -= 1
if blank_lst == selected_word_lst:
    print("You win")
elif choices_left == 0:
    print("You lose")
import random
word_lst = ["green","red","blue"]
selected_word = random.choice(word_lst)
blank_lst = []
for element in range(len(selected_word)):
    blank_lst.append("_")
choices_left = 10
selected_word_lst = list(selected_word)
while blank_lst != selected_word_lst and choices_left != 0:
    print(blank_lst)
    print(choices_left)
    letter_selection = input("Choose a letter ")
    if letter_selection in selected_word_lst:
        print("correct selection")
        for i in range(len(selected_word_lst)):
            element = selected_word_lst[i]
            if element == letter_selection:
                blank_lst[i] = letter_selection
    else:
        print("incorrect selection")
        choices_left -= 1
if blank_lst == selected_word_lst:
    print("You win")
elif choices_left == 0:
    print("You lose")

更改僅在第line 15for loop中。 index()方法為您提供元素外觀的第一個索引,因此您要更改該元素兩次。 此代碼將更改當前的 position。

一個相關的問題是您的代碼不能很自然地處理重復的猜測。 解決這兩個問題的一種直接方法是使用一組字母來跟蹤哪些字母還沒有被猜到。

另請注意,您可以直接迭代單詞(字符串),而無需將它們轉換為列表。 唯一的缺點是,每次正確猜到一個字母時,您都必須重新構建空白單詞(因為字符串是不可變的)。 因此,將代碼的這個空白更新部分放在 function 中是有意義的:

def update_blanks(selected_word, letters_to_guess):
    blank_word = ''
    for element in selected_word:
        if element in letters_to_guess:
            blank_word += '_'
        else:
            blank_word += element
    return blank_word

# temporarily changed to always produce problem with double letter
#import random
#word_lst = ["green","red","blue"]
#selected_word = random.choice(word_lst)
selected_word = "green"

letters_to_guess = set(selected_word)
blank_word = update_blanks(selected_word, letters_to_guess)
choices_left = 10

while blank_word != selected_word and choices_left != 0:
    print(blank_word)
    print(choices_left)
    letter_selection = input("Choose a letter ")
    
    if letter_selection in letters_to_guess:
        print("correct selection")
        letters_to_guess -= set(letter_selection)
        blank_word = update_blanks(selected_word, letters_to_guess)
    else:
        print("incorrect selection")
        choices_left -= 1
        
if blank_word == selected_word:
    print("You win")
elif choices_left == 0:
    print("You lose")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM