1 of 325

Revisit phase - Starter

Boolean expression

True/False

4 > 5 AND 7 == 7

NOT(2 == 2)

11 <= 2 OR 4 != 1

NOT(12 > 2 OR 5 < 1)

5 != 5 AND 7 > 2

10 >= 10 and 5<=6

False

False

True

False

False

True

Created by Mr Suffar

2 of 325

  • Be able to manipulate strings.
  • Apply string manipulations to different python tasks.
  • Understand the purpose of substrings and length checks.

String Manipulation

Lesson 6

Tuesday, July 18, 2023

Lesson objectives…

Created by Mr Suffar

3 of 325

Video

Lesson theory video: https://youtu.be/gEJMBkArksg

Created by Mr Suffar

4 of 325

String manipulation - Concatenation

Concatenation means ‘joining’.

The ‘+’ sign joins together two or more string (without a space).

firstname = input("Enter your firstname")

surname = input("Enter your surname")

print(firstname+surname)

Created by Mr Suffar

5 of 325

String manipulation - Concatenation

firstname = input("Enter your firstname")

surname = input("Enter your surname")

print(firstname+" "+surname)

What will the following program display if the user enters “Barry” as the firstname and “Wood” as the surname?

Created by Mr Suffar

6 of 325

String manipulation - Concatenation

You can also use the “,” to join 2 strings together WITH A SPACE. However, in the exam if they ask you to concatenate, you MUST use the “+” sign to join strings together.

firstname = input("Enter your firstname")

surname = input("Enter your surname")

print(firstname, surname)

Created by Mr Suffar

7 of 325

Using concatenation

firstname = input("Enter your firstname")

surname = input(firstname, " Enter your surname")

The above code will give you an error, because you can’t add a comma when storing a value in a variable because it will count it as an argument. However you can concatenate using “+” sign.

Created by Mr Suffar

8 of 325

Correct way:

firstname = input("Enter your firstname")

surname = input("hey " +firstname+" Enter your surname")

Created by Mr Suffar

9 of 325

String manipulation - Length

len() is a python function that finds the length of a string.

Example 1:

name = "Riley"

print( len (name) )

The above program will diplay the number 5. Because that is the number of characters is in the string "Riley"

Created by Mr Suffar

10 of 325

String manipulation - Length

len() is a python function that finds the length of a string.

Example 2:

name = input("Enter a name")

print( len (name) )

The above program will diplay the number 4 if the user types Jeff as the name.

Created by Mr Suffar

11 of 325

String manipulation - Length

What will the program below display?

  • 8
  • It displays 8 because the space counts as a character.

Created by Mr Suffar

12 of 325

String manipulation - Length

What will the program below display if the user enters Tennis?

  • “Can you even count?”

Created by Mr Suffar

13 of 325

Tasks

Complete the tasks

Created by Mr Suffar

14 of 325

148. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

name = input("Enter a name")

hobby=input("Enter hobby")

total = name+hobby

print(len(total))

Answer:

Created by Mr Suffar

15 of 325

149. The program below must: Ask the user for a film that contains 3 characters and then display whether the user is correct or incorrect.

Copy the code below, run it and fix the errors.

film = input(Enter a film that 3 characters")

if length(film)==3

print"Correct")

else:

print=("Incorrect")

Paste the correct version below:

Created by Mr Suffar

16 of 325

150. Create a program that:

Asks the user for a sentence.

If the sentence contains more than 20 characters including space, display “Too long”

Otherwise display “Just right”

sentence = _____("Enter a sentence")

if len(_____) >20:

_____("Too long")

_____:

_____("Just right")

Copy the code above, fill the gaps then paste your code below:

Created by Mr Suffar

17 of 325

151. Create a program that:

  • Asks the user for firstname.
  • Asks the user for surname (use the firstname inside the question – example below)
  • Using concatenation to display the firstname and surname in a full sentence.
  • Use + sign to concatenate.

Paste your code below:

Created by Mr Suffar

18 of 325

152. Create a program that:

  • Asks the user for their favourite drink.
  • Store the length of the string in a variable called “drinkLength”
  • Display the length of the drink on the screen in a full sentence.

Paste your code below:

Created by Mr Suffar

19 of 325

153. Create a program that:

  • Asks the user for their firstname.
  • Ask the user for their surname
  • Concatenate the two strings(variables) together without space and store it in a variable called “total”.
  • Display the total length of firstname+surname.

Paste your code below:

Created by Mr Suffar

20 of 325

154. Create a program that:

  • Ask the user to enter a colour that contains 6 letters.
  • If the user enters the correct answer. Display “well done”
  • Otherwise display “incorrect”.

Hint: len(colour)

Paste your code below:

Created by Mr Suffar

21 of 325

String manipulation – Case conversion

You can turn your string to either uppercase using .upper() or lowercase using .lower()

name = input("Enter your name")

print ( name.upper() )

print ( name.lower() )

Created by Mr Suffar

22 of 325

String manipulation – Case conversion

What will the following code display if the user enters “Football”.

  • Unknown
  • This is because the F is a capital.

Created by Mr Suffar

23 of 325

String manipulation – Case conversion

What will the following code display if the user enters “FoOtBaLL”.

  • 11 a side

Created by Mr Suffar

24 of 325

String manipulation – Substring

Substrings are used to extract part of a string.

Index numbers starts from 0.

What do you think the following code will display?

country = "ENGLAND"

print(country[0])

It will display the letter “E”

Index number

0

1

2

3

4

5

6

Characters

E

N

G

L

A

N

D

Created by Mr Suffar

25 of 325

String manipulation – Substring

Substrings are used to extract part of a string.

Index numbers starts from 0.

What do you think the following code will display?

country = "France"

print(country[1])

It will display the letter “r”

Created by Mr Suffar

26 of 325

String manipulation – Substring

What do you think the following code will display?

country = "Italy"

