Scenario: You are building a locked quiz for Year 7 maths revision.
Part A — Password
- Ask for a password.
- If it is exactly
y7python, printAccess grantedand continue. - Otherwise print
Access deniedand do not run the quiz. (Put the quiz code inside theif.)
Part B — Quiz
- Ask 5 questions:
What is 1 x 8?, then2 x 8?, … up to5 x 8?. Use aforloop. - If the answer is correct, add 1 to the score and print
Correct!. - If wrong, print the right answer.
Part C — Grade
- Print the final score out of 5.
- Print a grade:
- 5:
Grade: Excellent - 3 or 4:
Grade: Good - below 3:
Grade: Keep practising
- 5:
💡 Try the whole challenge in your editor first. Only reveal the solution when you have had a proper go.
Solution
password = input("Enter password: ")
if password == "y7python":
print("Access granted")
score = 0
for question in range(1, 6):
answer = int(input("What is " + str(question) + " x 8? "))
if answer == question * 8:
print("Correct!")
score = score + 1
else:
print("Wrong! The answer was", question * 8)
print("You scored", score, "out of 5")
if score == 5:
print("Grade: Excellent")
elif score >= 3:
print("Grade: Good")
else:
print("Grade: Keep practising")
else:
print("Access denied")
Example output:
Enter password: y7python
Access granted
What is 1 x 8? 8
Correct!
What is 2 x 8? 16
Correct!
What is 3 x 8? 20
Wrong! The answer was 24
What is 4 x 8? 32
Correct!
What is 5 x 8? 40
Correct!
You scored 4 out of 5
Grade: Good