Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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_Python 3.x - Fatal编程技术网

python在我的猜信游戏中遇到了困难

python在我的猜信游戏中遇到了困难,python,python-3.x,Python,Python 3.x,我想用python做一个猜谜游戏,但我想不出什么东西。我必须输入一个单词,它会打印出很多空格,人们应该猜这个单词。在输入单词后,它必须是这样的。用户将输入一个字母,并假设如下所示(单词为dog): 输入一个字母:a 到目前为止,你已经: *** 例如,如果他们猜到了“o”,它会用“o”来代替*等等,直到你把所有的单词都猜对为止。这就是我不明白的,有人能帮我吗?以下是我的far计划: def main(): letters_guessed = set() # Read wo

我想用python做一个猜谜游戏,但我想不出什么东西。我必须输入一个单词,它会打印出很多空格,人们应该猜这个单词。在输入单词后,它必须是这样的。用户将输入一个字母,并假设如下所示(单词为dog):

输入一个字母:a

到目前为止,你已经:

 *** 
例如,如果他们猜到了“o”,它会用“o”来代替*等等,直到你把所有的单词都猜对为止。这就是我不明白的,有人能帮我吗?以下是我的far计划:

def main():
    letters_guessed = set()

    # Read word 
    word = input("\n Please Enter a word: ")
    # print 100 spaces
    print("\n" * 100)
    # Storing the length of the word
    word_length = len(word)
    guess = '*' * word_length


    while True:
        print ("So far you have: ", 

        guess_letter = input ("Please guess a letter: ")
        if len(guess_letter) != 1:
            print ("Please guess one letter at a time")
        if guess_letter in letters_guessed:
            print ("\n You already guessed that letter, please try again")
        letters_guessed.add(guess_letter)

        if set(word) == set(letters_guessed):
            break

    print("You won, the word is " % word)
有人试图帮助我,但我只是不明白这是怎么回事,因为我是新来的程序,我想能够理解它也。非常感谢。这是他的作品,只是其中的一部分

while True:
    print ("So far you have: ", "".join([c if c in letters_guessed else "*" 
for c in word]))
    guess_letter = input ("Please guess a letter: ")

我将首先解释您收到的解决方案代码。以下代码:

[c if c in letters_guessed else "*" for c in word]
生成一个列表。如果您看到方括号[和],那么我们很可能正在创建一个列表

现在你朋友用的是发电机。这是创建for循环的一种简单方法。换句话说,这将做同样的事情

word = "dog"
letter_guessed = "go"
ourList = list() #new list
for letter in word: #we check every letter in the word
    if letter in letter_guessed: #if our letter has been guessed
        ourList.append(letter) # we can show that letter in our word
    else:
        ourList.append("*") # if the letter has not been guessed, we should 
        # not show that letter in our word, and thus we change it to a *
print(ourList)
这给了我们以下列表:[“*”,“o”,“g”]

然后,您的朋友会做的是获取该列表并使用“加入”:

"".join[ourList]
这是将字母列表转换回字符串的好方法

见:


您自己的代码有一些问题。有没有可能你没有把所有的东西都抄下来

在python中,使用制表符会影响程序的运行方式。因为你把账单放在

print("You won, the word is " % word)
您将每次运行这一行,而不仅仅是在激活break语句时

您也有类似的问题。添加!试着看看你自己是否能发现它

我也推荐写作

print("You won, the word is " + word)

因为这更容易使用。(有关更高级的格式设置,请查阅.format(),请参见

您的代码运行不正常,
print(“到目前为止:”,
此行似乎不完整,您的“您将”消息在while循环中打印,