0% found this document useful (0 votes)
43 views

Coding Example (E)

1. The document shows code for a quiz program that uses constants and input validation checks. Constants are defined at the start for things like the minimum and maximum player ages. 2. Input is validated by checking for presence of a name and that the age entered is within the defined range. 3. The main game loop iterates through lists of questions and answers, asking each question until the player answers correctly or reaches the maximum attempts. The player's score is incremented if they get a question right on the first try.

Uploaded by

Frederick Muller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Coding Example (E)

1. The document shows code for a quiz program that uses constants and input validation checks. Constants are defined at the start for things like the minimum and maximum player ages. 2. Input is validated by checking for presence of a name and that the age entered is within the defined range. 3. The main game loop iterates through lists of questions and answers, asking each question until the player answers correctly or reaches the maximum attempts. The player's score is incremented if they get a question right on the first try.

Uploaded by

Frederick Muller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import easygui

Constants used rather than literals, where appropriate.


PLAYER_AGE_MIN = 5 These are at the start of the program so that they can
PLAYER_AGE_MAX = 19 be easily located and changed if necessary.
Note: This is indicative only. For example, learners may
YOUTH_QUIZ_AGE = 14
have chosen a different range as reasonable, valid
MAX_QUESTION_ATTEMPTS = 3
minimum and maximum ages for users (eg 5 to 100).

# Variables for the EasyGui enterbox to get the user's name.


title = "Welcome to the CyberSmart Quiz"
msg = "What is your name?"
player_name = ""

# Presence check to check if user has entered anything. If the user hits
# the OK button without entering anything, an empty string is returned.
while player_name == "": Input validity check (presence check)
player_name = easygui.enterbox(msg, title, "")
Arguments for the
easygui.enterbox
title = "Welcome to the CyberSmart Start Quiz" function (variables) are
msg = "How old are you?" assigned here, rather than in
the function itself (as literals)
# Checks if the player has entered an integer within a valid age range
# (school ages). The loop repeats until a valid integer is entered, or
# the player presses Cancel.
player_age = easygui.integerbox(msg, title, "")
while player_age < PLAYER_AGE_MIN or player_age > PLAYER_AGE_MAX:
msg = "Please enter a valid age from " + str(PLAYER_AGE_MIN) + " to " + \
str(PLAYER_AGE_MAX) + " years."
player_age = easygui.integerbox(msg, title, "")

Input validity
# Checks whether check Constants
the player falls within the Youth used rather
Quiz range. If
(range check) than literals
# so, they are recommended the Youth Quiz instead, and given the
# option to continue or quit. Otherwise, the player is shown the
# quiz intro and rules, and the game begins.

Note: This example includes two different types


… of input validity checks. Learners do not need to
demonstrate all two. It is sufficient for them to
demonstrate any input validity checks.
Rather than duplicating code (see Merit exemplar), a for loop is used

to iterate over the lists of questions and answers in parallel.

# This is the main game loop that repeats for each of the
# questions in the quiz list.
for i in range(0,len(quiz_questions)):
msg = quiz_questions[i]
title = "Question " + str(i + 1)
choices = ["A","B","C"]
player_answer = ""
question_attempt = 0
Constant used rather
# This loop repeats until thethan literalgets the answer correct,
player
# or they have had exceeded the maximum number of attempts
# allowed.
while player_answer != quiz_answers[i] \
and question_attempt < MAX_QUESTION_ATTEMPTS:
player_answer = easygui.buttonbox(msg, title, choices=choices)
# If the player gets the correct answer, they are
# congratulated. If they got it correct on their first
# attempt (ie question_attempt is 1), their score is
# incremented by 1. If they get the question incorrect,
# they are told to try again, and the question_attempt
# is incremented by 1.
question_attempt += 1
print("player_answer: ", player_answer, "quiz_answer: ",
quiz_answers[i])
if player_answer == quiz_answers[i]:
msg="Well done, " + player_name + "! You got it right!"
ok_button = "Next Question"
easygui.msgbox(msg, title, ok_button)
if question_attempt == 1:
player_score += 1


Note: Although the use of user-defined functions would have improved
the structure of the program, at this level, learners are only required to
demonstrate their ability to use either user-defined functions or data
stored in collections (eg lists).

You might also like