简体   繁体   English

Python字母猜谜游戏

[英]Python letter guess game

I want to create the hangman python game, so I created a simpler piece of the game.我想创建刽子手 python 游戏,所以我创建了一个更简单的游戏。 The idea of the game is to guess the letters in a randomly generated word.游戏的想法是猜测随机生成的单词中的字母。 Here is what I have so far.这是我到目前为止所拥有的。 I listed the problem and questions I have at the bottom.我在底部列出了我遇到的问题。

import random


words = ['holiday', 'american', 'restaurant', 'computer', 'highschool', 'programming']
random_word = random.choice(words)
count = 0


while True:                                     #repeatedly asks you to guess the letter in a random word
    guess = str(input("Guess a letter:  "))
    guess = guess.lower()


    if guess in random_word:             #checks if the letter you input is in the random generated word
        print("Yay, its in the word")
      
    
    else:                          
        count += 1
        print("Not in the word, attempts: %d" % count)
        
        if count > 5:                   
            print("You have reached max attempts")
            print("Sorry, but hangman died! You lose")
            break
        else:
            continue

The problem I have: When the user guesses a letter, they can guess it again infinite times.遇到的问题:当用户猜一个字母时,他们可以无限次地再猜一次。 How can I make it so that the user can't repeatedly guess the same letter?我怎样才能让用户不能重复猜同一个字母?

Is there a way to make sure the user doesn't guess the same letter?有没有办法确保用户不会猜到同一个字母? This could be a problem in an actual hangman game when there are several letters that are the same.当有几个相同的字母时,这可能是实际刽子手游戏中的一个问题。 Any help/feedback appreciated, thanks!感谢任何帮助/反馈,谢谢!

Here is one way of doing it.这是一种方法。

import random


words = ['holiday', 'american', 'restaurant', 'computer', 'highschool', 'programming']
random_word = random.choice(words)
count = 0
guess_list = []

while True:                                     #repeatedly asks you to guess the letter in a random word
    guess = str(input("Guess a letter:  "))
    guess = guess.lower()

    if guess not in guess_list:
        if guess in random_word:             #checks if the letter you input is in the random generated word
            print("Yay, its in the word")

        else:                          
            count += 1
            print("Not in the word, attempts: %d" % count)

            if count > 5:                   
                print("You have reached max attempts")
                print("Sorry, but hangman died! You lose")
                break
                
    else:
        print("You have already guessed {}. Try again".format(guess))
        print(set(guess_list))
        
    guess_list.append(guess)

Sample output (word is computer):示例输出(单词是计算机):

Guess a letter:  c
Yay, its in the word
Guess a letter:  e
Yay, its in the word
Guess a letter:  e
You have already guessed e. Try again
{'e', 'c'}
Guess a letter:  e
You have already guessed e. Try again
{'e', 'c'}
Guess a letter:  w
Not in the word, attempts: 1
Guess a letter:  m
Yay, its in the word
Guess a letter:  m
You have already guessed m. Try again
{'w', 'm', 'e', 'c'}
Guess a letter:  

Notes:笔记:

  1. A guess_list is created that keeps record of all the guesses.创建一个 guess_list 来记录所有的猜测。
  2. After each guess the letter is appended to the list.每次猜测后,字母都会附加到列表中。
  3. When the user repeats the guess they are warned.当用户重复猜测时,他们会收到警告。 We use a set so only the unique elements in the guess list are displayed.我们使用一个集合,所以只显示猜测列表中的唯一元素。

The code can be refined further, but must do the job.代码可以进一步细化,但必须完成这项工作。

Depending on the word, you may need to allow multiple guesses if the letter appears more than once.根据单词的不同,如果字母出现多次,您可能需要允许多次猜测。 (this may not be your requirement. Not familiar with the game.) (这可能不是您的要求。不熟悉游戏。)

Approach方法

What you need is a store that keeps track of the number of occurrences of each letter in the word.您需要的是一个跟踪单词中每个字母出现次数的存储。

Something like:就像是:

letterCounts = Counter("hello") # word: 'hello' => {'h': 1, 'e': 1, 'l': 2, 'o': 1}

I'm using the counter collection here.我在这里使用计数器集合。

Then you can decrement the count of the guessed letter然后你可以减少猜测字母的数量

if guesses_letter in list(letterCounts.elements()):
    if (letterCounts[guessed_letter] > 0):
        letterCounts[guessed_letter] -= 1
    else:
        # the letter can't be repeated now.  do the needful here.

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

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