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

Coding Example (M)

The document describes code for a quiz program that gets a player's name and age, checks if they are eligible to take the quiz, then asks multiple choice questions in a loop. It analyzes the code commenting on variable names, comments, code structure, use of loops, debugging with print statements, and providing feedback to the player.

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)
15 views

Coding Example (M)

The document describes code for a quiz program that gets a player's name and age, checks if they are eligible to take the quiz, then asks multiple choice questions in a loop. It analyzes the code commenting on variable names, comments, code structure, use of loops, debugging with print statements, and providing feedback to the player.

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

Variable names clearly identify and distinguish

import easygui
different variables in terms of the data they hold.

player_name = easygui.enterbox("What is your name?",


"Welcome to the CyberSmart Start Quiz")
user_age = easygui.integerbox("How old are you?",
Meaningful comments are included that clearly
"Welcome to the CyberSmart Start Quiz")
indicate the code function and behaviour.

# This checks that the user is an appropriate age (under 14). If they
# are, continue_quiz is set to "Yes". This is used as a gate later on to
# decide whether the user sees the quiz questions or the program exits.
# If the user is older than 13 they are encouraged to do the Youth Quiz
# instead, and are given the option to continue on with the CyberSmart
# Start Quiz or quit. If they decide to continue, continue_quiz is set
# to "Yes".
if user_age < 14:
Literal used rather than constant, making the program less flexible.
continue_quiz = "Yes"
easygui.msgbox("Quiz rules/intro goes here")

# This print line is included for testing purposes.


print(user_age, continue_quiz)
Comments are as per Python convention. For example:
• block comments for blocks of code
else: • comment line length kept to a maximum of 72 characters
• no redundant
continue_quiz = easygui.buttonbox("Hi " comments (ie comments
+ player_name + "!\n\n that
Thisindicate
quiz \
is meant for people under 12. You
information that is already clear from the code itself) \
should try the CyberSmart Youth Quiz instead.
Do you want to continue?", choices = ["Yes", "No"])
• comments are full sentences starting with a capital letter.

# This is the gate to check whether the quiz should continue because
# either the user has earlier indicated they are under 14, or they want
# to continue even though they are older.
if continue_quiz == "Yes":

Python convention followed regarding line width (ie line


length for code kept
quiz_questions to a maximum
= ["Question of Someone
1:\n\n 79 characters).
sends you a text that is hurtful \
and makes you feel bad about yourself. What should you do?\n\n \
a. Delete the message and try to forget about it.\n \
b. Keep the text and show an adult you trust.\n \
c. Text the person back saying something mean to them.",
"Question 2:\n\n You find out that someone has posted an \
embarrassing picture of you on Twitter. What should you do?\n\n \
Program could be better structured. Currently the code for
… each question is a copy of Question 1, with a different question
# Question 1 and answer selected from the lists (quiz_questions,
answer = quiz_answers[0] quiz_answers) each time. A for loop iterating over the
question_try = 1 question and answer lists in parallel would have been a more
efficient, robust, and flexible solution.

# This print line is included for testing purposes.


print(quiz_questions[0], "\nquestion answer: ", quiz_answers[0])

while question_try < 4:


player_answer = easygui.buttonbox(quiz_questions[0],
choices=["A", "B", "C"])

# These print lines are included for testing purposes.


Print lines included to aid
print("\nplayer answer 1: ", player_answer) organised testing/debugging
print("question attempt: ", question_try) (see Item 9).

if player_answer == answer:
easygui.msgbox("Well done, " + player_name + "! You got it right!")

# The code block below runs if the player


# gets the answer correct on the first try.
if question_try == 1:
player_score += Python
1 convention followed by using consistent style
for variable names (lowercase with underscore).
# This print line is included for testing purposes.
print("player score is: ", player_score)
break
else:
easygui.msgbox("Sorry, " + player_name + "! That's not right.")
question_try += 1

Here the player is only told the letter for the


# If the player didn't get the answer right after 3 attempts,
correct answer (eg ‘B’). It would have been
# the game gives the player the correct answer and tells them to
more logical to give them the full answer for
# try the next question
the question.
if question_try == 4:
easygui.msgbox(answer + " is the correct answer. \
Try a different question.")

You might also like