Today in this article I want to share Python Quiz Code Sachin Vs Virat 2023, So our challenge is to make a quiz for the real-life super greatest cricketer Sachin Tendulkar sir and Virat Kohli sir including for def quiz() function with range loop. Hi Buddy my name is Amol and I am the founder of amolblog.com.
This is Python Quiz Code If you have any other requests or project ideas then please share them with YouTube Channel.
Please note: If you want only the final code please jump to the Final code see below table of contents.
Step 1 import random and print Welcome Massage
# Step 1 import random and print Welcome Massage
import random
print("Welcome To Sachin Sir VS Virat Sir Quiz Game")
Step 2 Create a quiz() function with questions
def quiz():
questions = [
"Who has scored more runs in international cricket?",
"Who has more centuries in international cricket?",
"Who has a better batting average in international cricket?",
"Who has more ODI centuries?",
"Who has more Test centuries?"
]
Step 3 Create a question with an answer
# Step 3 create question with answer
answers = {
"Who has scored more runs in international cricket?": "Sachin Tendulkar",
"Who has more centuries in international cricket?": "Sachin Tendulkar",
"Who has a better batting average in international cricket?": "Virat Kohli",
"Who has more ODI centuries?": "Sachin Tendulkar",
"Who has more Test centuries?": "Sachin Tendulkar"
}
Step 4 Create a range loop with all
score = 0
#Step 4 Create range loop with all
for i in range(5):
question = random.choice(questions)
print(question)
user_answer = input()
if user_answer.lower() == answers[question].lower():
print("Correct!")
score += 1
else:
print("Incorrect. The correct answer is", answers[question])
Step 5 Print score
# Step 5 Print score
print("Out Of 5 Your final score is", score)
quiz()
Final code
# Step 1 import random and print Welcome Massage
import random
print("Welcome To Sachin Sir VS Virat Sir Quiz Game")
# Step 2 Create quiz() fuction with questions
def quiz():
questions = [
"Who has scored more runs in international cricket?",
"Who has more centuries in international cricket?",
"Who has a better batting average in international cricket?",
"Who has more ODI centuries?",
"Who has more Test centuries?"
]
# Step 3 create question with answer
answers = {
"Who has scored more runs in international cricket?": "Sachin Tendulkar",
"Who has more centuries in international cricket?": "Sachin Tendulkar",
"Who has a better batting average in international cricket?": "Virat Kohli",
"Who has more ODI centuries?": "Sachin Tendulkar",
"Who has more Test centuries?": "Sachin Tendulkar"
}
score = 0
#Step 4 Create range loop with all
for i in range(5):
question = random.choice(questions)
print(question)
user_answer = input()
if user_answer.lower() == answers[question].lower():
print("Correct!")
score += 1
else:
print("Incorrect. The correct answer is", answers[question])
# Step 5 Print score
print("Out Of 5 Your final score is", score)
quiz()
How to add questions to the python quiz?
def quiz():
questions = [
“Who has scored more runs in international cricket?”,
“Who has more centuries in international cricket?”,
“Who has a better batting average in international cricket?”,
“Who has more ODI centuries?”,
“Who has more Test centuries?”
]
How to add answers to the python quiz?
answers = { "Who has scored more runs in international cricket?": "Sachin Tendulkar", "Who has more centuries in international cricket?": "Sachin Tendulkar", "Who has a better batting average in international cricket?": "Virat Kohli", "Who has more ODI centuries?": "Sachin Tendulkar", "Who has more Test centuries?": "Sachin Tendulkar" }

