0% found this document useful (0 votes)
25 views41 pages

Advanced Python Unit: ST Ambrose College Computer Science Year 9

Uploaded by

mustapha122003
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)
25 views41 pages

Advanced Python Unit: ST Ambrose College Computer Science Year 9

Uploaded by

mustapha122003
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
You are on page 1/ 41

St Ambrose College

Computer Science
Year 9

Advanced Python
Unit

Name:
Class:

Target:
Final Grade:
HOMEWORK/EXTENSION WORK
GCSE AS AN OPTION CHOICE
• Work your way through as many of the online tutorials as you
possibly can.
• Your teacher will inform you which ones you must complete
• https://www.codecademy.com/learn/learn-python-3
• https://www.tutorialspoint.com/python
• https://www.learnpython.org
• https://www.w3schools.com/python/

Syntax checklist
If you are faced with an error message, read it carefully and try to fix the problem.

Use the list below to check for common errors.

misspelled if, elif, or else (this includes using capitals)

forgot the colon : at the end of a line containing if, elif, or else

neglected to indent statements in the if–block, elif–block, or else–block

indented if, elif, or else by mistake

used = instead of == in a condition, to check if two values are equal

used quotes around the name of a variable

forgot to use quotes around a string literal (like "Monday")


LESSON ONE
• Revisit Basic Python Syntax and Constructs
• Reinforce Control Flow Structures
Starter: Complete the recap tasks below

1. Fill in the blank with the correct words.

Word bank: variables, while, if, input, arithmetic

In Python, are used to store data that can be accessed and manipulated

throughout the program.

The statement is used to execute a block of code repeatedly based on a

condition.

The statement is used to make a decision and execute different blocks of

code based on a condition.

The function is used to take data from the user and store it in a variable.

The operator is used to perform mathematical operations in Python.

2. Which of the following is a valid variable name in Python?

a) 123_variable

b) myVariable Answer:

c) my-variable

d) all of the above

3. What is the output of the following Python code?

x=5
y = 10
if x > y: Answer:
print("x is greater than y")
else:
print("y is greater than x")

4. What is the output of the following Python code?

x = 10
if x > 0: Answer:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is zero")
Task One:

1. Write a program that takes three numbers and prints the largest number or if they are all equal.

2. Write a computer program to evaluate the performance of a business. Your program will ask the
user to input:
a. The total amount spent/invested on the business: Total costs
b. The total amount raised by the business: Total sales

Your program will then calculate and display the profit made by the business and indicate
whether the business made a profit or a loss.

Your program will then calculate and display the Return On Investment (ROI) of the business as a
percentage value.
3. Write a program that calculates the discount based on the purchase amount:
a. Above £1000: 20% discount
b. £500-£1000: 10% discount
c. Below £500: No discount

4. Write a program that asks the user for the name of the book they borrowed and then ask how
many days the book is overdue. The program should calculate the fine for overdue library books
based on the number of days overdue and output the name of the book and the total fine in a
sentence at the end:
a. 1-5 days: £0.50 per day
b. 6-10 days: £1.00 per day
c. More than 10 days: £5.00 per day
5. Write a program that calculates the Body Mass Index (BMI) and categorizes it. The program
should:
a. Take the weight (in kilograms) and height (in meters) as input.
b. Calculate the BMI using the formula BMI
c. Categorise the BMI:
i. Below 18.5: Underweight
ii. 18.5 to 24.9: Normal weight
iii. 25 to 29.9: Overweight
iv. 30 and above: Obesity
Print the BMI value and the corresponding category.
6. Create a computer program to let the customer pick up their options for their ice cream.

The customer should be able to specify:

a. Whether they would like their ice cream to be served in a cup (50p) or on a cone (80p)
b. How many scoops (£1 per scoop) they would like to order (between 1 and 4)
c. Whether they would like to add a flake (40p)
d. Whether they would like to add some chocolate sprinkle (30p)
e. Whether they would like to add a strawberry coulis (60p)

The program will then output the order and calculate the total price
LESSON TWO
• Understand the Concept of While Loops
• Integrate Boolean Expressions in While Loops
• Solve Practical Problems Using While Loops

Starter: Use the space provided on the right to explain what is wrong with the code blocks below.

Task One: Complete the tasks below by filling in the blanks.

Data Type Name What it stores


Whole numbers (e.g., 1, 2, 3)

Str

Stores a collection of items in a particular order

Float

Stores values True or False

name = (input(“Enter your name”))

