# Initialize score counter
score = 0
total_questions = 3
print("Welcome to the Python Quiz Game!")
print(f"You'll be asked {total_questions} questions. Let's begin!\n")
# Question 1 - Python Basics
user_answer = input("1. What keyword defines a function in Python? ")
correct_answer = "def"
if user_answer.lower() == correct_answer:
print("Correct!")
score += 1
else:
print(f"Oops! The correct answer is '{correct_answer}'")
# Let student retry
while user_answer.lower() != correct_answer:
user_answer = input("Try again: ")
if user_answer.lower() == correct_answer:
print("Correct! It took you a few tries, but you got it!")
score += 0.5 # Half point for retries
break
# Question 2 - Data Types
user_answer = input("\n2. What data type is used for text in Python? ")
correct_answer = "str"
if user_answer.lower() == correct_answer:
print("Correct!")
score += 1
else:
print(f"Not quite! The correct answer is '{correct_answer}'")
# Alternative retry method
retries = 2
while retries > 0 and user_answer.lower() != correct_answer:
user_answer = input(f"You have {retries} more attempts: ")
retries -= 1
if user_answer.lower() == correct_answer:
print("You got it!")
score += 0.5
else:
print(f"The correct answer was '{correct_answer}'")
# Question 3 - Loops
user_answer = input("\n3. What loop repeats WHILE a condition is true? ")
correct_answer = "while"
if user_answer.lower() == correct_answer:
print("Correct!")
score += 1
else:
print("That's not right. Hint: The keyword is also the name of the loop.")
# Let student see the correct answer and confirm understanding
while True:
user_answer = input("Type the correct answer to continue: ")
if user_answer.lower() == correct_answer:
print("Good job learning it!")
score += 1 # Still gets full point
break
print("Please type exactly:", correct_answer)
# Final Results
print("\n--- Quiz Results ---")
percentage = (score / total_questions) * 100
print(f"Final Score: {score}/{total_questions} ({percentage:.1f}%)")
if percentage >= 80:
print("Excellent work! You know your Python!")
elif percentage >= 50:
print("Good effort! Review those missed questions.")
else:
print("Keep practicing! Your Python knowledge will grow.")
print("\nThanks for playing!")