简体   繁体   English

python在我的字母猜测游戏中遇到困难

[英]python having a hard time at my letter guessing game

I'm trying to do a guessing game in python but I cant figure some stuff out. 我正在尝试用python做一个猜谜游戏,但我无法弄清楚一些东西。 I have to enter a word and it will print a lot of spaces and the person is suppose to guess the word. 我必须输入一个单词,它会打印很多空格,并且该人应该猜出这个单词。 It has to look like this after the word it's been typed. 键入单词后,它必须看起来像这样。 The user will enter a letter and is suppose to look like this (word is dog): 用户将输入一个字母,并假定如下所示(单词是dog):

Enter a letter: a 输入字母:

So far you have: 到目前为止,您已经:

  *** 

if they guess the "o" for example, it will replace the * with an 'o' and so on until you get all the words right. 例如,如果他们猜到了“ o”,它将用*替换*,以此类推,直到所有单词正确为止。 And that's what I can't figure out, can somebody please help me? 那就是我不知道的,有人可以帮我吗? here is my program for 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)

Somebody tried to help me but I just didn't understand how this works because I am new to the program, I want to be able to understand it also. 有人试图帮助我,但我只是不了解它是如何工作的,因为我是该程序的新手,我也希望能够理解它。 Thank you. 谢谢。 Here it was his output, just part of it. 这是他的输出,只是其中的一部分。

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: ")

I'll first explain the solution code you received. 我将首先解释您收到的解决方案代码。 The following code: 如下代码:

[c if c in letters_guessed else "*" for c in word]

generates a list. 生成一个列表。 If you see square brackets [ and ] , then we're list likely creating a list. 如果您看到方括号[和],则我们列出的可能是列表。

now what your friend is using is a generator. 现在您的朋友正在使用的是发电机。 It's a short way of creating a for loop. 这是创建for循环的一种简短方法。 In other words, this would do the same thing. 换句话说,这将做同样的事情。

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)

This gives us the following list: ["*", "o", "g"] 这给出了以下列表:[“ *”,“ o”,“ g”]

What your friend then does, is take that list, and use join: 然后您的朋友所做的就是获取该列表,并使用join:

"".join[ourList]

This is a good way of turning a list of letters back into a string. 这是将字母列表变回字符串的好方法。

See: https://www.tutorialspoint.com/python/string_join.htm 请参阅: https//www.tutorialspoint.com/python/string_join.htm


Your own code has a few problems. 您自己的代码有一些问题。 Is it possible you didn't copy everything? 您是否可能没有复制所有内容?

In python, using tabs effects the way your program runs. 在python中,使用制表符会影响程序的运行方式。 Because you put a tab before 因为您之前放了一个标签

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

you'll run this line every single time, rather than only when the break statement is activated! 您将每次都运行此行,而不是仅在break语句激活时运行!

You have a similar problem with .add! .add也有类似的问题! Try to see if you can't spot it yourself. 尝试看看自己是否发现它。

I also recommend writing 我也建议写

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

because this is much easier to use. 因为这更容易使用。 (for more advanced formatting, look up .format() see https://pyformat.info/ (有关更高级的格式设置,请查找.format(),请参见https://pyformat.info/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM