Write a program that creates a GK quiz consisting of any five questions of your choice. The questions should be displayed randomly. Create a user defined function `score()` to calculate the score of the quiz and another user defined function remark (`scorevalue`) that accepts the final score to display remarks as follows: Marks Remarks 5 Outstanding 4 Excellent 3 Good 2 Read more to score more 1 Needs to take interest 0 Take it seriously.
Question
Write a program that creates a GK quiz consisting of any five questions of your choice. The questions should be displayed randomly. Create a user defined function score()
to calculate the score of the quiz and another user defined function remark (`scorevalue`) that accepts the final score to display remarks as follows:
Marks | Remarks |
---|---|
5 | Outstanding |
4 | Excellent |
3 | Good |
2 | Read more to score more |
1 | Needs to take interest |
0 | Take it seriously. |
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Here's a Python program to create a GK quiz with five random questions. It includes user-defined functions for calculating the score and providing remarks based on the score.
import random
# Function to ask questions and return score
def score():
questions = {
"What is the capital of France?": "Paris",
"In which year did World War I begin?": "1914",
"What is the largest planet in our solar system?": "Jupiter",
"Who wrote 'Romeo and Juliet'?": "Shakespeare",
"What is the chemical symbol for water?": "H2O"
}
# Shuffle and get the first 5 questions
selected_questions = random.sample(list(questions.items()), 5)
correct_answers = 0
for question, answer in selected_questions:
user_answer = input(question + " ")
if user_answer.strip().lower() == answer.lower():
correct_answers += 1
return correct_answers
# Function to provide remarks based on score
def remark(scorevalue):
if scorevalue == 5:
return "Outstanding"
elif scorevalue == 4:
return "Excellent"
elif scorevalue == 3:
return "Good"
elif scorevalue == 2:
return "Read more to score more"
elif scorevalue == 1:
return "Needs to take interest"
else:
return "Take it seriously."
# Main program execution
final_score = score()
print(f"Your score is: {final_score}/5")
print(remark(final_score))
How It Works:
score()
Function: This function contains a dictionary of five questions and their correct answers. It selects 5 random questions, prompts the user for answers, and calculates the number of correct responses.remark(scorevalue)
Function: Based on the input score, it returns an appropriate message.Main Execution: The program calculates the score and prints both the score and the remark.
Key Points:
Randomization: The
random.sample
function ensures that different questions are asked each time the quiz is taken.Input Matching: User inputs are compared to the correct answers in a case-insensitive manner.
Structured Output: The score and corresponding remarks are displayed at the end.
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month