print(country[len(country)-1]

It will display the letter “y”

len(country)-1 will allow you to display the last letter of the string. This is because len(country) will give you 5, and 5-1 = 4 and country[4] = “y”

Created by Mr Suffar

27 of 325

String manipulation – Substring

What do you think the following code will display?

country = "Egypt"

print(country[len(country)]

The program will crash as len(country) is 5 and there are no index 5 in the string as we start from 0.

Created by Mr Suffar

28 of 325

String manipulation – Substring

What do you think the following code will display?

country = “Jordan"

print(country[len(country)-2]

It will display the letter “a”

Created by Mr Suffar

29 of 325

String manipulation – Substring

What do you think the following code will display?

country = "Malta"

print(country[len(country)-1]

It will display the letter “a”

Created by Mr Suffar

30 of 325

String manipulation – Substring – PYTHON

The following code will display: “EN”

country = "ENGLAND"

print( country [ 0 : 2 ] )

Start position

Up to but not included

Index number

0

1

2

3

4

5

6

Characters

E

N

G

L

A

N

D

Created by Mr Suffar

31 of 325

String manipulation – Substring – PYTHON

The following code will display: “AND”

country = "ENGLAND"

print( country [ 4 : ] )

Start position

If you leave it empty, it will go to the end of the string

Index number

0

1

2

3

4

5

6

Characters

E

N

G

L

A

N

D

Created by Mr Suffar

32 of 325

String manipulation – Substring – PYTHON

What will the following code display?

country = "United States"

print( country[1: 6 ])

Start position

Up to but not including

It will display “nited”

Index number

0

1

2

3

4

5

6

7

8

9

10

11

12

Character

U

n

i

t

e

d

S

t

a

t

e

s

Created by Mr Suffar

33 of 325

String manipulation – Substring – PYTHON

What will the following code display?

country = "United States"

print( country[7 : ])

It will display “States”

Created by Mr Suffar

34 of 325

Tasks

Complete the tasks

Created by Mr Suffar

35 of 325

155. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

hobby = input("Enter a hobby")

choice = input("Do you want the first letter in Uppercase or lowercase?")

if choice == "upper":

print(hobby[0].upper())

else:

print(hobby[0].lower())

Answer:

Created by Mr Suffar

36 of 325

156. The program below must: Ask the user for a favourite car, then displays “German” if the user enters BMW. It displays Japan if the user enters Toyota. It displays the first letter of the car for any other choice.

Copy the code below, run it and fix the errors.

car input("Enter your favourite car")

if car.upper() == "BMW"

print"German")

elif car.lower == "toyota":

print("Japan")

else

print("The first letter of your favourite car is",car[1])

Paste the correct version below:

Created by Mr Suffar

37 of 325

157. Create a program that:

Asks the user to enter a city name that contains 5 letters. If the user gets it right, display “Correct” then display the first letter of the city. Otherwise display “Incorrect” and the last letter of the city.

city = ____("Name a city that contains 5 letters")

if len(____) == 5:

____("Correct")

print(city[__])

____:

print("Incorrect")

print(city[len(____)-1])

Copy the code above, fill the gaps then paste your code below:

Created by Mr Suffar

38 of 325

158. Create a program that:

  • Ask the user for their firstname.
  • Ask the user for their surname.
  • Display the first letter of the user’s firstname.
  • On a separate line, display the first 4 letters of the user’s firstname.
  • On another line, display the last letter of the user’s surname.

Paste your code below:

Created by Mr Suffar

39 of 325

159. Create a program that:

  • Ask the user for a quote.
  • Display the quote in uppercase
  • Then displays the quote in lowercase on a separate line.

Paste your code below:

Created by Mr Suffar

40 of 325

160. Create a program that:

  • Ask the user for their name.
  • Ask the user if they want their name to be displayed in capital letters or lowercase.
  • If the user types lowercase, display the name in lowercase.
  • If the user types uppercase, display the name in uppercase.
  • Otherwise display “wrong choice”.

Paste your code below:

Created by Mr Suffar

41 of 325

161. Create a program that:

  • Asks the user for their favourite colour.
  • Asks the user for their favourite food.
  • Display the first letter of the user’s favourite colour.
  • On a separate line, display the first 3 letters of the user’s favourite food.
  • On another line, display the last letter of the user’s favourite colour.

Paste your code below:

Created by Mr Suffar

42 of 325

162. Create a program that:

  • Ask the user for a game name that contains at least 6 characters.
  • Output “Acceptable” or “Not acceptable” depending on the user’s answer.

Paste your code below:

Created by Mr Suffar

43 of 325

163. Create a program that:

  • Ask the user to enter an animal that contains 4 letters.
  • If the user enters the correct answer. Display “well done”
  • Otherwise display “incorrect”.

Hint: len(animal)

Paste your code below:

Created by Mr Suffar

44 of 325

164. Create a program that:

  • Ask the user to enter their first name.
  • Ask the user to enter their surname.
  • Ask the if they want to see the first letter of firstname&surname or the last letter of firstname&surname.
  • If they enter “first”, display the first letter of their first name and the first initial of their surname joined together using concatenation.
  • If they enter “last”, display the last letter of their first name and the last letter of their surname.
  • Otherwise, display “wrong option”

Paste your code below:

Created by Mr Suffar

45 of 325

Formatting text

\n - This adds a new line. Example:

print("Hello \nWorld") would display:

\t - The t stands for “tab” which adds extra space. Example:

print("Hello \tWorld") would display:

Created by Mr Suffar

46 of 325

165) Create a program that:

  • Displays “Good” then “Game” on 2 separate lines using \n and 1 print command.
  • On another line, display Good game but with 4 spaces (TAB) in the middle using \t.

Paste your code below:

Created by Mr Suffar

47 of 325

166. Create a program that:

  • Ask the user to enter a school name.
  • Ask the if they want to see the first or last letter of the school name.
  • If the answer is “first”, display the first letter of the school name
  • If the answer is “last”, display the last letter of the school name “hint: use -1 to display last letter”.
  • Otherwise display “wrong option”.

Paste your code below:

Created by Mr Suffar

48 of 325

167. Create a program that:

  • Asks the user for a song name.
  • Asks the user how many characters do they want to see from the song name starting from the first character. Example: If the user types 4, show the first 4 characters of the song name.
  • Display the correct amount of characters depending on the user’s answer.
  • Store the total length of the song name in a variable called: “songLength”
  • On a separate line, display the total length of the song name in a full sentence.

Paste your code below:

Created by Mr Suffar

49 of 325

168. Create a program that:

  • Asks the user for the name of their favourite singer.
  • Store the length of the singer’s name in a variable called nameLength.
  • Asks the user how many characters do they want to see from the singer’s name starting from the first character. Example: If the user types 4, show the first 4 characters of the singer’s name.
  • Add validation check so that the amount of characters the user enters is not bigger than the total length of the name. For example: If the total length of the name is 7 characters, and the user enters 9 when asked how many characters do they want to see, then display an “Error”. Otherwise display the correct number of characters.

Paste your code below:

Created by Mr Suffar

50 of 325

169. Create a program that:

  • Asks the user for their name.
  • Extract the first character of their name and save it in a variable called “first”.
  • Force the first character to upper case using .upper() function.
  • Use concatenation to join the first letter back to the original string.
  • Example: if the user types suffar as there name, you need to display: Suffar

Paste your code below:

Created by Mr Suffar

51 of 325

170. Create a program that:

  • Asks the user to enter a school name.
  • Asks the user if they want the school name to be displayed in capital letters or lower case.
  • If the answer is “uppercase”, display the school name in uppercase.
  • if the answer is “lowercase”, display the school name in lowercase
  • Otherwise display “Wrong option”.

Paste your code below:

Created by Mr Suffar

52 of 325

171. Create a program that:

  • Ask user to enter a name, then display whether their name contains an odd number of letters or an even number of letters.
  • For example, if the user enters suffar, it must display even as it contains 6 letters.

Paste your code below:

Created by Mr Suffar

53 of 325

172. In a school, a student username is created by extracting first 2 letters of firstname, first 3 letters of surname, last 2 numbers of the year and it adds a hashtag depending on the number of other students with the same surname. For example: if a student named Bob Liam joined in 2023 and there are 2 other people in the school with the surname Liam, then the username of this student is: “BoLia23##”

  • Create a program that asks the user for firstname, surname, year joined and number of other students with same surname then display the username of the student.

Paste your code below:

Created by Mr Suffar

54 of 325

TASK 173

Answer:

Created by Mr Suffar

55 of 325

Task 174

Answer:

Created by Mr Suffar

56 of 325

175. A car registration code needs to be entered before a car can be registered. The registration code must start with a capital letter and must be followed by a number between 0 and 9.

Create a program that:

Asks the user to enter the registration code.

Checks and displays whether the code is valid or invalid.

Paste your code below:

Created by Mr Suffar

57 of 325

Created by Mr Suffar

58 of 325

Revisit phase - Starter

Used to …

Function / symbol

Change the string to lowercase

.lower()

Change the string to uppercase

Symbol used to concatenate 2 strings.

Finds the length of a string

Which brackets are used to extract part of a string

.upper()

+

len()

[]

Created by Mr Suffar

59 of 325

  • Understand the structure of a for loop.
  • Solve real-world problems using for loops.
  • Understand how to use selection and iteration on a single program to solve a problem.

Iteration

Lesson 7

Tuesday, July 18, 2023

Lesson objectives…

Created by Mr Suffar

60 of 325

Iteration theory: https://youtu.be/myTwo-B9E4k

Created by Mr Suffar

61 of 325

Knowledge phase�

Iteration: The repetition of a process/ section of code.

Created by Mr Suffar

62 of 325

for __ in range(__) :

code to be run here

more code to be run here

this code not in the for loop

Note that indenting (using the TAB key) is used to decide which code goes inside the loop.

Variable

Number of iteration

End with a colon

Structure of a for loop

Created by Mr Suffar

63 of 325

for x in range (5) :

print("Hello")

print("The end")

It will display “hello” 5 times then it will display “The end” ONCE.

Variable

Number of iteration

Created by Mr Suffar

64 of 325

for y in range (3) :

name=input("Enter a name")

print("Your name is", name)

print(“Good bye")

It will ask the question 3 times and then displays the names 3 times then it will display “Good bye” once

Variable

Number of iteration

Created by Mr Suffar

65 of 325

number = int(input("Enter number"))

for i in range (number) :

print("hello")

This will display “hello” depending on the number that the user entered.

Created by Mr Suffar

66 of 325

Tasks

Complete the tasks

Created by Mr Suffar

67 of 325

176. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

for x in range(10):

print("Computer Science")

print("Is the best")

Answer:

Created by Mr Suffar

68 of 325

177. The program below must: Ask the user for 5 football team names, then displays “You have now entered 5 teams when all 5 teams have been entered.

Copy the code below, run it and fix the errors.

for x in range(5)

team = input("Enter a football team"

print(The team you have entered is:"team)

prints("You have now entered 5 teams")

Paste the correct version below:

Created by Mr Suffar

69 of 325

178. Create a program that:

Asks the user to enter 5 numbers, for each number, if the number is odd, display the number followed by “is odd”. If the number is even, display the number followed by “is even”.

for ___ in ____(5):

num = ___(___("Enter a number"))

if num % ___ == 0:

print(___,"is even")

else:

print(num,"___")

Copy the code above, fill the gaps then paste your code below:

Created by Mr Suffar

70 of 325

179.

  • Copy the code below and run it.
  • Then change the % to a "$".
  • Run it again then comment on the code to explain the purpose of the code.

for count in range(1,31):

print (count * "%")

Paste your new code below with comments:

Created by Mr Suffar

71 of 325

180. Use a for loop to display “welcome” 100 times. Then display “bye” ONCE.

Paste your code below:

Created by Mr Suffar

72 of 325

181. Ask the user for a number. Repeat ‘I like turtles’ a certain number of times, based on a number entered by the user. (If the user enters 5, display I like turtles 5 times)

Paste your code below:

Created by Mr Suffar

73 of 325

182. Ask the user to enter a name. Ask the user how many times they want their name to be displayed. Display the user’s name repeatedly depending on the user’s answer.

Paste your code below:

Created by Mr Suffar

74 of 325

183) Ask the user their name and their age. Print their name the number of times as their age.

Paste your code below:

Created by Mr Suffar

75 of 325

184) Write a program that:

  • Asks the user to enter a number
  • Adds 5 to that number
  • Displays the new number on the screen.
  • Make the above process repeat 6 times.

Paste your code below:

Created by Mr Suffar

76 of 325

185. Create a program that:

  • Asks the user for a name.
  • Display the user’s name as many times as the letters in the name. For example: If the name has 7 letters, display that name 7 times.

Paste your code below:

Created by Mr Suffar

77 of 325

186. Create a program that asks the user for their name and then display it 10 times.

Paste your code below:

Created by Mr Suffar

78 of 325

187. Use a for loop to display “Hello World” 100 times. Then display “bye” ONCE.

Paste your code below:

Created by Mr Suffar

79 of 325

188) Create a program that:

  • Asks “how many negatives did you get?”
  • If the answer is 1, display prompt 10 times.
  • if the answer is 2, display reminder 50 times.
  • If the answer is 3, display warning 100 times
  • Display removal 500 times if any other number is entered.

Paste your code below:

Created by Mr Suffar

80 of 325

189) Wzzap market is a shop that sells face masks.

Create a program that:

  • Asks if the shop is open.
  • If the user answers "yes", display "Can I get a face mask please" 5 times using a for loop.
  • Otherwise display “I need to stay at home!” 100 times using a for loop.

Paste your code below:

Created by Mr Suffar

81 of 325

190) Create a program that:

  • Asks for the user’s age.
  • If the age is greater than 12, display happy birthday 100 times.
  • Otherwise display happy birthday 12 times.

