随机猜词游戏

2024-10-02 06:37:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个猜词游戏,程序从我的单词列表中随机选择一个单词,用户必须猜词。

  • 用户一次只能猜一个字母。
  • 用户只能有6个失败的猜测。(使用6次失败的尝试时失败)。
  • 如果用户在使用6次失败的尝试之前猜中了完整的单词,那么他将获胜。

所以我的程序面临很多问题:

    当猜到下一轮猜测时,我如何使猜测的字母停留在空白处?
  1. 如果这个单词有两个相同的字母,我该如何在空白处显示它呢?
  2. 如何显示每一轮中用户丢失的所有信件?

我到目前为止所做的是:

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) 

我希望得到的结果(对于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!

Tags: 用户makeisrandom单词wordprintplease
3条回答

与其重用上一轮的newBlanks字符串,我建议重新构建它,使用join和简单的列表理解,使用一个包含所有猜测的字符串guessed,比如here。还要注意,您对正确/不正确字母的检查不会这样工作,但会减少单词中每个不是猜测字母的字母的count。改用if guess in randWord:。另外,您可以使用count作为while循环的条件,并且如果guess不是单个字母,则可以使用continue作为循环的下一次迭代。

总而言之,您的代码可能如下所示:

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) 

How do I make the guessed letter stay on the blanks when it goes to the next round of guess?

只需为下一轮存储包含猜测字母和空格的字符串。您每次从wordlist开始重新计算(也可以每次重新计算,但之后需要修改字母搜索功能,请参见答案2)

If the word has two of the same letters, how do I display it on my blanks too?

修改您的搜索循环,它应该在找到第一个匹配的字母后继续搜索。

letterIndex = randWord.index(guess)将只返回字符串中guess的第一次出现。

How do I show all the user's missed letters for each round?

将它们分别存储在单独的字符串或列表中。所以你每次都可以打印出来。

与其重用上一轮的newBlanks字符串,我建议重新构建它,使用join和简单的列表理解,使用一个包含所有猜测的字符串guessed,比如here。还要注意,您对正确/不正确字母的检查不会这样工作,但会减少单词中每个不是猜测字母的字母的count。改用if guess in randWord:。此外,您可以使用count作为while循环的条件,并且如果guess不是单个字母,则可以使用continue作为循环的下一次迭代。

总而言之,您的代码可能如下所示:

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) 

相关问题 更多 >

    热门问题