= int (input(“How old are you?”))

school = (input(“Are you in school?”))

answer = (input(“What is 10 divided by 4”))


Task Two: Explain what the following coding blocks does.

It will ask you to enter either apple, banana, or cherry.

When the word is not apple, banana, or cherry, it will say “Invalid input. Please enter one of the
specified words.”

It will then re-ask you to enter either apple, banana, or cherry.

It will keep doing steps 3-4 until you enter enter either apple, banana, or cherry.
Task Three: Complete the programming tasks below

1. Write a program that asks the user to enter their age. The program should ensure that the
input is a positive integer.

If the user enters an invalid age (e.g., a negative number or zero), the program should
prompt the user to enter a valid age.

age = int(input("Please enter your age: "))

if age < 1:

print("Invalid age. Please enter your age: ")

age = int(input("Please enter your age: "))

2. Ask the user to enter in the school they go to and only allow entry if they enter one of the
following “St Ambrose College”, “Saint Ambrose College”, “st ambrose” or “St Ambrose”
3. Create a program using a while loop that keeps asking a user to enter a number and adds
those numbers together to a total.

When the total goes over 100, the program stops and outputs: “OVERLOAD! You have gone
over 100!”

4. Write a program that prompts the user to enter a password and then asks for confirmation
by entering the password again.

If the passwords do not match, the program should keep asking the user to re-enter both
passwords until they match.
Extension Task:

Write a program that generates a random number between 1 and 100 -


