简体   繁体   English

随机猜词游戏

[英]Random word guessing game

I want to create a word guessing game where the program randomly selects a word from my word list and the user has to guess the word.我想创建一个猜词游戏,程序从我的单词列表中随机选择一个单词,用户必须猜测这个单词。

  • User can only guess one letter at a time.用户一次只能猜出一个字母。
  • User is only allowed to have 6 failed guesses.用户只允许有 6 次失败的猜测。 (Loses when 6 failed attempts are used). (使用 6 次失败尝试时失败)。
  • User wins if he guess the complete word before 6 failed attempts is used.如果用户在使用 6 次失败尝试之前猜到了完整的单词,则他获胜。

So I'm facing quite a number of problems with my program:所以我的程序面临很多问题:

  1. How do I make the guessed letter stay on the blanks when it goes to the next round of guess?如何让猜出的字母在进入下一轮猜测时留在空白处?
  2. If the word has two of the same letters, how do I display it on my blanks too?如果单词有两个相同的字母,我该如何在空白处显示它?
  3. How do I show all the user's missed letters for each round?如何在每一轮中显示所有用户错过的字母?

Here's what I did so far:这是我到目前为止所做的:

import random

wordlist = ['giraffe','dolphin',\
            'pineapple','durian',\
            'blue','purple', \
            'heart','rectangle']

#Obtain random word
randWord = random.choice(wordlist)

#Determine length of random word and display number of blanks
blanks = '_ ' * len(randWord)
print ()
print ("Word: ",blanks)


#Set number of failed attempts
count = 6

#Obtain guess
while True:
    print ()
    guess = input ("Please make a guess: ")   
    if len(guess) != 1:
        print ("Please guess one letter at a time!")
    elif guess not in 'abcdefghijklmnopqrstuvwxyz':
       print ("Please only guess letters!")

#Check if guess is found in random word
    for letters in randWord:
        if guess == letters:
            letterIndex = randWord.index(guess)
            newBlanks = blanks[:letterIndex*2] + guess + blanks[letterIndex*2+1:]
            print ("Guess is correct!")
        else:
            count -=1
            print ("Guess is wrong! ", count, " more failed attempts allowed.")
    print() 
    print("Word: ",newBlanks) 

The results I hope to obtain (for randWord 'purple'):我希望获得的结果(对于 randWord 'purple'):

Word: _ _ _ _ _ _ 
Missed: 
Please make a guess: l
Guess is correct!


Word: _ _ _ _ l _ 
Missed:
Please make a guess: z
Guess is wrong! 5 more failed attempts allowed.


Word: _ _ _ _ l _ 
Missed: z
Please make a guess: o
Guess is wrong! 4 more failed attempts allowed.


Word: _ _ _ _ l _ 
Missed: z, o
Please make a guess: p
Guess is correct!


Word: p _ _ p l _ 
Missed: z, o
Please make a guess: e
Guess is correct!


Word: p _ _ p l e 
Missed: z, o
Please make a guess: r
Guess is correct!


Word: p _ r p l e 
Missed: z, o
Please make a guess: u
Guess is correct!


Word: p u r p l e 
YOU WON!

How do I make the guessed letter stay on the blanks when it goes to the next round of guess?如何让猜出的字母在进入下一轮猜测时留在空白处?

Just store string containing guessed letter and blanks for next round.只需存储包含下一轮猜测字母和空格的字符串。 You are recalculating it every time from wordlist (it can be also recalculated every time, but then you need to modify your search function for letters, see answer 2)您每次都从wordlist重新计算它(也可以每次都重新计算,但是您需要修改您的字母搜索功能,请参阅答案 2)

If the word has two of the same letters, how do I display it on my blanks too?如果单词有两个相同的字母,我该如何在空白处显示它?

Modify your search loop, it should continue searching after first matching letter is found.修改您的搜索循环,它应该在找到第一个匹配的字母后继续搜索。

letterIndex = randWord.index(guess) will return only first occurrence of guess in string. letterIndex = randWord.index(guess)将仅返回字符串中第一次出现的猜测。

How do I show all the user's missed letters for each round?如何在每一轮中显示所有用户错过的字母?

Store them in separate string or list.将它们存储在单独的字符串或列表中。 So you can just print it every time.所以你可以每次都打印它。

Instead of reusing the newBlanks string from the previous round, I suggest building it anew with join and a simple list comprehension, using a string guessed holding all the guesses, like here .相反重用的newBlanks从上一轮的字符串,我建议建立它与重新join和一个简单的列表中理解,使用字符串guessed拿着所有的猜测,就像这里 Also note that your check for correct/incorrect letters does not work this way, but will decrease count for each letter of the word that is not the guessed letter.另请注意,您对正确/不正确字母的检查不会以这种方式工作,但会减少不是猜测字母的单词的每个字母的count Use if guess in randWord: instead.使用if guess in randWord:代替。 Also, you can use count for the condition of the while loop, and continue with the next iteration of the loop if guess is not a single letter.此外,您可以使用count作为while循环的条件,如果guess不是单个字母,则continue循环的下一次迭代。

Putting it all together, your code could then look like this:将它们放在一起,您的代码可能如下所示:

guessed = ""
while count >= 0:
    guess = input ("Please make a guess: ")   
    # ... check guess, continue if not a letter
    guessed += guess

    if guess in randWord:
        # ... print 'correct', else 'not correct', decrease count

    newBlanks = " ".join(c if c in guessed else "_" for c in randWord)
    print("Word: ",newBlanks) 

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

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