gpt4 book ai didi

python - 如何将用户输入的字符串与列表中单词的字符(单个字母)进行比较?

转载 作者:行者123 更新时间:2023-12-01 08:29:57 25 4
gpt4 key购买 nike

我正在尝试用 python 创建一个刽子手游戏。我知道还有很多工作需要完成,但我正在尝试找出游戏的主要组成部分,即如何将用户输入的字符串(一个字母)与三个随机选择的单词之一中的字母进行比较。

import random

print("Welcome to hangman,guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
guess = input(str("Enter your guess:"))
guess_left = 10
guess_subtract = 1
if guess == "":
guess_left = guess_left - guess_subtract
print("you have" + guess_left + "guesses left")

最佳答案

我认为你需要一个骨架,然后花一些时间来改进游戏。

import random

print "Welcome to hangman, guess the five letter word"

words = ["china", "ducks", "glass"]
correct_word = (random.choice(words))

trials = 10

for trial in range(trials):
guess = str(raw_input("Enter character: "))

if (len(guess) > 1):
print "You are not allowed to enter more than one character at time"
continue

if guess in correct_word:
print "Well done! '" + guess + "' is in the list!"
else:
print "Sorry " + guess + " does not included..."

您的下一步可以打印出类似 c_i__ 的内容以及剩余的试验次数。玩得开心:)

完成实现后,花一些时间并使用函数重新实现它。

关于python - 如何将用户输入的字符串与列表中单词的字符(单个字母)进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53951578/

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