Python 我如何在问题中定义更多选项?
我这样做是因为我的问题包含8个答案,但在输出中,它同时打印正确+20分和错误+0分。我想修一下,请帮帮我Python 我如何在问题中定义更多选项?,python,Python,我这样做是因为我的问题包含8个答案,但在输出中,它同时打印正确+20分和错误+0分。我想修一下,请帮帮我 a1=str(input("4.Name one neighbouring country of India.")) if a1.lower()== ("pakistan"): print("correct +20 Score") score+=20 if a1.lower()==
a1=str(input("4.Name one neighbouring country of India."))
if a1.lower()== ("pakistan"):
print("correct +20 Score")
score+=20
if a1.lower()== ("china"):
print("correct +20 Score")
score+=20
if a1.lower()== ("nepal"):
print("correct +20 Score")
score+=20
if a1.lower()== ("bhutan"):
print("correct +20 Score")
score+=20
if a1.lower()== ("bangladesh"):
print("correct +20 Score")
score+=20
if a1.lower()== ("myanmar"):
print("correct +20 Score")
score+=20
if a1.lower()== ("sri lanka"):
print("correct +20 Score")
score+=20
if a1.lower()== ("maldivs"):
print("correct +20 Score")
score+=20
else:
print("incorrect +0 Score")
您需要使用if-elif-else语法
否则,您将创建独立的IFS语句。因此,如果上面的一个满意,那么最后一个包括else的不会满意。所以你会得到其他的
您需要使用if-elif-else语法
否则,您将创建独立的IFS语句。因此,如果上面的一个满意,那么最后一个包括else的不会满意。因此,您将得到else。在比较时需要使用else语句,而不是多个if。如果需要多个ifs,则应使用elif
但是,将答案存储在列表中并检查列表中是否存在用户的响应将更加清晰和简洁,否则不得分
a1=str(input("4.Name one neighbouring country of India."))
if a1.lower()== ("pakistan"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("china"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("nepal"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("bhutan"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("bangladesh"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("myanmar"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("sri lanka"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("maldivs"):
print("correct +20 Score")
score+=20
else:
print("incorrect +0 Score")
比较时需要使用else语句,而不是多个ifs。如果需要多个ifs,则应使用elif
但是,将答案存储在列表中并检查列表中是否存在用户的响应将更加清晰和简洁,否则不得分
a1=str(input("4.Name one neighbouring country of India."))
if a1.lower()== ("pakistan"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("china"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("nepal"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("bhutan"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("bangladesh"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("myanmar"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("sri lanka"):
print("correct +20 Score")
score+=20
elif a1.lower()== ("maldivs"):
print("correct +20 Score")
score+=20
else:
print("incorrect +0 Score")
使用更好的数据结构可以避免代码重用。这是我的建议
neighbouring = ['pakistan', 'china', 'nepal', 'bhutan', 'bangladesh', 'myanmar', 'sri lanka', 'maldivs']
a1=str(input("4.Name one neighbouring country of India."))
if a1.lower() in neighbouring:
print('correct 20 score')
score += 20
else:
print('incorrect')
使用更好的数据结构可以避免代码重用。这是我的建议
neighbouring = ['pakistan', 'china', 'nepal', 'bhutan', 'bangladesh', 'myanmar', 'sri lanka', 'maldivs']
a1=str(input("4.Name one neighbouring country of India."))
if a1.lower() in neighbouring:
print('correct 20 score')
score += 20
else:
print('incorrect')
这并不能说明OP犯了什么错误。当然,这是解决问题的方法。不过我建议使用集合而不是列表。在列表中查找成员身份将在中运行,而在O1中查找成员身份将在中运行。@RichardNeumann集合创建将在任何情况下运行,因此对于这个没有说明OP在何处出错的用例来说,这并不重要。当然,这是解决问题的方法。不过我建议使用集合而不是列表。在列表中查找成员资格在上运行,而在O1中查找成员是集运行。@RichardNeumann集创建在任何情况下都处于打开状态,因此对于此用例来说都不重要您有一种XY问题。你所问的是,这不是一个优雅的解决方案。你应该问的是如何回答。虽然这不是一个与答案相关的评论,但只是想说这是马尔代夫而不是马尔代夫。你有一种XY问题。你所问的是,这不是一个优雅的解决方案。你们应该问的是如何去做。虽然这不是一个与答案相关的评论,但只是想说这是马尔代夫而不是马尔代夫