我已经很多年没有接触过Python了,也从来没有真正学过它,但我正在尝试制作一个简单的问题生成器,用于在我的公司练习面试。我的公司使用大量但明确的问题库,能够随机输出它们将有助于面试准备。这部分代码位于末尾,如果您输入“Y”,则会生成另一个问题,或者如果输入其他任何内容,它会给出鼓舞人心的消息。如果我想让它接受“y”或其他东西,我需要添加什么?我尝试了一些方法,但我无法弄清楚。
selected_item = random.choice(lp_questions)
print(selected_item, "\n", "\n")
answer = (input("Would you like another? If so, type 'Y', if not, press any other button"))
if answer == ('Y'):
print ("\n")
continue
else:
print ("\n",
"You have got this interview in the bag! Come back for more practice if you need it and find someone to do a mock interview with!")
break
由于您的代码中有
continue
和 break
,我假设您已经将其放入 while
循环中,并仅提供代码的答案检查部分。
"""
we first lower-case the answer by 'answer.lower()', this helps dealing with 'Y'/'y' or 'Yes'/'YEs'/'yEs'
then check if the lower-case answer is in the acceptable answer set
you can create a set by using {} or 'set()'
"""
if answer.lower() in {"y", "yes", "yeah", "true", "great", "of course"}:
# ... do something when answer is accepted
continue
else:
# ... do something when answer not accepted
break