如何让代码问同样的问题,直到用户使用Python正确地猜出单词?

ryevplcw  于 2022-10-30  发布在  Python
关注(0)|答案(1)|浏览(119)
from itertools import chain, repeat

word = "trick or treat"     # This is the phrase a user needs to guess
prompts = chain(
    ["You meet a whitch with a cauldron full of candies, what do you say? \n"], repeat("Noope! "))
replies = map(input, prompts)
valid_response = next(filter(word.__contains__, replies))
print(valid_response)

错误:

"Chain has not attribute %s" % ident
                        ^
SyntaxError: invalid syntax
dsekswqp

dsekswqp1#

您可以只使用while循环:

word = "trick or treat"
while True:
    reply = input("You meet a witch with a cauldron full of candies, what do you say? \n")
    if word not in reply:
        print("Noope!")
    else:
        print("Correct!")
        break

相关问题