(https://www.w3schools.com/python/gloss_python_random_number.asp). The user must guess the
number, and the program should give feedback whether the guess is too high, too low, or correct.

The program should keep asking for a guess until the user guesses the correct number. Additionally,
validate that the input is a number within the correct range.
LESSON THREE
• Develop a Functional Python Program that meets a set of
requirements

Starter: Match up the keywords with definitions

Input perform actions repeatedly

Print display to the screen

While check a condition, to select


which actions to perform

If read from the keyboard

assign a value to a variable


=

What is the difference between a while loop and an IF statement - you have 15 words to describe
this difference.

Task One: Pick out the 5 errors in the code below and state what the error is on the next page.
Main Task: Subway Menu

Place evidence of your completed code in the space provided on the next page

Extension:

Validate the entry of the users personal details using the function that checks the length of a string

https://tinyurl.com/j37kc3fv
LESSON FOUR – TESTING AND PEER ASSESSMENT
• Understand and Implement System Testing
• Make necessary improvements to the Python program based on test
results

Starter: Find out what the difference between a syntax error and a logic error is – max 30 words

Task One: Spotting syntax and logic errors.

Q1. The aim of the program below is to:

Output the first 10 square numbers (e.g. 1x1=1, 2x2=4, 3x3=9 etc.).

01 for i = 0 to 10
02 print(i*i)
03 next j

There is a syntax error in this program.

Give the line number the error is on.

Explain why it is an error.

Write down the corrected version of this line that fixes the error.
There is a logic error in this program.

Give the line number the error is on.

Explain why it is an error.

Write down the corrected version of this line that fixes the error.

Q2. The aim of the program below is to:

Check if three user provided numbers are equal and output a message stating so.

01 a = input(“Enter first number”)


02 b = input(“Enter second number”)
03 c = input(“Enter third number”)
04 if a==b or b==c then
05 print(“All numbers are equal)
06 else
07 print(“Numbers are not equal”)
08 endif

There is a syntax error in this program.

Give the line number the error is on.

Explain why it is an error.

Write down the corrected version of this line that fixes the error.
There is a logic error in this program.

Give the line number the error is on.

Explain why it is an error.

Write down the corrected version of this line that fixes the error.

Q3. The aim of the program below is to:

Calculate the sum of 10 numbers as entered by the user.

01 sum = 0
02 for n = 1 to 10
03 num = int(input(“Enter number”)
04 sum = sum - num
05 next n
06 print(sum)

There is a syntax error in this program.

Give the line number the error is on.

Explain why it is an error.

Write down the corrected version of this line that fixes the error.
There is a logic error in this program.

Give the line number the error is on.

Explain why it is an error.

Write down the corrected version of this line that fixes the error.

Main Task:

You have been given a Python program that simulates a basic grade management system for a
student. The program should allow the user to input grades for 5 subjects, calculate the average
grade, and determine the student's performance level.

The code contains several logic and syntax errors. Your task is to identify and correct these errors.

Additionally, you will need to test the program with valid, invalid, and extreme inputs.

print("Welcome to the Grade Management System")


total = 0
for i in range(3, 5 +1):
grade = input("Enter grade between 0 and 100 for subject " + i + ": ")
while grade < 0 or grade > 100:
print("Grade must be between 0 and 100.")
grade = int(input("Enter grade for subject " + str(i) + ": "))
total += grade
total / num_subjects = average
if average <= 90:
print("Excellent performance")
elif average >= 105:
print("Good performance")
elif average >= 50:
print(Average performance)
else:
print("Outstanding performance")
print("Your average grade is " + average)
Tes Description Test Test Expected Actual
t Data Type Outcome Outcome
No
1 Does the program run N/A N/A
2
3
4 Enter in a grade that is less than -3 Invalid Data isn’t
0 accepted
5
6
7
8
9
10
11
12

Example Test Table


LESSON FIVE – FOR LOOPS
• Understand the Concept and Syntax of For Loops
• Apply For Loops to Solve Problems

Starter: Complete the programming terminology task. Paste the completed task into the space
below - https://tinyurl.com/yr9pythonstarter

Quick Task One:

1. Make a simple for loop just like the one we went through on slide 7 but make it repeat for 5 turns
and print your name instead of ‘This is a loop’.

2. Make another loop and repeat the word hello one thousand times.
Quick Task Two:

1. Create a program that loops from 100 to 200 and prints the loop number each time.

2. Create another program that starts at 10 and prints the even numbers up to 20...

HINT: Writing a
second comma and a
third number in the
range brackets e.g.
range(200,301,10)

3. Using your answer to #2 to help you, write a loop that counts backwards from 100 to 0, printing
each turn.

Main Task: Complete the tasks below and paste your code into the space provided

Using for loops, create a program to output this nursery rhyme: "1, 2, 3, 4, 5, Once I caught a fish
alive, 6, 7, 8, 9, 10 then I let it go again".
Write a program that prints the multiplication table for a given number. It will ask the user for a
number. Loop round 10 times and print the multiplication table for the given number.

Using a for loop ask the user to enter in 5 numbers and calculate the average of the 5 numbers.

Ask for a starting value and an ending value and output the sentence “20 times 20 times Man
United” this number of times
Write a program that prints the numbers from 1 to 100. But for multiples of three, print "Fizz"
instead of the number, and for the multiples of five, print "Buzz". For numbers which are multiples of
both three and five, print "FizzBuzz".

HINT – You will need to research what the % symbol does in python programming

Write a program that prints a pattern of stars in a pyramid shape.


LESSON SIX – INTRODUCTION TO LISTS
• Understand the Concept and Structure of Lists
• Perform Basic Operations with Lists

Starter: Complete the MCQ assessment using the link provided by your teacher. Paste your final
score into the space provided below.

Quick Task One:

Create a list named bands and include three of your favourite musicians.

Quick Task Two:

Use the .append command to add two more bands to your list

Output all the values stored in the list


Quick Task Three:

Remove the first and last elements of the list.

Output all the values stored in the list again.

Quick Task Four:

Create a list of at least five breakfast cereals

Print each cereal on a separate line.

Then print just the first cereal in a message underneath, using its index.
Main Task: Complete the tasks below and paste your code into the space provided

Write a program that declares a list and adds the following items: "apple", "banana" "cherry". The
program should then output each item in the list,

Write a program that allows a user to add three items to a list and then outputs the contents of the
list.

Write a program with the following items in added to a list: “Breaking Bad”, "Inbetweeners", “The
Wire”, “Stranger Things”, “24”, “Friday Night Lights”. The program should output the contents of the
list and then ask the user what tv series they want to remove and then remove the item from the
list. It should then output the contents of the list again.
Objective:

Write a program that finds and prints the largest number in a given list of numbers.

Description:

1. Create a list of numbers. 2. Use a loop to find the largest number in the list. 3. Print the largest
number.

Objective:

Write a program that counts and prints the number of times a specific element appears in a list.

Description:

1. Create a list with repeated elements. 2. Ask the user for the element to count. 3. Use a loop to
count the occurrences of the element. 4. Print the count.
LESSON SEVEN – LISTS PART 2
• Be able to create lists and use indices
• Perform list functions using methods

Starter:

What will be displayed on the screen when


this program is executed?

What will be the output of this program


when it is executed?

Quick Task One:

 Ask the user to enter as many words as they can in 20 seconds.


 Add each word to a list.
 When the 20 seconds is up the user has to enter STOP. You do not need to write any code
to count, the user should be trusted to type STOP.
 Use the len function to print the first and last words entered - do not count STOP as an
entered word.
 Look back at code about how to print an element using its index - remember to use square
brackets, like cities[0].
 Extra Challenge: Also print the total number of words entered.

Quick Task Two: (Use the Pokemon example on the board to help you)

 Create a list of five of your friends.


 Create an input line to ask a user to enter a name.
 Use an if statement to check if their name is in the list.
 Print appropriate responses if it is found and if it isn't.
Main Task:

Scenario: Imagine you are developing a simple grocery list management program for a user
who wants to keep track of their grocery items. The program should allow the user to create
a list, populate it with initial grocery items, add new items, search for specific items, and
remove items once they are no longer needed.

Objective: Create a Python program that helps the user manage their grocery list by
performing various operations.

 Create an empty list


o Initialise an empty list to represent the grocery list.
 Populate the list with initial items
o Allow the user to enter initial grocery items.
o Use a loop to continually accept input until the user types 'done'.
o Append each entered item to the list.
 Add new items to the list
o Ask the user to enter an additional grocery item.
o Append the entered item to the list.
 Search for specific items in the list
o Prompt the user to enter an item to search for in the list.
o Check if the entered item is in the list.
o Print a message indicating whether the item was found or not.
 Remove items from the list
o Prompt the user to enter an item to remove from the list.
o Check if the entered item is in the list.
o Remove the item if it is found, and print a message confirming the removal.
o If the item is not found, print a message indicating it cannot be removed.

Paste your code in the space provided on the next page


LESSON EIGHT – SUBROUTINES AND FUNCTIONS
• Understand what subroutines and functions are
• Make use of subroutines to complete tasks
• Understand the difference between a local and global variable

Starter: Spot and highlight the errors in the paragraph

In Python programming, we use variables to store values that can change, such as user inputs or

calculations. Constants are values that should remain the same throughout the program, although

Python doesn't have built-in support for constants, we usually write them in lowercase letters. An if

statement helps us to make decisions in our code. For example, if a variable x is equal to 10, we can

print "x is ten". Loops allow us to repeat a block of code multiple times. A for loop is a condition-

controlled loop that continues until a certain condition is met, while a while loop is count-controlled

and repeats a set number of times. In the following code, we initialize a variable num to 0 and use a

for loop to increment it up to 5: for i in range(6) num = num + i. After the loop, we check if num is

greater than 10 using an if statement: if num == 10 print "num is greater than 10".

Write the correct sentences in the space provided below


Quick Task One:
Write 3 procedures (sub-routines) named "Task 1", "Task 2", and "Task 3", each of which should
output "This is task x!", then call all three procedures in order.

Quick Task Two:


Create a procedure called hello that just prints “Hello there!”.
In the main program create a for loop that calls the procedure 10 times.
You must use a procedure.
Main Task
Create a program with three different subroutines (procedures). One subroutine asks the user their
name and prints a response. The second asks for their age and prints a response. The third asks for
their favourite colour and prints a response.
Remember to write subroutines before the main program.

Create a program that asks the user to enter their name in the main program. Call a subroutine that
greets the user using the name variables. You must use a procedure and a global variable.
Create a Python program that manages student grades using subroutines. The program should
include subroutines for adding 3 grades, calculating the average grade, finding the overall total,
finding the highest grade, and finding the lowest grade.
The program should have a menu system that takes the user to the correct subroutine and then
completes that task
LESSON NINE – REVISION
• Reinforce Understanding of Core Python Concepts
• Practice Problem-Solving with Python
• Identify and Address Areas for Improvement

Starter: Formulate a question that fits the provided answer. Each question should be clear and
specific to the concept discussed.

1. Answer:

o They are used to store information that can be referenced and manipulated in a
program. They provide a way to label data with a descriptive name.

2. Answer:

o It is a collection of items that are ordered and changeable. They are created using
square brackets, and elements can be of different data types.

3. Answer:

o These allow you to execute a block of code only if a specified condition is true.

4. Answer:

o A float would be better as it can store decimal places and the price of an item could
be £9.99 for example.

5. Answer:
o A for loop will loop round a certain amount of times and is count controlled a while
loop will continue to loop until a condition is met.
Main Task: Complete as many of the tasks you can from the revision list below. As a minimum you
need to achieve 12 points
Python Basics – Cheat Sheet
EXTENSION TASKS

You might also like