1
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.

Spino_PPG
  • 51
  • 1
  • 4
  • 1
    You have a sort of XY problem. What you are asking is something like [Elif-row without else python](https://stackoverflow.com/q/24230930/2745495), which isn't an elegant solution (IMHO). What you should be asking instead is how to [Testing user input against a list in python](https://stackoverflow.com/q/3944655/2745495). – Gino Mempin Jan 11 '21 at 02:11
  • Though this is not a comment related to answer but just wanted to say it's `Maldives` not `Maldivs` – CopyrightC Jan 11 '21 at 02:17

3 Answers3

4

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')
PacketLoss
  • 5,561
  • 1
  • 9
  • 27
0

Using a better data structure can save you from code reuse. Here is my suggestion

score = 0
questions = [
    {'''...'''},
    {'''...'''},
    {'''...'''},
    {
        "text": "Name one neighboring country of India.",
        "answers": [
            "pakistan",
            "china",
            "nepal",
            "bhutan",
            "bangladesh",
            "myanmar",
            "sri lanka",
            "maldivs"
        ],
        "points": 20
    },
]

# for idx, question in enumerate(questions):
idx, question = 3, questions[3]
user_answer = str(input(f"{idx + 1}. " + question['text'])).lower()
if user_answer in question["answers"]:
    pnt_gain = question["points"]
    score += pnt_gain
    print(f"correct +{pnt_gain} Score")
else:
    print("incorrect +0 Score")
James
  • 387
  • 1
  • 11
-1
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")

You need to use the if elif else syntax.

Otherwise you create independent ifstatement. So if one on top is satisfied, the last one which includes the else won't. So you'll get the else.

Synthase
  • 5,849
  • 2
  • 12
  • 34