create quiz application
*
unable to uplink my python idle file after several tries too
Code Sample:
import random
# Define questions and answers for different topics
topics = {
"Mathematics": [
{
"question": "What is the square root of 64?",
"options": ["6", "7", "8", "9"],
"answer": "8"
},
{
"question": "What is the value of pi (approx.)?",
"options": ["3.14", "3.141", "3.1415", "3.14159"],
"answer": "3.14"
},
{
"question": "What is the formula for calculating the area of a circle?",
"options": ["pi * r^2", "2 * pi * r", "pi * d", "pi * r"],
"answer": "pi * r^2"
}
],
"History": [
{
"question": "Who was the first president of the United States?",
"options": ["George Washington", "Thomas Jefferson", "John Adams", "Benjamin Franklin"],
"answer": "George Washington"
},
{
"question": "Which country did Napoleon Bonaparte come from?",
"options": ["France", "England", "Germany", "Russia"],
"answer": "France"
},
{
"question": "What was the name of the German dictator during World War II?",
"options": ["Adolf Hitler", "Joseph Stalin", "Benito Mussolini", "Hideki Tojo"],
"answer": "Adolf Hitler"
}
],
"Science": [
{
"question": "What is the chemical symbol for gold?",
"options": ["Au", "Ag", "Cu", "Fe"],
"answer": "Au"
},
{
"question": "What is the largest organ in the human body?",
"options": ["Liver", "Heart", "Brain", "Skin"],
"answer": "Skin"
},
{
"question": "What is the smallest unit of life?",
"options": ["Cell", "Molecule", "Atom", "Element"],
"answer": "Cell"
}
]
}
# Allow users to select a topic
print("Welcome to the Quiz! Please select a topic:")
for i, topic in enumerate(topics.keys()):
print(f"{i+1}. {topic}")
topic_index = int(input("Enter topic number: ")) - 1
selected_topic = list(topics.keys())[topic_index]
# Shuffle questions
questions = topics[selected_topic]
random.shuffle(questions)
# Define score and question index
score = 0
question_index = 0
# Loop through questions
while question_index < len(questions):
question = questions[question_index]
# Display question and options
print(f"\nQuestion {question_index + 1}: {question['question']}")
for i, option in enumerate(question['options']):
print(f"{chr(i + 65)}. {option}")
# Allow user to select an answer
answer = input("Enter your answer (A/B/C/D): ").upper()
# Check if answer is correct
if answer == question['answer'][0].upper():
print("Correct!")
score += 1
else:
print(f"Sorry, the correct answer is {question['answer']}.")
question_index += 1
# Display final score
print(f"\nYour final score is {score}/{len(questions)}")
Clone link:
git clone ssh://pratikborle74%40gmail.com@anvil.works:2222/HU4LGYWXLUDBWL3I.git Quiz_Conductor
share a copy of your app
Quiz Conductor