多种可能的输入

时间:2014-01-28 17:04:45

标签: python input

我只是盯着python,我认为做一个小测验会很有趣。我遇到的问题是,当你考虑到国会大厦的字母和字母时,有很多可能的答案。常见的拼写错误。我想说明这些,但没有'elif'功能的堆栈。有没有办法将所有可能的答案都放到一行?类似于||的东西在C. 代码示例:

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

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

4 个答案:

答案 0 :(得分:4)

你想要的是:

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

我也在降低答案,所以情况无关紧要!

N.B。:建立一个测验,这就是我的方法:

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

答案 1 :(得分:3)

if y.lower() in ["athens", "greece"]:
    print("Correct!")

答案 2 :(得分:1)

烨!

只是做:

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

答案 3 :(得分:1)

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