我必须创建一个游戏,其中计算机选择一个随机的单词,而玩家必须猜测该单词。电脑会告诉玩家单词中有多少个字母。然后,玩家有五次机会问单词中是否有字母。计算机只能使用"yes"
或"no"
进行响应。然后,玩家必须猜出单词。
我只有:
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "truck" , "doom" , "mayonase" ,"flying" ,"magic" ,"mine" ,"bugle")
word = random.choice(WORDS)
print(len(word))
correct = word
guess = input("\nYour guess: ")
if guess != correct and guess != "" :
print("No.")
if guess == correct:
print("Yes!\n")
我不知道该怎么办。
最佳答案
您正在寻找类似下面的内容
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "truck" , "doom" , "mayonase" ,"flying" ,"magic" ,"mine" ,"bugle")
word = random.choice(WORDS)
correct_answer = word
max_guesses = 5
print("Word length:", len(word))
print("Attempts Available:", max_guesses)
for guesses in range(max_guesses):
guess = input("\nEnter your guess, or a letter: ")
if guess == correct_answer:
print("Yay! '%s' is the correct answer.\n" % guess)
break
elif guess != "":
if guess[0] in correct_answer:
print("Yes, '%s' appears in the answer" % guess[0])
else:
print("No, '%s' does not appear in the answer" % guess[0])
else:
print("\nYou ran out of maximumum tries!\n")
关于python - 如何从字符串中提取信息并输出?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19081750/