Python中的基本刽子手游戏

Python中的基本刽子手游戏,python,python-3.x,Python,Python 3.x,一周前我开始学习python,现在我正努力专注于非常基础的项目。我被困在“刽子手游戏”中 刽子手 这可能是这6个小项目中最难的一个 项目。这将类似于猜测数字,只是我们是 猜单词。用户需要猜测字母,给用户no 猜错字母的次数超过6次。这意味着你 必须有一个柜台 到目前为止,我已经编写了以下代码: import random name = input("Please enter your name to play Hangman! ") print("Welcome "+name+" !. L

一周前我开始学习python,现在我正努力专注于非常基础的项目。我被困在“刽子手游戏”中


刽子手
这可能是这6个小项目中最难的一个 项目。这将类似于猜测数字,只是我们是 猜单词。用户需要猜测字母,给用户no 猜错字母的次数超过6次。这意味着你 必须有一个柜台

到目前为止,我已经编写了以下代码:

import random

name = input("Please enter your name to play Hangman! ")

print("Welcome "+name+" !. Lets play Hangman.")

wrong_attempt = int(input("How many incorrect attempts do you want ? "))

f = open('words.csv',"r")

secret_word = f.readline()
#print(secret_word)
guesses = ''
while wrong_attempt > 0 :
    c = 0
    letter = input("\nGuess a word : ")
    for char in secret_word :
        if char == letter :
            print(char,end = '')
        else :
            print('*',end = '')
            c += 1
            if c == len(secret_word) :
                wrong_attempt -= 1
                print("Bad Luck. You have ",wrong_attempt," attempts left.")
                print("The secret word is ",secret_word)

if wrong_attempt == 0 :
    print("You LOSE.")
我现在得到的输出:

Please enter your name to play Hangman! ss Welcome ss !. Lets play Hangman. How many incorrect attempts do you want ? 2

Guess a word : c c******** Guess a word : o
*o******* Guess a word : m
**m****** Guess a word : z
*********Bad Luck. You have  1  attempts left.

Guess a word : d
*********Bad Luck. You have  0  attempts left. You LOSE. The secret word is  computer
预期产出:

Please enter your name to play Hangman! ss Welcome ss !. Lets play Hangman. How many incorrect attempts do you want ? 2

Guess a word : c c******** Guess a word : o co******* Guess a word : m com****** Guess a word : z
*********Bad Luck. You have  1  attempts left.

Guess a word : d
*********Bad Luck. You have  0  attempts left. You LOSE. The secret word is  computer

此外,我是新的stackoverflow当谈到张贴问题。任何建议都将不胜感激。

我能看出的唯一区别是预期代码中的这一行:

猜一个词:c c**********猜一个词:o co*******猜一个词:m com*******猜一个词:z

而你的是:

猜一个词:c c*******猜一个词:o

*猜一个词:m **m*******猜一个词:z

因此,请尝试在结尾处\t而不是在此行中\n创建选项卡:

    letter = input("\nGuess a word : ")

我想这就是你的意思:

基本上,我所做的是创建一个列表并用astrik填充项目,然后在字符检查时,我弹出第n个astrik并用字母替换它

import random
import sys
name = input("Please enter your name to play Hangman! ")

print("Welcome "+name+" !. Lets play Hangman.")

wrong_attempt = int(input("How many incorrect attempts do you want ? "))

f = open('words.csv',"r")

secret_word = f.readline()
#print(secret_word)
guesses = ''
word = []
for i in secret_word.strip("\n"):
    word.append("*")
while wrong_attempt > 0 :
    word_str = ''.join([str(elem) for elem in word])

    #print(word_str)
    c = 0
    letter = input("\nGuess a word : ")
    for char in secret_word :
        if char == letter :
            word.pop(c)
            word.insert(c,char)
            c += 1
        else :
            c += 1
            if c == len(secret_word) :
                wrong_attempt -= 1
                print("Bad Luck. You have ",wrong_attempt," attempts left.")
                print("The secret word is ",secret_word)
    c=0

    word_str = ''.join([str(elem) for elem in word])
    print(word_str)
    if word_str == secret_word.strip("\n"):
        print("Yeee, you won")
        sys.exit()
if wrong_attempt == 0 :
    print("You LOSE.")

在您的输出中,您不想要什么不同?在您的代码中,您没有考虑到迄今为止正确猜测的字母,只有在本次循环中猜测的字母。我建议您创建一个字符串,用
******
(长度正确)初始化,并在猜测过程中实现。这也会让你知道玩家是否赢得了比赛。我还建议您在
trunt\u left
中将变量重命名为`error\u trunt`,它的名称是误读的。如果您阅读了代码,那么您会看到每一行都在单独的一行上。代码中的问题是,每次猜测都只能看到该尝试中正确的字母。