JavaScript
x
28
28
1
a1=str(input("4.Name one neighbouring country of India."))
2
if a1.lower()== ("pakistan"):
3
print("correct +20 Score")
4
score+=20
5
if a1.lower()== ("china"):
6
print("correct +20 Score")
7
score+=20
8
if a1.lower()== ("nepal"):
9
print("correct +20 Score")
10
score+=20
11
if a1.lower()== ("bhutan"):
12
print("correct +20 Score")
13
score+=20
14
if a1.lower()== ("bangladesh"):
15
print("correct +20 Score")
16
score+=20
17
if a1.lower()== ("myanmar"):
18
print("correct +20 Score")
19
score+=20
20
if a1.lower()== ("sri lanka"):
21
print("correct +20 Score")
22
score+=20
23
if a1.lower()== ("maldivs"):
24
print("correct +20 Score")
25
score+=20
26
else:
27
print("incorrect +0 Score")
28
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.
JavaScript
1
10
10
1
neighbouring = ['pakistan', 'china', 'nepal', 'bhutan', 'bangladesh', 'myanmar', 'sri lanka', 'maldivs']
2
3
a1=str(input("4.Name one neighbouring country of India."))
4
5
if a1.lower() in neighbouring:
6
print('correct 20 score')
7
score += 20
8
else:
9
print('incorrect')
10