Paste your code below:

Created by Mr Suffar

82 of 325

191) Create a program that:

Asks if McDonald’s is open. If the user answers “yes”, ask the user to enter how much their meal will cost.

IF their meal costs more than 5, display “no McDonald’s today” 5 times. Else display “I can buy this meal” 5 times.

Otherwise, display “McDonald’s is closed” 5 times.

Paste your code below:

Created by Mr Suffar

83 of 325

192. Ask the user for a number. Then ask the user what their favourite subject is.

Display a full sentence which shows the user’s favourite subject. R

Paste your code below:

Created by Mr Suffar

84 of 325

193. Ask the user to enter a name. If the name contains 5 or more characters, display the first letter of the name 10 times. Otherwise display the last letter of the name once.

Paste your code below:

Created by Mr Suffar

85 of 325

194. Ask the user to enter their firstname and store it in a variable called “firstname”. Then ask them to enter their surname and store it in a separate variable. Create a variable called fullname and use concatenation to join the 2 strings together. Now display the total amount of characters in the fullname 5 times. Then display the full name without a space in capital letters.

Paste your code below:

Created by Mr Suffar

86 of 325

for x in range (1,11) :

print(x)

This is used if you want to use a counter. x is a variable that will hold numbers from 1 to 10.

Variable

Start from

Up to but not included

Created by Mr Suffar

87 of 325

for count in range (1,6) :

print(count* 10)

This will produce the 10 times table from 1 to 5.

Variable

Start from

Up to but not included

Created by Mr Suffar

88 of 325

number = int ( input ( "Enter number " ) )

for count in range (1,6) :

print ( number * count )

What will the above program display if the user enters 100 as the number.

  • It will display the times table of the number that the user enters (100) from 1 to 5.

Created by Mr Suffar

89 of 325

num = 7

for x in range(1,6):

print(x,"*",num,"=",x*num)

The above program will display the times table of 7 from 1 to 5.

String

Created by Mr Suffar

90 of 325

Time.sleep function

Time.sleep function is used to add delay in the execution of a program.

import time

num = int(input("Enter a number"))

for x in range(1,11):

print(x,"*",num,"=",x*num)

time.sleep(1)

Delay in seconds

Allows you to use the

time.sleep function

Created by Mr Suffar

91 of 325

Tasks

Complete the tasks

Created by Mr Suffar

92 of 325

195. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

import time

num = int(input("Enter a number"))

for x in range(1,101):

print(num,"/",x,"=",num/x)

time.sleep(0.5)

Answer:

Created by Mr Suffar

93 of 325

196. The program below must: Count from 1 to 100. For each number, display the number followed by “is divisible by 4” if it is divisible by 4. and “is not divisible by 4” if it is not divisible by 4.

Copy the code below, run it and fix the errors.

import timeeee

for x in range(1,10001):

if x % 4 == 0

print(y,"is divisible by 4")

time.sleep(21)

else

print(x,"is not divisible by 4")

time.sleep(1)

Paste the correct version below:

Created by Mr Suffar

94 of 325

197. Create a program that:

Counts from 1 to 100. For each number, display each number and then display Blue if the number is odd, display “Red” if the number is even.

import ____

for x in ____(1,101):

print(____)

if __ % 2 == 0:

____("Red")

else:

print("____")

Copy the code above, fill the gaps then paste your code below:

Created by Mr Suffar

95 of 325

198. Create a program that counts from 1 to 100.

Paste your code below:

Created by Mr Suffar

96 of 325

199. Ask the user for a number. Display the times table of that number from 1 to 100.

Paste your code below:

Created by Mr Suffar

97 of 325

200. Ask the user for a number. Display the times table of that number from 1 to 5 in the following style 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪

Paste your code below:

Created by Mr Suffar

98 of 325

201) Create a program that:

  • Displays all odd numbers between 1 and 99.

Paste your code below:

Created by Mr Suffar

99 of 325

202) Create a program that:

  • Displays all even numbers between 1 and 100.

Paste your code below:

Created by Mr Suffar

100 of 325

203. Create a program that counts and displays all numbers from 15 to 25.

Paste your code below:

Created by Mr Suffar

101 of 325

204. Ask the user for a number. Display the powers of 0 to 10 for that number.

HINT: ** = the exponent operator

Paste your code below:

Created by Mr Suffar

102 of 325

205. Print the numbers from 1 to 100, but replace multiples of 3 with “Chicken" and multiples of 5 with “Nuggets".

Paste your code below:

Created by Mr Suffar

103 of 325

206. Create a program that:

Prints the numbers from 1 to 100, but replace multiples of 3 with "Fizz" and multiples of 5 with "Buzz". If a number is both a multiple of 3 and 5, it will be replaced with "FizzBuzz".

Paste your code below:

Created by Mr Suffar

104 of 325

207. Ask the user for a number. Divide that number from 1 to 5 and display the answer in a full sentence. 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪 🡪

Paste your code below:

Created by Mr Suffar

105 of 325

208. Create a program that takes a number that is greater than 0 and less than 100 as an input.

  • It calculates and output the square numbers up to and including the number input.
  • For example: if they enter 4, it should display, 1*1=1, 2*2=4, 3*3=9, 4*4=16

Paste your code below:

Created by Mr Suffar

106 of 325

Task 209

Python

Answer:

Created by Mr Suffar

107 of 325

Created by Mr Suffar

108 of 325

for count in range (1,6) :

print(count* 10)

This will produce the times table of 10 from 1 to 5.

Variable

Start from

Up to but not included

What will the following code display?

Revisit phase - Starter

Created by Mr Suffar

109 of 325

for count in range (1,101) :

print(count* 3)

  • This will produce the 3 times table from 1 to 100.

Variable

Start from

Up to but not included

Revisit phase - Starter

What will the following code display?

Created by Mr Suffar

110 of 325

  • Understand the totalling concept.
  • Understand the counting concept.
  • Be able to solve problems using counting and totalling method.

Totalling, Counting, Highest & Lowest

Lesson 8

Tuesday, July 18, 2023

Lesson objectives…

Created by Mr Suffar

111 of 325

total = 0

for x in range(10):

num = int(input("Enter a number"))

total = total+num

The above program will calculate the total (sum) of 10 numbers.

Set total to 0

Number of iteration

Ask for a number

Adds the number to

The total variable.

Created by Mr Suffar

112 of 325

count = 0

for x in range(10):

num = int(input("Enter a number"))

if num>100:

count=count+1

The above program will count how many of the 10 numbers entered are above 100.

Set count to 0

Number of iteration

Ask for a number

Checks if the number is

bigger than 100

Increment count by 1

Created by Mr Suffar

113 of 325

#Ask for 10 numbers, calculate highest and lowest number

highest = 0

lowest=9999999

for x in range(10):

num = int(input("Enter a number"))

if num > highest:

highest = num

if num < lowest:

lowest = num

Checks if the number entered

Is bigger than highest.

Set highest to that number.

Checks if the number entered

Is smaller than lowest.

Set lowest to that number.

Created by Mr Suffar

114 of 325

Assignment

In python, a single “=“ sign indicates an assignment statement.

  • number = 5

In this assignment statement, we are assigning the value 5 to the variable number.

