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")
I made this cause my question contains 8 answers but in output, it prints both “correct +20 Score” and “incorrect +0 Score”. I want to fix it Please help me.
Advertisement
Answer
You need to use else statements when comparing, not multiple ifs. If you need multiple ifs you should use elif.
https://www.tutorialspoint.com/python/python_if_else.htm
However, it would be much more clear and concise to store your answers in a list and check if the users response exists in the list, else no 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')