Python 多个可能的输入
我刚刚开始学习python,我想做一个小测验会很有趣。我的问题是,考虑到国会大厦的信件和常见的拼写错误,有很多可能的答案。我想解释这些,但没有一堆“elif”函数。有没有办法把所有可能的答案都写在一行上?类似于C中的| |。 代码示例:Python 多个可能的输入,python,input,Python,Input,我刚刚开始学习python,我想做一个小测验会很有趣。我的问题是,考虑到国会大厦的信件和常见的拼写错误,有很多可能的答案。我想解释这些,但没有一堆“elif”函数。有没有办法把所有可能的答案都写在一行上?类似于C中的| |。 代码示例: y = input("Where was the 2004 Olympics held? ") if y == "Athens": print ("Correct!") score = score + 1 但允许'athens'或'Greece
y = input("Where was the 2004 Olympics held? ")
if y == "Athens":
print ("Correct!")
score = score + 1
但允许'athens'
或'Greece'
作为答案您需要的是:
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
你想要的是:
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 in ["athens", "blah", "other",...]:
print ("Correct!")
score += 1
是的
只要做:
if y in ["athens", "blah", "other",...]:
print ("Correct!")
score += 1
谢谢都缩进了,但是复制粘贴好像把它弄糟了。谢谢。都缩进了,但复制粘贴似乎把它弄糟了。