Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 想搞个绞刑游戏吗_Python - Fatal编程技术网

Python 想搞个绞刑游戏吗

Python 想搞个绞刑游戏吗,python,Python,我是一个极端的初学者,目前正在尝试用Python3创建一个hangman游戏 看起来,我遇到的唯一问题是,当用户输入一个字符时,我不太确定如何用用户输入的正确字母替换“\u” import random def hangman(): # setting up the word and spacing possible_words = ["SOFTWARE", "COMPUTER", "RESEARCH", "BLIZZARD"] chosen_word = rando

我是一个极端的初学者,目前正在尝试用Python3创建一个hangman游戏

看起来,我遇到的唯一问题是,当用户输入一个字符时,我不太确定如何用用户输入的正确字母替换“\u”

import random


def hangman():
    # setting up the word and spacing
    possible_words = ["SOFTWARE", "COMPUTER", "RESEARCH", "BLIZZARD"]
    chosen_word = random.choice(possible_words)
    unknown_word = chosen_word
    # del
    print(chosen_word)
    # del
    for char in unknown_word:
        unknown_word = unknown_word.replace(char, "_ ")
    print(unknown_word)
    y = list(unknown_word)
    print()

    # Playing the game
    mistakes = 0
    incorrect_guesses = []
    while mistakes < 5:
        guess = input("Enter a guess:\n")
        length = len(guess)
        # PROBLEMS
        if guess.upper() in chosen_word and length == 1:
            print(unknown_word)
            print()
        # PROBLEMS
        elif length != 1:  # For if more than 2 characters are entered.
            print("Please enter a single character.")
            print()
            continue
        else:  # If the guess isn't in the string
            print("Incorrect guess!")
            mistakes += 1
            print("You have {0} attempts left".format(5 - mistakes))
            incorrect_guesses.append(guess)
            print("You have used {0} so far.".format(incorrect_guesses))
            print()
    print("GAME OVER")


hangman()
随机导入
def hangman():
#设置单词和间距
可能的单词=[“软件”、“计算机”、“研究”、“暴雪”]
选择的单词=随机。选择(可能的单词)
未知单词=所选单词
#德尔
打印(所选单词)
#德尔
对于未知单词中的字符:
未知单词=未知单词。替换(字符“\”)
打印(未知单词)
y=列表(未知单词)
打印()
#玩游戏
错误=0
不正确的猜测=[]
当错误<5时:
猜测=输入(“输入猜测:\n”)
长度=长度(猜测)
#问题
如果所选单词中的guess.upper()和长度==1:
打印(未知单词)
打印()
#问题
elif长度!=1:#如果输入的字符超过2个。
打印(“请输入单个字符”)
打印()
持续
否则:#如果猜测不在字符串中
打印(“猜错了!”)
错误+=1
打印(“您还有{0}次尝试”。格式(5个错误))
猜测不正确。追加(猜测)
打印(“到目前为止您已经使用了{0}。”.format(不正确的猜测))
打印()
打印(“游戏结束”)
刽子手
所以,我需要弄清楚的问题是,如果用户输入正确的字符会发生什么


提前谢谢你

下面的代码将用原始字符替换
\uu

unknown_word = ' '.join([guess.upper() if chosen_word[i] == \
                guess.upper() else c for i, c in enumerate(
                 unknown_word.replace(' ', ''))]
                ) + ' '

你可以做一些像

unknown_word = chosen_word #define the word
current_guess = '_'*len(unknown_word) #get the blanks (if you keep the spaces the loc parameter below will be multiplied by 2)
guessed_letter = input("Enter a guess:\n") #get the guessed letter

if guessed_letter in unknown_word:
    loc=unknown_word.index(guessed_letter)
    current_guess=current_guess[:loc]+guessed_letter+current_guess[loc+1:]

这将在空白处用字母替换空白,如果是在不知道的单词

如另一位所述。如果保留空格,索引位置(loc)将乘以2

编辑-以上仅适用于每个字母出现一次…这将适用于同一字母出现多次的单词:

guessed_letter = input("Enter a guess:\n")
for loc in range(len(unknown_word)):
    if unknown_word[loc] == guessed_letter:
        current_guess = current_guess[:loc]+guessed_letter+current_guess[loc+1:]

首先,对于
不正确的猜测
,我建议使用
set()
而不是
列表
(您需要使用
。添加
而不是
。追加
)。这样,如果用户重复相同的字符,就可以避免重复值

您还可以使用
correct_gusties
变量来跟踪成功的尝试,并使用它们避免在
未知单词中用
替换它们

下面是更改后受影响的代码段的情况:

    incorrect_guesses = set()
    correct_guesses = set()
    while mistakes < 5:
        guess = input("Enter a guess:\n")
        length = len(guess)
        char = guess.upper()
        if char in chosen_word and length == 1:
            correct_guesses.add(char)
            unknown_word = chosen_word
            for char in (c for c in unknown_word if c not in correct_guesses):
                unknown_word = unknown_word.replace(char, "_ ")
            print(unknown_word)
不正确的猜测=set()
更正猜测=设置()
当错误<5时:
猜测=输入(“输入猜测:\n”)
长度=长度(猜测)
char=guess.upper()
如果所选单词中的字符和长度==1:
更正猜测。添加(字符)
未知单词=所选单词
对于中的字符(如果c不在正确猜测中,则c代表未知单词中的c):
未知单词=未知单词。替换(字符“\”)
打印(未知单词)

使用
unknown\u word=“\u”*len(selected\u word)
可以更简单地创建
unknown\u word
。如果当前字符与猜测匹配,请将
未知词
中相应的
\ucode>替换为该字符。但是请记住,
unknown\u word
中的索引是
selected\u word
中索引的两倍,由于每个
\uu
后面都有空格。这将覆盖以前使用
\u
正确猜出的所有字符。问题已经解决:)如果同一个字符在单词中多次出现,这将不起作用。呃,在我的回答中,我忘了您不能分配字符串索引!但是你忘了用
2
:D乘以
loc
。。我有关于乘法的文本,但添加了一条注释,以明确表示感谢@jacalvo!然而,您能否解释一下这两行代码的作用:'对于char in(c对于未知单词中的c,如果c的猜测不正确):unknown\u word=unknown\u word.replace(char,“”)'。我试着把它弄清楚;;
(c代表未知单词中的c,如果c不在正确猜测中)
给出了
未知单词中未在正确猜测中出现的字符列表,也就是说,还没有被猜测的字符列表,然后我们用char变量再次循环该结果,将其替换为
“\
未知单词中
。您可以在在线提供的许多Python教程中找到有关“生成器表达式”的更多信息。