Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

python - How may I define more option in my question?

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.

question from:https://stackoverflow.com/questions/65660474/how-may-i-define-more-option-in-my-question

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...