未指定输入内容时打印内容

时间:2018-08-05 14:52:19

标签: python python-3.x

很抱歉这个可怕的问题。基本上,我正在用Python制作Yes或No游戏,我需要它来打印一些东西,例如如果他们输入了我已经用每个可接受的单词制作的库中的内容,那么他​​们将“ You Win”。提前TY

q1 = input ("Answer: ")

while q1 == "yes" or "Yes" or "no" or "No":
    print ("Sorry but you answered \"" + q1 + "\" which means you lose!")
    sys.exit ("\nGame Over")
if q1 == ###:
    print ("Well done you have earned yourself a point")
    score +=1
    print ("Your current score is: " + score)

2 个答案:

答案 0 :(得分:0)

input_word = input('Insert a word: ')
list_of_acceptable_words = ['word1', 'word2']
if input_word in list_of_acceptable_words:
    print('You Win')

input_word是包含用户输入内容的字符串,而list_of_acceptable_words是可接受单词的列表

答案 1 :(得分:0)

您可能可以使用内置集合类型从此片段开始:

>>> lib={'Alice','Bob'}
>>> print('Bob' in lib)
True
>>> print('Carol' in lib)
False

然后您可以添加一些代码以更紧密地满足您的需求:

>>> guess='Dave'
>>> print(['You lose...', 'You WIN!'][guess in lib])
You lose...
>>> guess='Alice'
>>> print(['You lose...', 'You WIN!'][guess in lib])
You WIN!

编辑:既然您已经用一些代码片段更新了OP,我可以根据您的需要编写一些代码。

accepted=set(line.strip() for line in open('accepted.txt'))

q1 = input("Answer: ")

while q1.lower() in {"yes", "no"}:
    print ("Sorry but you answered \"" + q1 + "\" which means you lose!")
    sys.exit ("\nGame Over")

if q1 in accepted:
    print ("Well done you have earned yourself a point")
    score +=1
    print ("Your current score is: " + score)

当然,如果您计算出好的答案,您将希望将其放入某个循环中,然后accepted的初始化可以留在外面。