Reputation: 1
I am using a set of questions list and answer list so if user answers the correct answer which is given in the list the score will increase else a prompt will be shown stating your answer is wrong and the score. So I gave a wrong answer and was expecting the loop to stop (as I had added a break statement at the end but the loop kept on displaying the questions and asked for user input to answer and after all the questions were over exited the loop, the score was displayed correctly but instead of exiting when wrong and was given it kept on asking the questions)
I am using a set of questions list and answer list so if user answers the correct answer which is given in the list the score will increase else a prompt will be shown stating your answer is wrong and the score. So I gave a wrong answer and was expecting the loop to stop (as I had added a break statement at the end but the loop kept on displaying the questions and asked for user input to answer and after all the questions were over exited the loop, the score was displayed correctly but instead of exiting when wrong and was given it kept on asking the questions)
My Code:
`import random
counter = 0
score = 0
incorrect = 0
name = input("What is your name?")
print("Swagat hai "+name+" aapka KBC mein!")
questions_list = ["Who won the World cup 2022?","The International Literacy day is observed on:", "Bahubali festival is related to:","Which day is observed as the world standards day (Enter month then day)"]
answer_list = ["Argentina","Sep 8", "Jainism","Oct 14"]
inputs=[]
idx_question = list(enumerate(questions_list))
idx_answer = list(enumerate(answer_list))
random.shuffle(idx_question)
for idxq,question in idx_question:
print(question)
ans = input("What is the answer? ")
inputs.append(ans)
for idxa,answer in idx_answer:
if idxq==idxa and ans == answer:
print("Sahi jawab")
counter = counter +1
score = score + (counter*10000)
elif idxq==idxa and ans!=answer:
incorrect = incorrect +1
break
print("Arrey Galat jawab...Aap sirf jeete:",score)
print("End")`
o/p
What is your name?JIO
Swagat hai JIO aapka KBC mein!
Bahubali festival is related to:
What is the answer? Jainism
Sahi jawab
The International Literacy day is observed on:
What is the answer? 12 Jan
Which day is observed as the world standards day (Enter month then day)
What is the answer? 12 Oct
Who won the World cup 2022?
What is the answer? hum
Arrey Galat jawab...Aap sirf jeete: 10000
End
print("Arrey Galat jawab...Aap sirf jeete:",score) print("End")
o/p What is your name?JIO Swagat hai JIO aapka KBC mein! Bahubali festival is related to: What is the answer? Jainism Sahi jawab The International Literacy day is observed on: What is the answer? 12 Jan Which day is observed as the world standards day (Enter month then day) What is the answer? 12 Oct Who won the World cup 2022? What is the answer? hum Arrey Galat jawab...Aap sirf jeete: 10000 End
In the above application, I have pre-defined a list of questions and their answers. So a question is asked to the user randomly from the list as i have used the random operator, if the answer matches with the answer list the score will be incremented else if the answer is wrong then the program should exit (in elif) as break statement is also used and print the End statement and the statement above it
Upvotes: 0
Views: 32
Reputation: 445
Your program keeps running even after incorrect answer because your break
statement lies within a nested for
loop. It only breaks the inner for
loop and the outer for
loop continues running.
Consider looking at the .zip()
function which can be used to iterate over corresponding questions & answers together instead of iterating all the answers for every question (in the worst case scenario). You will have a single for
loop, and it'd break out successfully.
Upvotes: 0