In pseudocode, the arrow 🡨 is used to indicate an assignment statement.

  • number 🡨 5

Created by Mr Suffar

115 of 325

COLD CALL

Created by Mr Suffar

116 of 325

COLD CALL

Created by Mr Suffar

117 of 325

COLD CALL

Created by Mr Suffar

118 of 325

Tasks

Complete the tasks

Created by Mr Suffar

119 of 325

210. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

total = 0

for x in range(10):

num = int(input("Enter a number"))

if num % 2 == 0:

total = total + num

print("Sum of even numbers:", total)

Answer:

Created by Mr Suffar

120 of 325

211. The program below must: Display all numbers from 1 to 10 inclusive. Adds each number to a total then displays the total at the end.

Copy the code below, run it and fix the errors.

total = 5

for num1 in range(1, 115)

print(num

total = num + total1

print("Sum of numbers:" total)

Paste the correct version below:

Created by Mr Suffar

121 of 325

212. The program below must: Asks the user to enter 10 numbers, counts how many numbers entered are above 1000. Display the answer at the end.

Copy the code below, run it and fix the errors.

count = 4

for i in range(10:

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

if num == 1000:

count = count - 1

print("The number of numbers above 1000 is:" count)

Paste the correct version below:

Created by Mr Suffar

122 of 325

213. Create a program that: Asks the user for 5 numbers, calculates and displays the highest and lowest number

highest = ____

lowest=____

for x in range(___):

num = ___(___("Enter a number"))

if num > ___:

___ = num

if num < ___:

lowest = ___

print("Highest:",___)

print("Lowest:",___)

Copy the code above, fill the gaps then paste your code below:

Created by Mr Suffar

123 of 325

214. In python,

  • use a count controlled loop (for loop) to ask the user for 10 numbers and add each number to the total.
  • Display the total after every number that is entered.
  • Display the average of the 10 numbers at the end.

Paste your code below:

Created by Mr Suffar

124 of 325

215. In python,

  • Asks the user to input the 12 numbers.
  • Count and display how many of the numbers are above 1000.

Paste your code below:

Created by Mr Suffar

125 of 325

216. In python

  • Asks the user to input 7 numbers. Display each number after they are entered.
  • Display the highest number and the lowest number at the end in a full sentence.

Paste your code below:

Created by Mr Suffar

126 of 325

217. In python:

  • Asks the user to enter 20 numbers.
  • Displays the total of the 20 numbers entered.
  • Displays the average of the 20 numbers.

Paste your code below:

Created by Mr Suffar

127 of 325

218. A teacher is recording the amount of time it take to solve a Rubik cube. The algorithm must:

  • Asks the user to input the number of attempts.
  • Ask the user to input how long it took in seconds to solve the Rubik cube.
  • Displays the total time of all attempts.
  • Displays the average time of the attempts.

Paste your code below:

Created by Mr Suffar

128 of 325

219. A teacher wants to count the number of students who play games for at least 10 hours a week.

Create an python program that:

Ask 5 students for the number of hours a week they spend playing games.

Count and display the number of students who play games for at least 10 hours a week.

Paste your code below:

Created by Mr Suffar

129 of 325

220. Ask the user to enter 10 numbers.

Count and display how many of the numbers entered are between 10 and 99.

Paste your code below:

Created by Mr Suffar

130 of 325

221. Create a program to calculate how many points a player has after 5 games. Players receive 3 points for winning, 1 point for a draw and 0 points for losing a game. The player will move up to a higher division if they achieve at least 12 points. The player will move to a lower division if they achieve a less than 6 points. Otherwise they will stay in the same division. Your program must:

  • Ask the player if they have won, lost or drew.
  • Calculate the total number of points earned after 5 games.
  • Output the number of points earned after 5 games.
  • Use selection to find out if the player has been promoted, relegated or stayed in the same division.
  • Output the answer with an appropriate message.

Paste your code below:

Created by Mr Suffar

131 of 325

222. Create a program that:

  • Asks the user to input 5 numbers.
  • Count and display the following:
  • How many of the number are odd
  • How many of the numbers are even
  • How many of the numbers are positive
  • How many of the numbers are negative
  • How many of the numbers are zeros.

Paste your code below:

Created by Mr Suffar

132 of 325

223. Dodgy Sam visited 6 supermarkets in 1 day to stock pile toilet rolls.

Create an algorithm that:

  • Asks the user to input the number of toilet rolls bought at each supermarket.
  • If they entered 1 or above, then ask them how much each toilet roll cost.
  • Calculate the total amount spent on toilet rolls after visiting all 6 supermarkets.
  • Displays the highest amount paid for a single toilet roll.
  • Display the lowest amount paid for a single toilet roll.

Hint: Use for a for loop and if statements.

Paste your code below:

Created by Mr Suffar

133 of 325

224. A hospital wants to create a survey to check how many patients are carrying a virus. If the patient has a fever AND a dry cough, the hospital assumes they test positive for the virus.

Create a program that:

  • Ask the user to input how many patients are taking the test.
  • Ask the user if they have a fever.
  • Ask the user if they have a dry cough.
  • If they have both of the above, display you have tested positive for the virus.
  • If they tested positive for the virus, ask them if the symptoms are mild or severe.
  • Display how many patients tested positive and how many patients tested negative for the virus.
  • Display how many patients have mild symptoms and how many patients have severe symptoms

Paste your code below:

Created by Mr Suffar

134 of 325

Task 225

Answer:

Created by Mr Suffar

135 of 325

Task 226

Answer:

Created by Mr Suffar

136 of 325

227. In python, create a program that:

  • Take 1000 numbers as an input.
  • Count and output how many numbers are less than 500.
  • Count and output how many numbers are greater than 750
  • Calculate and output total of any number that is below 500.
  • Display your answers in full sentence.

Paste your code below:

Created by Mr Suffar

137 of 325

228. A class has 4 students.

Create a program that asks 4 students for their name and their height in CM

Calculate and display the average height of the class.

Calculate and display the name and the height of the tallest person in the class.

Calculate and display the name and the height of the shortest student in the class.

Paste your code below:

Created by Mr Suffar

138 of 325

Homework

Created by Mr Suffar

139 of 325

  • Understand the purpose of a trace table.
  • Complete trace tables for a given scenario
  • Solve real world problems using nested for loops.

Nested for loop & Trace table

Lesson 9

Tuesday, July 18, 2023

Lesson objectives…

Created by Mr Suffar

140 of 325

Created by Mr Suffar

141 of 325

Trace table

Trace table is used to:

  • Find the output of programs
  • Find the changes in the values of variables
  • Find errors in the program.

Created by Mr Suffar

142 of 325

Example of a simple trace table:

x

Output

for x in range (1,6):

print(x*2)

1

2

2

4

3

6

4

8

5

10

Created by Mr Suffar

143 of 325

Example of a trace table:

x

x < 3

Output

for x in range (1,5):

if x < 3:

print("less than 3")

else:

print("3 or above")

1

True

Less than 3

2

True

Less than 3

3

False

3 or above

4

False

3 or above

Created by Mr Suffar

144 of 325

Assume the user enters the following values: “Sam”, “upper”, “Ayo”, “lower”, “Zoe”,”upper”

x

choice

case

Output

for x in range(1,4):

choice = input("Enter a word")

case = input("upper or lower")

if case == "upper":

print(choice.upper () )

else:

print(choice.lower () )

1

Sam

upper

SAM

2

Ayo

lower

ayo

3

Zoe

upper

ZOE

Created by Mr Suffar

145 of 325

Tasks

Complete task

Created by Mr Suffar

146 of 325

229. Complete the trace table.

X

Output

for x in range (1,6):

print(x**2)

Created by Mr Suffar

147 of 325

230. Complete the trace table.

for x in range (1,5):

country = "France"

print(country[x])

X

country

Output

Created by Mr Suffar

148 of 325

231.Assume the user enters the following values: 0, 3, 2, 5

for x in range (2,7):

if x < 3:

print("it’s Jeff")

else:

num = int(input("Enter a number"))

print(x * num)

x

Is x < 3

num

Output

Created by Mr Suffar

149 of 325

232. Assume the user enters the following values: “Nick”, “Lukas”, “Lara”, “Tim”

for x in range (1,5):

name= input("Enter a name")

length = len(name)

print(x * length)

X

name

length

Output

Created by Mr Suffar

150 of 325

233. Assume the user enters the following values: 7, 0, 4, 8

for x in range (1,5):

country = "United States"

num = int(input("Enter a number"))

letter = country[num]

print(country[x] + letter)

X

country

num

letter

Output

Created by Mr Suffar

151 of 325

234. Assume the user enters the following values: 4, *, 3, % ,6, %, 5, *

Hint: % = MOD (finding the remainder)

for x in range (1,5):

num = int(input("Enter a number"))

operator = input("Enter operator")

if operator == "%":

print(num % x)

else:

print(num*x)

x

num

operator

Output

Created by Mr Suffar

152 of 325

235. Complete the trace table.

for x in range (1,7):

if x % 2 == 0:

print(x)

X

Is x % 2 == 0

Output

Created by Mr Suffar

153 of 325

236. Complete the trace table

for x in range (1,4):

for y in range (1,4):

print( x * y )

x

y

Output

Created by Mr Suffar

154 of 325

for x in range(1,6):

for y in range(1,11):

print(x,"X",y,"=",x*y)

The above program will calculate the total (sum) of 10 numbers.

Repeats 5 times

Repeats 10 times for every x, in this case this

line will be executed 50 times.

Displays the times table of 1 to 5

from 1 to 10.

A nested for loop is a for loop inside another for loop

Created by Mr Suffar

155 of 325

FOR loop with different steps

You can change the number that each loop increments by.

for x in range(1, 101, 2) :

print(x)

This will for loop will start from 1 and increments by 2 until it reaches 100.

Created by Mr Suffar

156 of 325

FOR loop with different steps

You can also count down:

for y in range(100,0,-1) :

print(y)

This will count from 100 down to 1. The last number is not inclusive.

Created by Mr Suffar

157 of 325

237. Create a nested loop (for loop inside a for loop) to display the multiplication timetable between 1 and 5. Display your answer in an appropriate format. Example:

Paste your code below:

Created by Mr Suffar

158 of 325

238. Create a program that counts down from 1000 to 1.

Paste your code below:

Created by Mr Suffar

159 of 325

239. Create a program that counts up in steps of 3 from 1 to 1000.

Paste your code below:

Created by Mr Suffar

160 of 325

240. Code the following flowchart in python:

Paste your code below:

Created by Mr Suffar

161 of 325

241. Code the following flowchart in python:

Paste your code below:

Created by Mr Suffar

162 of 325

Created by Mr Suffar

163 of 325

  • Be able to loop through a string
  • Complete complex challenges on looping through a string.
  • Solve problems using chr() and ord() function.

Looping through string & ASCII characters

Lesson 10

Tuesday, July 18, 2023

Lesson objectives…

Created by Mr Suffar

164 of 325

name = "Max"

print(name[0])

print(name[1])

print(name[2])

print(len(name))

What will the above program display?

  • M
  • A
  • x
  • 3

Looping through a string

Created by Mr Suffar

165 of 325

name = "Max"

for x in range(0,3):

print(name[x])

What will the above program display?

  • M
  • A
  • x
  • This is because the first value of x is 0, then 1 then 2.

Looping through a string

Created by Mr Suffar

166 of 325

name = "Max"

for x in range(0,len(name)):

print(name[x])

What will the above program display?

  • M
  • A
  • x
  • len(name) is = 3 so x will count from 0 to 2.

Looping through a string

Created by Mr Suffar

167 of 325

phonenum = input("Enter a phone number")

for x in range(0,len(phonenum)):

print(phonenum[x])

Loops through the string

Displays every number in

the string on a separate line

Looping through a string

Created by Mr Suffar

168 of 325

nine = 0

phonenum = input("Enter a phone number")

for x in range(0,len(phonenum)):

if phonenum[x] == "9":

nine = nine+1

print("There are",nine,"nines in the phone number")

The above program will count and display the number of 9s in the phone number.

Looping through a string

Created by Mr Suffar

169 of 325

lowercase = ""

sentence = input("Enter a sentence")

for x in range(0,len(sentence)):

if sentence[x] == sentence[x].lower():

lowercase = lowercase + sentence[x]

print(lowercase)

The above program asks the user for a sentence, and then adds all the lowercase characters to a variable called lowercase and displays it on the screen.

Created by Mr Suffar

170 of 325

242. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

sentence = input("Enter a sentence")

newsentence = ""

for x in range(len(sentence)):

if sentence[x].lower() == "s":

newsentence = newsentence+ "#"

elif sentence[x].lower() == "a":

newsentence = newsentence+ "@"

else:

newsentence = newsentence+ sentence[x]

print(newsentence)

Answer:

Created by Mr Suffar

171 of 325

243. The program below must: Ask the user for a 4 digit pin code that doesn’t have the number 0. If the length of the digit is not 4, display “Not a 4 digit pin”, otherwise, check if the code includes the number 0, if it does, display invalid, if it doesn’t have a 0, display valid.

Copy the code below, run it and fix the errors.

pin = input("Enter a 4 digit pincode without the number 0)

valid = Truee

if len(pin) == 4

for x in range(length(pin)):

if pin[y] == "0":

valid= False

printvalid)

else

print("Not a 4 digit code"

Paste the correct version below:

Created by Mr Suffar

172 of 325

244. Create a program that: Asks the user for a name. Count and display how many letters between “a” to “g” inclusive is in the name.

name = ____("Enter a name")

count = __

for x in range(len(____)):

letter = ____[x].lower()

if letter >= "a" and ____ <= "g":

count = ____+__

print("Number of letters between a-g in the name:",____)

Copy the code above, fill the gaps then paste your code below:

Created by Mr Suffar

173 of 325

245. Create a program that:

  • Asks the user to enter a binary number.
  • Counts how many 1s and how many 0s are in the following binary number.
  • The program must display the number of 1s and 0s in a full sentence.
  • Hint: Use a for loop to loop through the string.

Paste your code below:

Created by Mr Suffar

174 of 325

246. Create a program that:

  • Asks the user for a phone number (must be a string)
  • Display all the numbers on the screen on separate line.
  • Calculate the total of all the numbers in that string by casting each number to integer first then adding it to a total.

Paste your code below:

Created by Mr Suffar

175 of 325

247. Create a program that:

  • Asks the user to enter a sentence.
  • Display the number of spaces present in the sentence.

Paste your code below:

Created by Mr Suffar

176 of 325

248. Create a program that asks the user for their name and then display the name with each character repeated once.

For example: if the user enters Max, it displays Mmaaxx.

Paste your code below:

Created by Mr Suffar

177 of 325

249. Create a program that ask the user to enter a sentence, then display only the uppercase characters in that sentence.

Paste your code below:

Created by Mr Suffar

178 of 325

250. Ask the user to enter their school name. Then capitalise the last letter of the school name and display the answer on the screen.

Paste your code below:

Created by Mr Suffar

179 of 325

251. Create a program that:

  • Asks user for a word, reverse the letters in the word and display the new word on the screen. So If the user enters “alan” it should display “nala”

Paste your code below:

Created by Mr Suffar

180 of 325

252. Create a program that asks the user for a sentence. Then reverse the case. All lower-cased letters should be upper-cased, and vice versa. Display your answer on the screen.

Paste your code below:

Created by Mr Suffar

181 of 325

253. Create a program that:

  • Asks the user to enter 10 letters.
  • If the user does not enter 10 letters, display an error.
  • If the user enters 10 letters, then calculate and display the number of CAPITAL letters the user entered.

Paste your code below:

Created by Mr Suffar

182 of 325

254. Palindromes is a word, phrase, or sequence that reads the same backwards as forwards, e.g. madam , bob.

Create a program that:

  • Asks the user for a word that is also a palindrome.
  • If the user enters a palindrome then display “Correct”, otherwise display “Incorrect”

Paste your code below:

Created by Mr Suffar

183 of 325

255. Parity bits are used to ensure binary data are not corrupted during transit. They work by calculating the number of 1s in a binary number. If the binary number contains an even number of 1s, it means an even parity was used. If the number of 1s are odd, it means it uses odd parity.

Create a program that:

  • Asks the user to enter a binary number.
  • Assuming the binary number is not corrupted, display whether the binary number was sent using odd parity or even parity.

Paste your code below:

Created by Mr Suffar

184 of 325

256. Create a program that asks the user to enter a sentence then count the number of words in that sentence and displays it on the screen.

Paste your code below:

Created by Mr Suffar

185 of 325

257. Create a program that asks the user for a sentence then turns any letter “s” or letter “x” into a capital letter then display the answer on the screen.

Paste your code below:

Created by Mr Suffar

186 of 325

258. Create an encryption algorithm (program) that encrypts a sentence.

Your program must ask the user for a sentence then encrypt the sentence by changing the letters (a and n to “#”) and the letters (m, t to “&”) and (g, s to “@”).

Paste your code below:

Created by Mr Suffar

187 of 325

259. Create a program that:

  • Asks the user for a name and changes the letters in the name.
  • If the letter is between a-g inclusive then change that letter to a “#”
  • if the letter is between h-n inclusive then change the letter to a “%”
  • Otherwise, do not change the letter.

Paste your code below:

Created by Mr Suffar

188 of 325

ord() is a function that returns the ascii code for the character.

Example: ASCII code for ‘A’ is 65, ‘a’ is 97, etc.

Example: ord("a") is 97

chr() is the opposite of ord()

chr() is a function - it gives the corresponding character for given ASCII code. For example, to find the letter of the ASCII code 97 we write:

chr(97) which is ‘a’

Created by Mr Suffar

189 of 325

260. Create a program that displays the ascii code for the letter “C” and then displays the character for the ascii code 98.

Paste your code below:

Created by Mr Suffar

190 of 325

261. Create a program that:

  • Asks the user to enter a number between 97 and 122 inclusive.
  • Convert that letter to a character using chr() and then display the letter on the screen.
  • If an invalid number is entered, display “Incorrect number entered”.

Paste your code below:

Created by Mr Suffar

191 of 325

262. Ask user for name, convert each letter to character code then add the number (code) to a total and display the total.

Paste your code below:

Created by Mr Suffar

192 of 325

263. Ask user for name, convert each letter to a character code then add 1 to that number and convert the code back to a character and store it in a new string. If the letter is “z”, you need to convert it to the letter a. If the letter is b convert it to a c and so on. Now display the new name.

Paste your code below:

Created by Mr Suffar

193 of 325

264. Create a program that asks the user to enter a number from 5 to 30. If the user enters a number outside this range, display “invalid number entered”

If the user enters a number in the correct range, add 60 to that number, then convert it to ASCII character code using chr() and display it on the screen.

Paste your code below:

Created by Mr Suffar

194 of 325

Created by Mr Suffar

195 of 325

Revisit phase – COLD CALL

Identify the syntax and Logic errors in the code above:

Syntax: missing colon

Syntax: missing parenthesis

Logic: should be > instead of <

Syntax: should be indented. Missing Quotation mark also.

Syntax: incorrect spelling

Created by Mr Suffar

196 of 325

Revisit phase – COLD CALL

What is the definition of:

Syntax error:

  • When you break the rules of the programming language. Example: spelling mistake.

Logic error:

  • When your program runs however it produces an unexpected results.

Created by Mr Suffar

197 of 325

  • Explain the difference between count controlled and condition controlled loop.
  • Solve real life problems using a while loop.
  • Define the meaning of iteration

Iteration: While loop

Lesson 11

Tuesday, July 18, 2023

Lesson objectives…

Created by Mr Suffar

198 of 325

Knowledge phase�

Created by Mr Suffar

199 of 325

Knowledge phase�

Iteration: The repetition of a process/ section of code.

Examples: For loop, while loop and repeat – until loop (Pseudocode only)

Created by Mr Suffar

200 of 325

Count controlled iteration – For loop

An example of a count controlled loop is: For loops

A for loop is count controlled because you know the exact number of iteration that will occur.

Example:

The code will repeat 10 times.

Created by Mr Suffar

201 of 325

Condition controlled iteration – While loop

An example of a condition controlled loop is: While loop

A while loop is condition controlled because it will loop until a condition is met.

Example:

Condition

Colon

Indented which means

It is inside the loop

Created by Mr Suffar

202 of 325

Condition controlled iteration – While loop

The code below will repeat until the user enters “Suffar” when asked for a name. Then it will display “You are correct” once.

Note: Indentation indicates that the block of code is INSIDE the while loop.

Indented which means

It is inside the loop and will repeat

Created by Mr Suffar

203 of 325

Condition controlled iteration – While loop

You can set variables to either True or False to start a loop. Example below:

exits the while loop.

Created by Mr Suffar

204 of 325

Structure of a while loop

In a while loop, the process will repeat if the condition is True.

Created by Mr Suffar

205 of 325

Creating the same program using 2 different styles.

Example 1:

Example 2:

Created by Mr Suffar

206 of 325

Creating the same program using 2 different styles.

Example 1:

Example 2:

Created by Mr Suffar

207 of 325

Converting code from for loop to while loop

Example 1:

Example 2:

Created by Mr Suffar

208 of 325

Forever loop

The program below will repeat infinitely.

Created by Mr Suffar

209 of 325

Forever loop

You can exit a forever loop using the keyword “break

Created by Mr Suffar

210 of 325

COLD CALL

Created by Mr Suffar

211 of 325

Tasks

Complete the tasks

Created by Mr Suffar

212 of 325

265. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

count = 10

while count >= 0:

print(count)

count = count – 1

Answer:

Created by Mr Suffar

213 of 325

266. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

total = 0

num = 1

while num != 0:

num = int(input("Enter a number (0 to quit): "))

total = total + num

print("Sum of numbers:", total)

Answer:

Created by Mr Suffar

214 of 325

267. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

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

count = 1

while count <= num:

print(num, "x", count, "=", num * count)

count = count + 1

Answer:

Created by Mr Suffar

215 of 325

268. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

secret_number = 7

guess = 0

while guess != secret_number:

guess = int(input("Guess the number: "))

print("Congratulations! You guessed the correct number.")

Answer:

Created by Mr Suffar

216 of 325

269. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

base = int(input("Enter the base: "))

exponent = int(input("Enter the exponent: "))

while exponent > 0:

result = base ** exponent

exponent = exponent - 1

print("The result is:", result)

Answer:

Created by Mr Suffar

217 of 325

270. Copy the code below, run it and test it, then explain the purpose of the program/what the program does.

Explain the purpose of the code below:

sentence = input("Enter a sentence: ")

character = input("Enter a character: ")

count = 0

index = 0

while index < len(sentence):

if sentence[index] == character:

count = count + 1

index = index + 1

print("The number of occurrences of", character, "is:", count)

Answer:

Created by Mr Suffar

218 of 325

271. The program below must: ask user for a number, display whether the number is even or odd. Repeatedly ask for a number until a negative number is entered. When a negative number is entered. Exit the loop and display the total number of even numbers and the total number of odd numbers.

Copy the code below, run it and fix the errors.

even_count = 1

odd_count = 0

num = int(input("Enter a number")

while num >=1:

if num % 2 == 1:

even_count = even_count + 1

print(num"is even")

else

odd_count = odd_count + 0

print(num,"is odd")

num = int(input("Enter a number"))

print("Even numbers:", even_count

print(Odd numbers:", odd_count)

Paste the correct version below:

Created by Mr Suffar

219 of 325

272. The program below must: Repeatdly ask the user for a number until -1 is entered. Calculate and display the total and average of all numbers entered.

Copy the code below, run it and fix the errors.

count = 5

total = 2

while Truee:

number = integer(input("Enter a number (enter -1 to stop): "))

if number == -1

break

total = total+numbers

count = count+1

if count != 0:

average = total * count

print("The total is:",total)

print("The average is:", average)

else

print("No numbers were entered.")

Paste the correct version below:

Created by Mr Suffar

220 of 325

273. Create a program that: Repeatedly ask the user for a good football team name that wears “red shirt” until “Liverpool” is entered. If any other team is entered, display “not good enough”. When Liverpool is entered, display “Correct”.

team = _____("Enter team name")

while _____ != "_____":

_____("Not good enough")

team = _____("Enter team name again")

_____("Correct")

Copy the code above, fill the gaps then paste your code below:

Created by Mr Suffar

221 of 325

274. Create a program that:

  • Asks the user to input a password.
  • If password is correct, display correct password.
  • If password is incorrect, display incorrect password.
  • The program will repeatedly ask the same question until the correct answer is entered.
  • Assume the correct password is: “csgo1234”

Paste your code below:

Created by Mr Suffar

222 of 325

275. Create a program that:

  • Asks the user to enter 2 numbers that are the equal to each other.
  • Display the first number multiplied by the second number if they are both the same.
  • The program will repeatedly ask the user for 2 numbers until both numbers are equal to each other.

Paste your code on the next slide:

Created by Mr Suffar

223 of 325

276) Create a program that:

  • Asks the user to enter a number.
  • Adds that number to a total which starts from 0.
  • Displays the total.
  • The program repeats until the total is over 50.

Paste your code below:

Created by Mr Suffar

224 of 325

277. The school is holding an election to reward the top students: Create a program that:

  • Allow voters to enter either “Billy” or “Sarah” or “Chilly”.
  • Keeps track of how many times each student has been voted for.
  • Repeat the process until the user enters “STOP”.
  • At the end of the algorithm, output the number of votes for each student.

Paste your code below:

Created by Mr Suffar

225 of 325

278) Create a program for a cashier, your program must:

  • Ask the user how many items the customer wants to buy.
  • Ask for the price of each item and then add it to a total.
  • Display the total price for all items.
  • Use a condition controlled loop (while loop) to solve this task.

Paste your code below:

Created by Mr Suffar

226 of 325

279) A cinema wants to record how many children and how many adults buy tickets for the “lion king” film. Create a program that:

  • Ask the user for their age. If they are 18 or above. Then they count as an adult. Otherwise they count as a child.
  • Repeat the question until the user enters 0 or a negative number.
  • Display the total number of adults and number of children.

Paste your code below:

Created by Mr Suffar

227 of 325

280. Code the following flowchart in python:

Paste your code below:

Created by Mr Suffar

228 of 325

281. Use a while loop to create the 2 times table up to 100. Display “You have finished” when you get to the number 100.

Paste your code below:

Created by Mr Suffar

229 of 325

282. Display “Hey” and “Bye” on separate lines 1000 times using a while loop.

Paste your code below:

Created by Mr Suffar

230 of 325

283. Create a program that:

  • Asks the user to input 4 names.
  • Displays “You have entered 4 names” when all 4 names has been entered.
  • Use a while loop to repeat the question 4 times to enter 4 names in total.

Paste your code below:

Created by Mr Suffar

231 of 325

284. Create a program that:

  • Asks the user for a username.
  • If the username is less than 8 characters long, display “username is invalid” and repeat the question until a valid username is entered.
  • Asks the user for a password.
  • If the password is less than 8 characters long OR greater than 15 characters, display “Password is invalid” and repeat the question until a valid password is entered.

Paste your code below:

Created by Mr Suffar

232 of 325

285. Create a program that:

  • Asks the user to input a password.
  • If password is correct, display correct password.
  • If password is incorrect, display incorrect password.
  • Repeat the question until the user enters the correct password or reaches 4 attempts. After 4 incorrect attempts, display "Locked"
  • Assume the password is: "Rashford"

Paste your code below:

Created by Mr Suffar

233 of 325

286. Borednite is a new game. When a character takes damage in the game, their health bar goes down.

The health bar starts to increase (regenerate) 5 seconds from the last time a player was hit and by 15% per second. At 100%, the health bar has 500 health points.

Create an algorithm that:

  • Asks the user how many health points they were on since the last time they got attacked.
  • Calculates and display how many seconds it will take before the user reaches 100% health (500 points) from the last time they got attacked.

Hint: Use a while loop.

Paste your code below:

Created by Mr Suffar

234 of 325

Homework

Created by Mr Suffar

235 of 325

Revisit phase - Starter

Give 1 example of count controlled loop:

  • For loop

Give an example of a condition controlled loop:

  • While loop

What is the purpose of the code above:

  • Displays Hello 100 times.

What is the purpose of the code above:

  • Asks the user to enter a game repeatedly until the user enters “Fifa” as the answer

Created by Mr Suffar

236 of 325

  • Complete trace tables on while loop.
  • Solve real life problems using a while loop.
  • Use a if statement and iteration to solve problems.

While loop & Trace table

Lesson 12

Tuesday, July 18, 2023

Lesson objectives…

Created by Mr Suffar

237 of 325

Theory

Created by Mr Suffar

238 of 325

Example of a simple while loop trace table:

x

x < 5

Output

x = 1

While x < 5:

print(x*2)

x = x + 1

print(" The end ")

1

True

2

2

True

4

3

True

6

4

True

8

5

False

The end

Created by Mr Suffar

239 of 325

Assume the user enters the following inputs: 12,10,20,9

num

num>=10

Output

num = int(input("Enter number"))

while num >= 10:

print("Incorrect")

num = int(input("Enter number"))

print("Correct")

12

True

Incorrect

10

True

Incorrect

20

True

Incorrect

9

False

correct

Created by Mr Suffar

240 of 325

Assume the user enters the following values: “yes”, “no”, “chicken”

x

dancer

x < 3

choice

output

0

0

True

yes

great

1

1

True

no

Why not?

2

1

True

Chicken

Why not?

3

Created by Mr Suffar

241 of 325

Tasks

Complete task

Created by Mr Suffar

242 of 325

287. Complete the trace table. Assume the user enters the following values: “Tom”,”Sarah”,”Andy”,”Ed”

singer

singer != "Ed"

Output

Created by Mr Suffar

243 of 325

288. Complete the trace table.

x

y

x < 5

x < 3

Output

Created by Mr Suffar

244 of 325

289. Complete the trace table. Then write the final value of letter:

Final value of letter: rl

x

name

x < 4

Output

letter

Created by Mr Suffar

245 of 325

290. Complete the trace table. Assume the user enters the following numbers: 10, 50, 300,5, 1000

Highest

lowest

number

Output

Created by Mr Suffar

246 of 325

291. Complete the trace table. You may not need all the spaces in the trace table.

Enter the final value of z:

z = 0

x

y

z

total

Output

Created by Mr Suffar

247 of 325

292. Complete the trace table.

x

y

z

Output

Created by Mr Suffar

248 of 325

293. Complete the trace table.

num

num < 5

Output

Created by Mr Suffar

249 of 325

294. A league is holding player of the month selection. Create a program that:

  • Allow voters to enter either Salah or Vardy.
  • Keeps track of how many times each player has been voted for.
  • Repeat the process until the user enters “STOP”.
  • At the end of the algorithm, output the number of votes for each player.
  • Output a message to say whether Salah or Vardy won or if there was a draw.

Paste your code below:

Created by Mr Suffar

250 of 325

295. Create a program that:

  • Ask the user if they want to enter a number.
  • If the answer is not “NO”, ask them to enter an even number.
  • If the number entered is odd. Add 1 to it and display the new number.
  • Otherwise display the original number entered by the user.
  • The process repeats until the user enters “NO”.

Paste your code below:

Created by Mr Suffar

251 of 325

296. A shop owner has a charity box. She wants to create an algorithm that counts how many customers put money in the charity box. Create an algorithm that:

  • Asks the user if the customer donated or not.
  • If the customer donated, ask how much did that customer donate.
  • Calculate & display how many customers donated, the total amount of donation and how many customers didn’t donate.
  • Your program must repeat until the user enters “end”.

Paste your code below:

Created by Mr Suffar

252 of 325

297. Create a program which:

  • Asks the user to input a number
  • Check if the number is greater than 100
  • If it is bigger, display “End of Game”
  • If it is smaller:
  • Add the number to a running total, and ask them to enter a new number
  • Keep adding the numbers until the total is greater than 1000

Paste your code below:

Created by Mr Suffar

253 of 325

298. Create a program that:

  • Asks the user to enter a pin code that contains the number 1, 2 and 3 only.
  • If the code valid (must contain only 1s and 2s and 3s) then display “Pin code accepted”.
  • Otherwise ask the user to enter the code again.
  • Hint: Use a for loop to loop through the string.

Paste your code below:

Created by Mr Suffar

254 of 325

299. Create an algorithm that calculates the total sum of internal angles in degrees of a regular polygon using the following formula:  ”(N - 2) x 180“ where n is the number of sides. N will always be greater than 2.

Create a program that:

  • Asks the user for the number of sides. Repeat the question if the number of sides is less than 3.
  • Calculate and display the total sum of internal angles in degrees in a full sentence.

Paste your code below:

Created by Mr Suffar

255 of 325

300. Create a program that calculates the highest and lowest positive number entered. Your program must:

  • Asks the user to enter a number.
  • Repeat the above process until the user enters a negative number.
  • Display the highest and lowest number entered once the user enters a negative number.

Paste your code below:

Created by Mr Suffar

256 of 325

Task 301

Answer:

Created by Mr Suffar

257 of 325

Task 302

Answer:

Answer:

Created by Mr Suffar

258 of 325

Task 303

Answer:

Created by Mr Suffar

259 of 325

Task 304

Answer:

Created by Mr Suffar

260 of 325

305. Create a program that:

  • Create a voting system for students to check how many students like school and how many don’t.
  • Ask the user to vote “like” or “dislike”.
  • Count and display and how many students entered like and how many entered dislike.
  • Repeat the above until the user enters “STOP” when asked to enter a vote.

Paste your code below:

Created by Mr Suffar

261 of 325

Task 306

Answer:

Created by Mr Suffar

262 of 325

Task 307

Answer:

Created by Mr Suffar

263 of 325

TASK 308

Answer:

Created by Mr Suffar

264 of 325

Task 309

Answer:

Created by Mr Suffar

265 of 325

Task 310

Answer:

Created by Mr Suffar

266 of 325

TASK 311

Answer:

Created by Mr Suffar

267 of 325

Task 312

Answer:

Created by Mr Suffar

268 of 325

Homework

Created by Mr Suffar

269 of 325

Revisit phase – Starter – Data types

Identify 5 different data types used in programming and give an example of each?

  • Integer – Holds hole numbers.
  • Example: Age = 20

  • Real – Can hold decimal numbers.
  • Example: Temperature = 19.5

  • Boolean: Holds True or False
  • Example: Authorised = True

  • Character: Holds a single alphanumeric character.
  • Example: Initial = B

  • String: Holds alphanumeric characters including numbers that can start with a 0!
  • Example: Address: 250 Suffar Road
  • Example: phone number: 07002023231

Created by Mr Suffar

270 of 325

  • Identify examples of different programming constructs.
  • Identify which programming construct is used in a piece of code.
  • Solve real life problems using while loop.

While loop & programming constructs

Lesson 14

Tuesday, July 18, 2023

Lesson objectives…

Created by Mr Suffar

271 of 325

Theory

Created by Mr Suffar

272 of 325

Programming constructs

There are 3 programming constructs:

  • Sequence
  • Selection
  • Iteration

Created by Mr Suffar

273 of 325

Sequence

Sequence: The concept of one statement being executed after another.

All statements will run each time.

Example:

Created by Mr Suffar

274 of 325

Selection (conditional statement)

Selection: A condition is used to decide whether code should be executed or which statements are to be executed.

Example: If statement

Created by Mr Suffar

275 of 325

Selection (conditional statement)

Selection: May or may not run depending on a condition.

Example: If statement

Created by Mr Suffar

276 of 325

Selection (conditional statement)

Switch Case is another type of Selection.

It is a conditional statement to deal with many possible outcomes.

Example: Switch case (PSEUDOCODE ONLY)

Created by Mr Suffar

277 of 325

Iteration

Iteration: A construct where code is executed repeatedly a set number of times or until a condition is met.

Count controlled:

  • For loops

Fixed number of repetitions.

Condition controlled

  • While loops

Created by Mr Suffar

278 of 325

Iteration

Iteration: A construct where code is executed repeatedly a set number of times or until a condition is met.

Condition controlled – (PSEUDOCODE ONLY)

  • Repeat until

Created by Mr Suffar

279 of 325

Iteration

Difference between while and repeat until:

  • While: Has criteria check at start (pre-condition). Code inside while may never run.
  • Repeat until: Has criteria check at the end (post-condition). It will always run at least once.

Created by Mr Suffar

280 of 325

Questions – Cold Call

Which programming construct was used in the above program?

  • Sequence

Created by Mr Suffar

281 of 325

Questions – Cold Call

Which programming construct was used in the above program?

  • Iteration (Count controlled iteration)

Created by Mr Suffar

282 of 325

Questions – Cold Call

Which programming construct was used in the above program?

  • Sequence (line 1) & Selection

Created by Mr Suffar

283 of 325

Questions – Cold Call

Which programming construct was used in the above program?

  • Sequence

Created by Mr Suffar

284 of 325

Questions – Cold Call

Which programming construct was used in the above program?

  • Sequence (line 1) & Iteration (condition controlled iteration) line2 to line 3.

Created by Mr Suffar

285 of 325

Questions – Cold Call

Identify the line numbers where sequence occurred?

  • Line 1 and line 2.

Identify the line numbers where selection occurred?

  • Line 3 to line 6.

Created by Mr Suffar

286 of 325

Questions – Cold Call

Identify the line number where iteration starts

  • Line 1

Identify the line number where selection starts?

  • Line 3

Created by Mr Suffar

287 of 325

Created by Mr Suffar

288 of 325

Count vs condition controlled iteration

Count: The loop runs a set number of times which means the number of iteration is known before it starts.

Condition: The loop runs until a condition is met.

Created by Mr Suffar

289 of 325

Task 313

Created by Mr Suffar

290 of 325

Task 314

Answer:

Answer:

Answer:

Created by Mr Suffar

291 of 325

Task 315

Created by Mr Suffar

292 of 325

Task 316

Answer:

Created by Mr Suffar

293 of 325

Task 317

Created by Mr Suffar

294 of 325

Task 318

Answer:

Created by Mr Suffar

295 of 325

319. A supermarket does not allow customers to buy more than 3 toilet rolls. The supermarket charges £2 for 1 toilet roll, £6 for 2 toilet rolls, £12 for 3 toilet rolls.

Create an algorithm that:

  • Asks the customer how many toilet rolls do they want.
  • Calculate the total price depending on how many toilet rolls they buy.
  • Asks the question repeatedly if the user enters a number above 3 or a number below 1.
  • Outputs the total price.

Paste your code below:

Created by Mr Suffar

296 of 325

320. Create a program that:

  • Asks the user for username and password.
  • Checks if both username and passwords are correct.
  • Counts and displays the number of attempts it took to enter the username and password correctly.
  • Displays an error message if either the username or password is incorrect.
  • The program repeats until the user enters the correct username & password.
  • Assume the correct username = "David" and correct password = "abc"

Paste your code below:

Created by Mr Suffar

297 of 325

321. Create a program for a customer who wants to buy a group ticket to a cinema.

The following restrictions applies to a group ticket:

  • Number of tickets must be between 5 and 30.
  • Number of children must be between 5 and 25.
  • Number of adults must be less than 6.
  • Adult ticket cost: £8 and children ticket cost £6.

The program repeats until the customer enters valid inputs for the number of tickets, number of children and number of adults. Calculate and display the total amount needs to be paid.

Paste your code below:

Created by Mr Suffar

298 of 325

322. A ride in Chicken World has limits to who can ride. Customers must be at least 135 CM tall and must not be pregnant.

Create an algorithm that:

  • Asks the user to input the height in CM.
  • Ask if they are male or female. If female, ask if they are pregnant.
  • Repeat the process until 4 people are on the ride.
  • Display if they can ride or not.
  • Display “The ride is now full, enjoy your ride!” after 4 people have been allowed to ride.

Paste your code below:

Created by Mr Suffar

299 of 325

323) A cat café wants to know how the number of males and females entering his store in a single day and wants to create an algorithm for it. Create an algorithm that:

  • Ask the user to either type male, female or quit.
  • If the user picks male, add 1 to a variable called male.
  • If the user picks female, add 1 to a variable called female.
  • If the user picks “quit” then display the total number of males and females picked.
  • Otherwise display "wrong option".
  • The program repeats until the user enters quit.

Paste your code below:

Created by Mr Suffar

300 of 325

324. A supermarket is adding new measures to stop stock piling. Create an algorithm that allows the customer to buy a maximum of 1 hand sanitiser and 1 toilet roll and a maximum of 2 of the same items. The customers can not spend over £100.

Create an algorithm that:

  • Asks the customer how many hand sanitisers do they want.
  • Asks the customer how many toilet rolls do they want to buy.
  • Asks the customer if they have more than 2 of the same item.
  • Asks the customer for the overall cost of their items.
  • Cast each variable to an appropriate data type.
  • Outputs an error message and repeatedly asks the questions above until valid inputs have been entered.
  • Output a suitable message when valid inputs have been entered.

Paste your code below:

Created by Mr Suffar

301 of 325

325. Create a program that:

  1. Asks the user for an item original price and discount percentage.
  2. Calculate and display the new price of the item after the discount.
  3. Add the discounted price to a total that starts from 0.
  4. Display the total each time an item is added.
  5. Repeated the above until price entered is 0.

Paste your code below:

Created by Mr Suffar

302 of 325

Task 326

Answer:

Created by Mr Suffar

303 of 325

Task 327

Answer:

Created by Mr Suffar

304 of 325

Task 328

Answer:

Answer:

Created by Mr Suffar

305 of 325

329. Create a program that:

  • Repeatedly asks the user to input a number between 1 and 1000 until a number outside this range is entered.
  • Calculate the largest and smallest number entered between 1 and 1000.
  • Display the largest and smallest number at the end with the range of numbers (largest-smallest).

Paste your code below:

Created by Mr Suffar

306 of 325

Task 330

Answer:

Answer:

Answer:

Answer:

Created by Mr Suffar

307 of 325

Task 331

Answer:

Created by Mr Suffar

308 of 325

March 2023

Task 332

Created by Mr Suffar

309 of 325

Specimen1

Task 333

Created by Mr Suffar

310 of 325

Task 334

Answer:

Answer:

Answer:

Answer:

Created by Mr Suffar

311 of 325

November 2020 – v3

Task 335

Answer:

Answer:

Answer:

Answer:

Created by Mr Suffar

312 of 325

Task 336

Created by Mr Suffar

313 of 325

Task 337

Answer:

Created by Mr Suffar

314 of 325

Task 338

Created by Mr Suffar

315 of 325

Task 339

Answer:

Answer:

Answer:

Created by Mr Suffar

316 of 325

Task 340

Write your answer in python below:

Created by Mr Suffar

317 of 325

Task 341

Answer:

Created by Mr Suffar

318 of 325

Task 342

Created by Mr Suffar

319 of 325

Task 343

Answer:

Answer:

Created by Mr Suffar

320 of 325

Task 344

Answer:

Created by Mr Suffar

321 of 325

345. Create a program that:

Asks the user for a binary number. Validate it contains 8 characters only and must have 1s and 0s only. If valid, display “Binary number accepted”, otherwise, repeat the question again.

Paste your code below:

Created by Mr Suffar

322 of 325

Task 346

Answer:

Created by Mr Suffar

323 of 325

Task 347

Answer:

Created by Mr Suffar

324 of 325

Homework: Revise for end of unit assessment.

Complete kahoot:

https://create.kahoot.it/share/programming-constructs-for-loops/b386c8e4-ace7-434f-8837-2ef28efccebe

Created by Mr Suffar

325 of 325

End of unit test

NOW:

Complete end of unit assessment.

MS Form assessment - test link: https://forms.office.com/Pages/ResponsePage.aspx?id=NRrmRxWbmk-P3e1XFrwgvUKZUnHi_FpMtvQIsO11-sxURDQ0RkJHNTFQWDA2UTEwRFpKVVBDNEw3TC4u

THEN:

Complete any unfinished tasks from previous lessons.

Lesson 15

Created by Mr Suffar