0% found this document useful (0 votes)
57 views3 pages

Y10 02 CT9 Homework Solutions

The document contains sample code and exercises for using conditional statements like if/elif/else to structure a code based on different conditions. It includes examples of declaring constants, reading user input, comparing values, and printing outputs based on the comparisons. The exercises focus on improving readability, adding comments, and using conditionals to direct program flow and output messages depending on user input.

Uploaded by

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

Y10 02 CT9 Homework Solutions

The document contains sample code and exercises for using conditional statements like if/elif/else to structure a code based on different conditions. It includes examples of declaring constants, reading user input, comparing values, and printing outputs based on the comparisons. The exercises focus on improving readability, adding comments, and using conditionals to direct program flow and output messages depending on user input.

Uploaded by

18gmills
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

TOPIC 1: COMPUTATIONAL THINKING

If…elif…else, readability

HOMEWORK SOLUTIONS FOR Y10-02-CT9

Question 1
Here are some constants. Decide if there is anything wrong with the way they have been
declared and initialised. If so, write a line of code to fix it.

Constant What’s wrong with it? How would you fix it?

sidesOfDice = 6 should be capitals SIDES_OF_DICE = 6

YES = “Y” nothing no fix required

Bicyclewheels = 40 should be capitals; BICYCLE_WHEELS = 2


incorrect value

P = 3.14159 not a meaningful name PI = 3.14159

MAX_SWIMMERS = 25 nothing no fix required

h = “SOS” not a meaningful name; HELP = “SOS”


should be capitals

Question 2
Here is the code for a simple program.
Make the code more readable by using white space, comments, and meaningful identifiers.
You may use your Integrated Development Environment (IDE).

XXX = "Goodbye"
n1 = 0
n2 = 0
theSum = 0
dif = 0
n1 = int(input("Enter a number: "))
n2 = int(input("Enter another number: "))
if (n1 > n2):
print(str(n1) + " is larger than " + str(n2))
theSum = n1 + n2
print("The sum is: ", str(theSum))
elif (n2 == n1):
print("Both numbers are equal.")
else:
print(str(n2) + " is larger than " + str(n1))
dif = n2 - n1
print("The difference is: " + str(dif))
print(XXX)

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
1
TOPIC 1: COMPUTATIONAL THINKING

If…elif…else, readability

# ------------------------------------------------------------
# Constants
# ------------------------------------------------------------
BYE = "Goodbye"

# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
num1 = 0 # Only work with integers
num2 = 0
theSum = 0
difference = 0

# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------

num1 = int(input ("Enter a number: "))


num2 = int(input ("Enter another number: "))

if (num1 > num2):


print(str(num1) + " is larger than " + str(num2))
theSum = num1 + num2
print("The sum is: ", str(theSum))
elif (num2 == num1):
print("Both numbers are equal.")
else:
print(str(num2) + " is larger than " + str(num1))
difference = num2 - num1
print("The difference is: " + str(difference))

print(BYE)

Question 3
A program is needed that allows a user to type in a letter of the alphabet. The program
should function so that one of four different messages is printed based on the letter typed in.
Write the program.
Hint: You will need to use if/elif/elif/else to provide four paths.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
2
TOPIC 1: COMPUTATIONAL THINKING

If…elif…else, readability

Letter range Message

A–F Level one

G–L Level two

M–R Level three

S–Z Level four

# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
choice = "" # Users input

# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
choice = input("Enter a letter of the alphabet: ")
choice = choice.upper()

if (choice >= "S"):


print("Level four")
elif (choice >= "M"):
print("Level three")
elif (choice >= "G"):
print("Level two")
else:
print("Level one")

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
3

You might also like