Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...