多个可能的输入

2024-05-28 23:43:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始关注python,我觉得做个小测验会很有趣。我的问题是,当你考虑到国会大厦的信件和常见的拼写错误时,有很多可能的答案。我想解释这些,但没有一堆“elif”函数。有没有办法把所有可能的答案都写在一行上?类似于C中的| |的东西。 代码示例:

y = input("Where was the 2004 Olympics held? ")
if y == "Athens":
    print ("Correct!")
    score = score + 1

但允许'athens''Greece'作为答案


Tags: the函数答案代码示例inputwherescore
3条回答

是的!你知道吗

只要做:

if y in ["athens", "blah", "other",...]:
    print ("Correct!")
    score += 1

你想要的是:

answer = input("Where was the 2004 Olympics held? ")
if answer.lower() in ("athens", "greece"):
    print ("Correct!")
    score = score+1

我也在降低答案,所以这个案子不重要!你知道吗

注:要建立一个测验,我会这样做:

qas = {
    'Where was the 2004 Olympics held?': ['athens', 'greece'],
    'What is the answer of the question about life, the universe and everything?': ['42', 'forty-two'] 
    …
}

for q, a in qas:
    ans = input(q)
    if ans.lower() in a:
        print("Correct!")
        score += 1
if y.lower() in ["athens", "greece"]:
    print("Correct!")

相关问题 更多 >

    热门问题