Python 字母猜谜游戏
Python letter guess game
我想制作刽子手 python 游戏,所以我制作了一个更简单的游戏片段。游戏的想法是猜测随机生成的单词中的字母。这是我到目前为止所拥有的。我在底部列出了我遇到的问题。
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
我遇到的问题:当用户猜一个字母时,他们可以无限次地再次猜它。怎样才能让用户不能重复猜同一个字母?
有没有办法确保用户不会猜出同一个字母?当有几个相同的字母时,这在实际的刽子手游戏中可能会出现问题。任何 help/feedback 感谢,谢谢!
这是一种方法。
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)
示例输出(word 是计算机):
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:
备注:
- 创建一个 guess_list 来记录所有的猜测。
- 每次猜测后,字母都会附加到列表中。
- 当用户重复猜测时,他们会收到警告。我们使用集合,因此仅显示猜测列表中的唯一元素。
代码可以进一步细化,但必须完成工作。
根据单词的不同,如果字母出现不止一次,您可能需要允许多次猜测。 (这可能不是你的要求,对游戏不熟悉。)
方法
您需要的是一个可以跟踪单词中每个字母出现次数的存储。
类似于:
letterCounts = Counter("hello") # word: 'hello' => {'h': 1, 'e': 1, 'l': 2, 'o': 1}
我在这里使用 counter 集合。
然后可以将猜中字母的次数减1
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.
我想制作刽子手 python 游戏,所以我制作了一个更简单的游戏片段。游戏的想法是猜测随机生成的单词中的字母。这是我到目前为止所拥有的。我在底部列出了我遇到的问题。
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
我遇到的问题:当用户猜一个字母时,他们可以无限次地再次猜它。怎样才能让用户不能重复猜同一个字母?
有没有办法确保用户不会猜出同一个字母?当有几个相同的字母时,这在实际的刽子手游戏中可能会出现问题。任何 help/feedback 感谢,谢谢!
这是一种方法。
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)
示例输出(word 是计算机):
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:
备注:
- 创建一个 guess_list 来记录所有的猜测。
- 每次猜测后,字母都会附加到列表中。
- 当用户重复猜测时,他们会收到警告。我们使用集合,因此仅显示猜测列表中的唯一元素。
代码可以进一步细化,但必须完成工作。
根据单词的不同,如果字母出现不止一次,您可能需要允许多次猜测。 (这可能不是你的要求,对游戏不熟悉。)
方法
您需要的是一个可以跟踪单词中每个字母出现次数的存储。
类似于:
letterCounts = Counter("hello") # word: 'hello' => {'h': 1, 'e': 1, 'l': 2, 'o': 1}
我在这里使用 counter 集合。
然后可以将猜中字母的次数减1
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.