gpt4 book ai didi

python - Hangman 游戏 - 如果输入的字母序列困惑,则无法将输入的字母与单词匹配

转载 作者:太空宇宙 更新时间:2023-11-03 14:35:58 25 4
gpt4 key购买 nike

下面是我要构建的内容的概述:

  1. 从用户那里获取关于字长的输入
  2. 根据用户输入的词长从文本文件中提取词
  3. 从用户输入中获取尝试次数
  4. 将单词显示为 *
  5. 从中获取提示字母输入用户
  6. 运行游戏

    • 首先显示 * 中的单词
    • 显示剩余的尝试次数
    • 提示输入下一个字母
      • 如果输入与单词匹配
        • 打印“你猜对了字母”
        • 在适当的空格处替换字母中单词中的 * 并打印
        • 打印剩余的尝试次数
        • 打印猜出的字母
        • 提示输入下一个字母*这一直持续到所有正确的字母都被猜到为止
        • 打印“你赢了”
      • 如果输入与单词不匹配
        • 打印“你猜错了字母”
        • 打印*中的单词
        • 打印剩余的尝试次数
        • 打印猜出的字母
        • 提示输入下一个字母*这一直持续到剩余的 attepmt 为 0
        • 打印“你输了”
      • 如果尝试次数为 0
        • 打印“没有尝试了”
        • 写出正确的单词

只有当输入的字母是常量时,代码才有效。

假设游戏词是“雨”,只有当用户输入“R”、“A”、“I”、“N”时,代码才会起作用。

如果输入的字母乱七八糟,例如“A”、“R”、“I”、“N”,代码将无法正常工作。

我相信它可以通过使用枚举的迭代来实现,但我不确定如何实现。

这是我的代码:

import random

WORDS = "wordlist.txt"

"""Getting Length input from user and selecting random word from textfile"""
def get_word_length_attempt():
max_word_length = int(input("Provide max length of word [4-16]: "))
current_word = 0
word_processed = 0
with open(WORDS, 'r') as f:
for word in f:
if '(' in word or ')' in word:
continue
word = word.strip().lower()
if len(word) > max_word_length:
continue
if len(word) < 4:
continue
word_processed += 1
if random.randint(1, word_processed) == 1:
current_word = word
return current_word


"""Getting input of number of attempts player wants to have"""
def get_num_attepmts():
num_attempt = int(input("Provide number of attempts you want: "))
return num_attempt

"""Displaying word in *"""
def display_word_as_secret():
display_word = '*' * len(get_word_length_attempt())
print(display_word)

"""Getting hint letter from user input"""
def get_user_letter():
user_letter = input("Enter letter: ").lower()
if len(user_letter) != 1:
print("Please Enter single letter")
else:
return user_letter

"""Starting Game"""
def start_game():
game_word = get_word_length_attempt()
attempts_remaining = get_num_attepmts()
print('Your Game Word: ' + game_word)
print('Your Game Word: ' + '*'*len(game_word))
print('Attempts Remaining: ' + str(attempts_remaining))
guessed_word = []

while attempts_remaining > 0:
next_letter = get_user_letter()
if next_letter in game_word:
print('You guessed correct')
guessed_word.append(next_letter)
print('Your Game Word: ' + game_word)
print('Your Game Word: ' + '*' * len(game_word))
print('Attempts Remaining: ' + str(attempts_remaining))
correct_word = "".join(guessed_word)
print(guessed_word)
if correct_word == game_word:
print('you won')
break
else:
print('The letter in not in the game word')
attempts_remaining -= 1
print('Your Game Word: ' + game_word)
print('Your Game Word: ' + '*' * len(game_word))
print('Attempts Remaining: ' + str(attempts_remaining))

else:
print('no attempts left')
print('You Lost')
print('The Word is: ' + game_word)


start_game()

最佳答案

您正在按照用户输入的顺序从猜测的字母构造 correct_word。猜测的字符串 'ARIN' 不等于 'RAIN'

相反,您需要进行不关心顺序的比较。最简单的解决方法是更改​​

if correct_word == game_word:

if set(correct_word) == set(game_word):

因为无论顺序如何,集合都会根据内容进行比较。它还将更好地处理重复的字母,例如 'letterbox' 将被视为字母的集合 {'b', 'e', 'l', 'o', “r”、“t”、“x”

您不妨首先将猜出的字母存储为一个集合,因为无论如何,多次猜测同一个字母是没有意义的。

关于python - Hangman 游戏 - 如果输入的字母序列困惑,则无法将输入的字母与单词匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58512387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com