GCSE - Python-All Tasks - With Helpsheets
GCSE - Python-All Tasks - With Helpsheets
GCSE - Python-All Tasks - With Helpsheets
Name:
Class:
VARIABLES, INPUT
AND OUTPUT
Q1 helpsheet
print("Hello World")
print(“I like cheese")
1. Display the following on 3 separate lines:
This is my first program.
It shows messages
Sometimes on different lines
Paste your code below:
2. Create a variable called food and store your favourite food
inside the variable. Print out the value of the variable onto the
screen.
Paste your code below:
Q2 helpsheet
food = "_____"
print(_____)
3) Ask the user for their favourite film. Display “I also
like watching”, film. E.G:
Paste your code below:
4. Create a program that ask the user for their firstname, surname, favourite
subject and age then display the above on 4 separate lines. “Remember you
can’t have space in a variable name.
Paste your code below:
Q3 helpsheet
food = _____("_____")
print("I also like", _____)
7. Ask for a user’s name and age. Display “your name is”, name,
“and you are”, age, “year old”.E.G:
Paste your code below:
Q7 helpsheet
Age Integer
House address
Name
Weight
Price
Is 5 == 5
9. Ask the user for 2 numbers then divide the first number by the
second number. Display the answer.
Paste your code below:
Q9 helpsheet
width = int(input("________"))
______ = int(input("Enter a height"))
area = _____*_____
print("The area of the rectangle is", _____)
11. Asks 2 users for their weight, calculate the average weight of
the 2 users.
Paste your code below:
Q11 helpsheet
Addition +
Subtraction -
Multiplication
Division
Exponent
Modulus
Floor Division
Q13 helpsheet
number1 = 8 # store the value 8 in a variable called number1
number2 = 6 #store the value 6 in a variable called number2.
answer= number1 + number2 #add 2 numbers together and store
them in a variable called answer.
print(answer) #displays the answer.
13. Create a variable x with a value of 5. Create a variable y
with a value of 3. Create a variable z with a value 10. Multiply
three numbers together and store them in a variable called
answer. Comment on the code.
Paste your code below:
Q14 helpsheet
number1 = 8
number2 = 6
answer= number1 + number2
print(answer)
14. Alex has £20. Spending:£5 on pens. £3 on pencils. Total
amount left?
Demonstrate this example using 4 variables and arithmetic
operators. Comment on your code.
Paste your code below:
15. Ask how many apples the user wants. Ask how many
people the user will share the apples with. Find out how many
apples will remain if you share the apples equally. Hint: use
modulus %.
Paste your code below:
Q15 helpsheet
KEYWOR Equal to, not equal to, greater than, less than
Relational operators
Question True/False
5 == 3
2 != 5
5>6
8<2
2!=2
7==7
1>=1
7>=2
9<=2
9<=9
Q18 helpsheet
answer=input("What is your answer? ")
if answer == "chocolate":
print("yum")
elif answer == "biscuits":
print("crunchy")
elif answer == "sweets":
print("chewy")
else:
print("I don't know what that means.")
18. Use the code on the previous slide to complete the following
table:
mancity = ____
manutd = ___
_____= _____ < ______
_____(score)
20. Create a program that asks for a person’s age. If the age is
greater than or equal to 18, display “You are old enough to
vote”, else display “You are not old enough to vote”.
Paste your code below:
Q20 helpsheet
age = __________
if ____>=18:
_____("You are old enough to vote")
____:
print(_________)
21. Create a program that asks for a person’s name. If the name
is equal to Tom, display “Welcome Tom”, else display “Hello
stranger”.
Paste your code below:
Q21 helpsheet
name = ___________
if _____ == "Tom":
print_______
____:
_____("Hello stranger")
Q22 helpsheet
print("Hello user")
singer = input("Enter your favourite singer")
if singer == "Beyonce":
print("Good singer")
elif singer =="Ed":
print("Pretty decent")
else:
print("Not too bad")
22. Create a program that Greets the user.
• Asks the user how they are feeling
• If the user enters “happy”, print “glad to hear it”
• If the user enters “sad” will tell the user a joke
• Has an error message for any other entry
Paste your code below:
Q23 helpsheet
You can only have one else and it has to be at the end.
27) Ask a user to enter a football team. If the user enters
Chelsea, display blue, else if user enters Liverpool, display red,
else display team not registered.
Paste your code below:
Q28 Helpsheet
You can only have one else and it has to be at the end.
30) Ask a user whether they want to take the red pill or the blue pill. If they
write “red” then print “red is the colour of blood”. Elif they write “blue”
then print “Are you sick?”. Else print “I don’t like that colour”
Paste your code below:
Q31 Helpsheet
name = input("Enter a name")
if name == "Steve":
print ("Hi Steve, how are you?")
elif name == "John" :
print ("Good to see you John")
else :
print ("I don’t know you")
You can only have one else and it has to be at the end.
31) Ask the user to enter traffic light colour, if colour is = red,
display STOP, else if colour = yellow, display get ready, else if
colour is = green, display GO, else display an error.
Paste your code below:
Q32 HELPSHEET.
hobby = ___________
hobby2 = input("user1's hobby is "+hobby+" What is yours?")
print(________)
Length
This example program asks the user to input a name. It then
finds the length of the string and stores it in the nameLength
variable. The length of the string is displayed.
drink = ___________
drinkLength = len(____)
print(_______)
3. Ask the user for their firstname and store it in a variable. Ask the user for
their surname and store it in a variable. Concatenate the two strings together
without space. Display the length of firstname+surname.
Paste your code below:
Q3 helpsheet
firstname = _________
surname = ___________
total = ___________
print(___(total))
3.5. Ask the user to enter a colour that contains 6 letters. If the user enters
the correct answer. Display “Well done”, else, display incorrect.
Hint: len(colour)
Paste your code below:
Q3.5 Helpsheet
colour = _________
if len(____) == 6:
print(______)
____:
___________
Substring
This can be used to extract a
specific part of a string.
Index 0 1 2 3
Character m a r k
name = "mark"
Up to but not
print(name[0:2])
included
Starting position
Substring
name = input("Enter a name")
print(name[0]) #displays the first character.
firstname = ____________
surname = _______________
print(firstname[___])
print(firstname[____])
print(surname[____])
Case Conversion
quote = ___________
print(quote.______())
print(quote._____())
6. Ask the user for their name. Ask the user if they want their name to be
displayed in capital letters or lowercase. Display the user’s name depending
on their answer.
Paste your code below:
Q6 Helpsheet:
name = ________
choice = __________
if choice.lower() == "______":
print(name._____())
else:
print(name._______)
6.5) asks the user to enter a quote. Asks the user to
input:
• the word they want to replace
• the word they want to replace it with
• Print the final quote. Hint: quote.replace(word1,word2)
Paste your code below:
Q6.5 Helpsheet
quote=___________
word1=_________
word2=____________
print (quote.replace(_____,____))
String manipulation
quote = __________
print(quote.______())
print(quote._____())
print(____.capitalize())
print(quote.____())
Formatting text
quote=__________
print ("i appears",quote._____("i"),_____)
8. Create a program that will allow the user to enter a quote by a famous
person. On separate lines, check if the quote contains letters, space, digits.
Paste your code below:
Q8 Helpsheet
quote = _____________
print(quote._____ ())
print(quote. _____())
print(quote. _____())
String manipulation
The following will split a string into a number of items
on a list.
hobby = ________
print(hobby.______("f"))
Iteration – For loop
Counter variable.
You can change the steps that each loop around will change the
counter value by
for y in range(100,0,-1) :
print(y)
number = __________
for i in range (_____):
print(___ * ___)
12. Ask the user for a number. Repeat ‘Hello’ a certain number of times,
based on a number entered by the user.
Paste your code below:
Q12 Helpsheet
number=________
for i in range (_____):
print(_____)
13. 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:
Q13 Helpsheet
name = __________
answer = ____________
for i in range (_______):
print(________)
14. 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:
Q14 Helpsheet
number = _________
for x in range(_____):
answer = _______
print (x, "x", _____, "____", _____)
15. Ask the user to enter a word and store it in a variable called word. For
each letter in that word, print the word. Hint: use for i in word:
Paste your code below:
Q15 Helpsheet
word = ________
for i in word:
print(_____)
Q16 helpsheet
hobby = input("Enter a hobby in a sentence")
split = hobby.split("a")
for i in range(len(split)):
print(split[i][0])
16. Ask the user to enter a quote. Split the quote at every “space”. Display the
first character in the quote after every space.
Paste your code below:
17) Write a program that displays all odd numbers between 1 and 99
Paste your code below:
Q17 Helpsheet
num = _
for i in range (____):
print(___)
num = ___+2
18) Ask the user their name and their age. Print their name the number of
times as their age.
Paste your code below:
Q18 Helpsheet
name = __________
age = __________
for i in range(____):
print(_____)
19) Write a program that asks the user to enter a number and then adds 5
to this and prints out the result. Make the program ask the question 6
times.
Paste your code below:
Q19 Helpsheet
for i in range(____):
num = _______
print(___+___)
20) Ask “how many negatives did you get?” if answer is 1, display prompt 10
times. Else if answer is 2, display reminder 50 times, else if answer is 3,
display warning 100 times, else display removal 500 times.
Paste your code below:
Q20 Helpsheet
negative = ___________
if _______ == 1:
for i in range(___):
print(____)
elif ________:
for i in range(50):
print(________)
elif __________
for i in range(____):
print(_________)
else:
for i in range(____):
print(__________)
21) Ask for the user’s age. If age is greater than 12, display happy birthday
100 times. Else display happy birthday 12 times.
Paste your code below:
Q21 Helpsheet
age = _________
if ___ > ___:
for i in range(____):
print(_______)
____:
for i in range(___):
print(_________)
WHILE LOOPS
Condition controlled.
WHILE
The WHILE statement is used to loop around code as long as
certain criteria are fulfilled. For example:
while x > 7:
this code will be repeated
so will this code
This code will not be repeated
Note that indenting (using the TAB key) is used to decide what
code is inside the while statement.
22) 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:
Q22 Helpsheet
number = 0
while _____ < ____:
print(____)
number = ____ + 2
print("You have finished")
23) Display “Hey” and “Bye” on separate lines 1000 times using a while
loop.
Paste your code below:
Q23 Helpsheet
number = _
while _____ < _____:
print(____)
print(_______)
number = _______ + 1
24) Ask the user to input 4 names. Display “You have entered 4 names”
when the user inputs all 4 names.
Paste your code below:
Q24 Helpsheet:
number = 0
while number != ___:
name = ________
print(_____)
______ = number + 1
print(________)
25) Ask the user to input a password. If password is correct, display correct
password. If password is incorrect, display incorrect password. Display
“LOCKED” after 4 attempts.
Paste your code below:
Q25 Helpsheet
attempts = 0
while _____ != 4:
password = _________
attempts = _______ + 1
if ________ == "stfrancis":
print(_________)
attempts = 3
_____:
print(______)
26) Ask the user to input a password. If password is correct, display correct
password. If password is incorrect, display incorrect password. The program
will loop until the correct answer is entered. Hint: use attempts = False
Paste your code below:
Q26 Helpsheet
attempts = _____
while ______ == False:
password = __________
if ______ == "stfrancis":
print(_____)
______ = True
____:
print(____)
27)
1- Ask the user for a username.
2- While the username is less than 8 characters long,
3-Display “Invalid username, enter another username”) and force the user to
enter another username.
4- Display “your username has been accepted” if the username is at least 8
characters long.
5- Ask the user for a password.
6- While the password is less than 8 characters long OR greater than 15
characters long,
7- display “Invalid password, enter another password”) & force the user to
enter another password.
Display “your password has been accepted” if the password is between 8 and
15 characters long.
Hints: use while len(password) < 8 or len(password) > 15:
Paste your code on the next slide:
Q27 Helpsheet
username = __________
while len(______) < 8:
username = __________
print(____________)
password = ________
while len(_____) < 8 or ___(password) > 15:
password = ________
print("Your password has been accepted")
28) Write a program that allows the user to type in a number. This number
will then be added onto a total, which will be displayed. Continue to ask for
a number until the total is over 100.
Paste your code below:
Q28 Helpsheet
total = 0
while total < ____:
number = ________
total = total+______
print(____)
RANDOM NUMBER PICK
import random
x = random.randint(1,100)
print(x)
import ______
x = _________
print(___)
number = ________
print((x+number) / ___)
30) Guessing game. Store a random number between 1 and 100 in a
variable using random.randint(1,100). Ask the user to guess it. Display a
“too high”, “too low” or “well done” message. Keep asking the question
until they get it correct.
Fill the gaps in the code on the next slide.
import random
random = _____________ # stores a random number.
number = int(_____________))
while True: # infinite loop.
if number > random:
print(__________)
number = int(input("Enter a number"))
elif number < random:
print(__________)
number = int(_______________))
else:
print(__________)
break #breaks out of loops.
31) Store 2 random numbers in 2 different variables. Ask the user to work
out the answer when these two numbers are multiplied. Mark their answer
as right or wrong. Repeat this 5 times using a for loop.
Fill the gaps in the code on the next slide.
import __________
for i ________:
first = random.randint(1,10)
second = _______________
answer = __________
guess = int(input(str(first)+" x " + str(second)+ " = "))
if guess == ________:
print(__________)
else:
print(_______________)
Lists and arrays.
1D ARRAYS
Theory: Understanding Arrays
array = ["saw","batman","superman"]
for x in array:
print(x)
We can loop through an array using a for loop.
array = ["saw","batman","superman"]
film = input("Enter a film to see if it's in the
array")
for i in range(len(array)):
if film == array[i]:
print ("found")
34) Write a program that checks whether an element occurs in a list. For
example: array = [4, 6, 7, 8, 10]. Which number would you like to find? 10
Found!
Paste your code below:
Q34 Helpsheet
array = [2,3,4,5,6]
number=_________
for i in range(___(array)):
if number == array[___]:
print (____)
Append means add #Declare an empty array
(something) to the end. student = [ ]
array = []
for i in range (___):
game = _______
____.____(game)
print(____)
Slicing:
array = [2,3,4,5]
print(array[:2]) #prints index 0 & 1
print(array[2:]) #prints index 2 & 3
print(array[1:3]) #prints index 1&2
array = []
for i in range (___):
number = _________
_____.____(number)
array._____()
print(____)
37) Ask the user to enter 3 games and store it in an array. Sort the list in
alphabetical order. Hint: array.sort()
Paste your code below:
Q37 Helpsheet
array = []
for i in range (___):
game = ______
array.____(____)
____.____()
print(array)
37.5) Extend your previous program so that after adding the games, the user is
asked which index number they want to see. This game is then printed out. For
example, if they enter 3 then they should be shown the 3 rd game in the list.
Hint: print(array[number-1])
Paste your code below:
37.5 helpsheet
array = []
for i in range (_):
game = ________
array.______(_____)
array.____()
number = ________
print(_____[____-1])
examples!
In all of these examples, replace arrayname with the name of your array/list.
array = []
for i in range (_):
film = _______
_____._____(______)
print(array[____])
array._____(____[-1])
print(array)
print(array[_])
38.5) Ask the user to enter 6 numbers which are then stored in
an array. The user should then be able to choose to see either
the total or the average of these numbers. Print out the answer.
Paste your code below:
38.5) If you are struggling with the question use the code below:
score = []
for i in range (__):
number=______________
score.append(____)
option = _________________
total = score[0]+______________
if option == "total":
print(_____)
elif option == "average":
print(______)
else:
print("Invalid option")
38.6) Create a questionnaire program that asks the user for their
gender and gives the options MALE/FEMALE/QUIT. Store the
answer in an array. Keep asking the question until someone
enters QUIT. Then print out the total number of people who
answered the question, the number of males and the number of
females. Use the code on the next slide if you are struggling.
Paste your code on the next slide:
Q38.6)
males = _
females = _
storage = []
gender = ____________
storage.append(_____)
while gender.lower() == "male" or ____________________:
gender = input("Are you a male or female, or quit? ")
____________(gender.lower())
if gender.lower() == "quit":
for item in storage:
if item.lower() == _______:
males = _____ + 1
else:
if _______== "female":
females = females + ___
print(str(males) +" males")
print(str(females) +" females")
38.7) Create a program to store the names of computer games
that they play in an array. Create a simple menu system to allow
the user to either add, edit or delete a game. Also allow the user
to print the array whenever needed.
Use the code on the next slide if you are struggling.
Paste your code on the next slide:
games = [] Q38.7)
while True:
menu = input("\nDo you want to add, edit or delete a game, or print all games? ")
if _____ == "add":
newgame = input(___________)
games.append(________)
print("Game '"+newgame +"' added. ")
elif menu == "print":
print("Your games are: ")
print(_______)
elif _____ == "edit":
print("Your games are: "+str(games))
editone = input("Which one do you want to edit?")
edittext = _____("What do you want to change it to?")
games.______(editone)
games.______(edittext)
elif _____ == "delete":
print("your games are: "+str(games))
delete = input("Which game would you like to delete?")
games.______(_____)
____:
print("Invalid option")
2D ARRAYS
array = [ [3,2,1], [2,3,4], [7,5,2] ]
print(array[2][1])
print(array[0][1])
Plonker", 23,12,32]]
print(grades) # displays the list.
print(grades[1][2]) # displays 22
grades[1].append(56) # adds 56 to billy
array, element, table, assigning, identifier
KEYWOR
39) Write a program that converts the following table into a two-
dimensional array called ‘grades’:
Student name Grade1 Grade2 Grade3
Paste your code below: Alfie Little 24 32 5
Billy Bob Junior II 22 22 53
Mark Jones 43 54 23
King Plonker 23 12 32
40) Modify Mark Jones Grade2 to 76 from the previous question. You will
need the following: grades[index][index] = 76
Paste your code below:
41) Add ‘Grade4’ to your array with the following data: You will need the
following: grades[x].append(value) Student name Grade4
Alfie Little 37
Paste your code below: Billy Bob Junior II 99
Mark Jones 32
King Plonker 42
Q41 Helpsheet
import _____
array=[]
for i in range(_):
singer = ________
array._____(____)
number=_____.randint(____)
print(array[_____],"has been chosen")
45) Ask the user to enter the name of 3 singers, stores them in
an array. Sort the array in alphabetical order then display each
singer in the array on a separate line with their position.
Hints: array.sort() & len(array)
Paste your code below:
Q45 Helpsheet
array=[]
for i in range(_):
singer = ________
array.________(______)
array.____()
for x in range(___(array)):
print("Singer number",x+1,"is",array[___])
46. Ask the user to enter a game, then output the numbers from 0 to the
number of characters in the game entered. Display each number on separate
line.
Paste your code below:
Q46 Helpsheet
game = __________
for count in range(0, ___(____)+1):
print(____)
47. Ask the user to enter a quote. Split the quote into individual words by the
spaces. Display each word on a new line.
Paste your code below:
Q47 Helpsheet
quote = ___________
split = quote.____(___)
for x in range(len(____)):
print(split[_])
48. Create a random number generator that generates 5 random numbers
and displays it on the screen. Store the numbers in an array. Sort the list in
order then display it.
Hint: use array.append and a for loop.
Paste your code below:
Q48 Helpsheet
import _____
array = []
for x in range (___):
number = _____.______(1,100)
print("The random number is", _____)
array._____(____)
array._____()
print(_____)
49. Random number generator. Ask the user how many numbers they want to
generate. Ask the user for the lowest and highest number they want to generate.
Display each number generated. Store the numbers in a list. Display the list in reverse
order. Hint: array.reverse
Paste your code below:
Q49 Helpsheet
import random
array = []
number = ___________
lowest = ____________
highest = _______________
for x in range (______):
randomNum = ______._____(_____, _____)
print("the random number generated is:", ________)
array.______(_______)
_____.reverse
print(______)
Python 3 programming tasks
FILE HANDLING
Writing to a file
userinput = ___________
file = open(______, "w")
text = __________
file._____(text)
____.close()
fileReading = ____(userinput, "r")
read = _________
______.close()
print(read._____())
print(_____.upper())
5. Ask a user if they want to read or write. If they answer read, the filename specified
is opened and the contents of the file is read and displayed to the screen. If they
answer write, then ask the user to write a sentence to the file. Hint: use if statement &
create a .txt file in advance.
Paste your code below:
Q5 Helpsheet
ask = ________________
if ____ == "write":
file = open("_____", "w")
text=_____("Enter a sentence")
file.write(____)
____.close()
____ ask == "read":
fileReading = ______("______", "r")
read = fileReading._____()
_____.close()
print(read._____())
print(_____.upper())
____:
_____("Wrong answer!")
Appending “adding to the end” data to a file.
name = _________
data = ___("data file 1.txt", "__")
data.____(____+"\n")
____.close()
name2 = _______
dataFile = ____("data file 1.txt", "___")
dataFile._______(_____)
______.close()
7. Update your last program by adding the following at the end of your program on a
new line: Alex hunter
Paste your code below:
8. Ask the user to input 5 numbers and append these to the end of the text file. Hint:
use a for loop.
Paste your code below:
Q8 Helpsheet
array= ___
for x in range (____):
name=_____
array._____(____)
file= ____("____", "____")
for names in _____:
file._____(_____+"\n")
file.____()
10. Ask the user how many numbers they want to enter. Let them enter this many
numbers and write them to a text file. Each number must be on a separate line.
Paste your code below:
Q10 Helpsheet
user = ________
file = ____("_" , “_")
for x in range(____):
number = _____("Enter number " + str(___ + 1) + "\n")
file._____(_____+"\n")
file.____()
11. Write a program that asks the user for their personal details including: first name,
surname, age and address. Store them in a file.
Paste your code below:
Q11 Helpsheet
firstname=_________
surname=__________
age=_________
address=___________
file = ____("____", "__")
file._____("First name: "+firstname+", surname:" ______)
____.close()
12. Ask the user to enter the name of 5 different games. Store them in a file. Read the
data from the file and store it in a list. Separate each of them by a comma.
Paste your code below:
Q12 Helpsheet
import ______
teamList = _____("team.txt", "__")
data = teamList._______()
randomChoice= random._______(____)
teamName =[]
player =[]
for lines in data:
split = lines._____(',')
teamName.______(split[0])
player._______(_____[1])
teamName = teamName[_______]
letters = _______[randomChoice]
print("\nThe team is ",______)
splitLetters = letters._____(' ')
print("And the first letter of the player’s firstname and surname is")
for x in range(_____(splitLetters)):
print((______[x][_]).upper())
14. Write a program that allows the user to create and store a checklist for their
homework. Ask for pupil’s name, and create a text file using the pupil’s name.
Ask how many homeworks they need to complete. The user should then be able to
enter each homework name. Store them in a file.
Paste your code below:
Q14 Helpsheet
homework=[]
name=__________
homeworkNum=__________
for i in range(0,_____):
homework._____(input("Enter the name of homework
"+str(i+1)+": "))
file = _____((name+".txt"), "_")
for item in ______:
file.write(_____+"\n")
file._____()
Procedures
• A subroutine or a subprogram is a
named self-contained section of code
that performs a specific task.
• Parameters: Specific variables used to
pass values into a sub program.
• Arguments: Actual values that the
parameters take when the sub program
is called.
KEYWOR Procedure, function, subroutine, subprogram, return, parameter
PROCEDURE
Function/procedure
def is a name
command
Ask the
which allows
def firstSubroutine(): user to
you to define a
name=input("what is your name") input a
new function / print(name) name.
procedure. firstSubroutine()
firstSubroutine()
Calls the Displays
procedure. This the user’s
will run line 2 answer.
and 3.
KEYWOR Procedure, arguments, subroutine, subprogram, parameter
PROCEDURE
Function/procedure
name. Parameter.
def is a
command def welcome(name):
which allows print(“Welcome to school“ , name )
Displays
welcome("Alex")
you to define a welcome to
welcome(“Tom")
new function. school + the
value of the
Calls the argument.
procedure. This
will run line 2. Argument.
Parameters
def calculate(num,num2):
print(num * num2)
calculate(2,4) Local
Variable
Global
variable
15. Create a procedure that will ask the user for their name. Display the name.
def firstname():
ask = ________
print("Your first name is", ____)
____ lastname():
ask = __________
print("Your last name is", ____)
def ____():
ask = input("What is your age")
print("You are", ____, "year old")
_____("Which question would you like to answer")
question = ______("Press 1 for firstname, press 2 for lastname, press 3 for age."))
if question == 1:
_______
____ question == 2:
_______
____ question == 3:
______
___:
print("Wrong answer")
18. Use procedure with parameter. Ask the user to input numbers until they say “no”.
Output if each number is greater than or less than or equal to 5.
Paste your code below:
Q18 Helpsheet
___ output(number):
if ____ > 5:
____("Greater than 5")
____ number < ___:
_____("Less than 5")
____:
_____("It is equal to 5")
userInput = "yes"
____(______.lower() != "no"):
num = ____("Enter a number \n"))
output(____)
userInput = ____("Would you like to try again?")
19. Create 2 procedure with 2 parameters. Ask 2 users for their name & age then display
which user is older/younger.
Paste your code below:
Q19 Helpsheet
def calculate(num,num2):
Stores the
value
answer = num * num2
returned return answer
in the
function
output = calculate(2,4) answer returned to
the main program.
print(output) Local
Variable
Arguments, the values 2 and 4 are passed into num and num2.
Parameter passing
You can return multiple values.
Parameters
def calculate(num,num2):
Stores the
value
message = "your answer is"
returned answer = num * num2
in the Message & answer
function
return message, answer returned to the main
output = calculate(2,4) Variable
Local program.
print(output)
____ function(currentage,year):
futureAge= _____ + _____
_____ futureAge
age = ___________
ask = _____("Would you like to know how old you will be in 18 year in 12"))
num18= _____(age,18)
num12= function(___,____)
if ask == 12:
print(_____)
elif ____== 18:
print(_____)
___:
___("invalid option")
22. Create a function to return the value of 3 numbers multiplied by each other. Ask
the user to input these numbers. Use 3 arguments and 3 parameters.
Paste your code below:
Validation: to check that the data entered by a user or from a file
meets specified requirements. Validation does not check if the
data is accurate.
1. Range check
2. Valid choice check
3. Length check
4. Presence check
5. Lookup check
6. Validation
Range check
validnum = False
5”))
tickets validnum
if tickets > 07 and tickets < 6:
False
-8 False
validnum = True
3 True
else:
again”)
print(“You Validation,
have purchased”,
rangetickets,
check,“tickets”)
value, while,
KEYWOR
We can also use these additional built-in
functions:
• isalpha() – returns true if all the
characters in a string are letters.
• isdigit() – returns true if
characters in a string are numbers.
• isalnum() – returns true if all the
characters are either letters or
numbers.
• isupper() – returns true if
characters in a string are upper
case.
• islower() – returns true if
characters in a string are lower
case.
KEYWOR Validation, range check, value, while,
23. A teacher is booking a school trip to Cadbury world. Create a program that will
allow the teacher to enter how many students they intend to take. They are unable to
take fewer than 25 students and no more than 36.
Paste your code below:
LENGTH CHECK
lengthokay = False
again”)
E Mr_Suffar
validChoice = _____
while _____ == False_
username = _____
password = _____
password2= _________
if _____ == password2 and len(____) >=2 and ___(username) <= 20 and
len(_____)>=8:
print("Correct username & password")
validChoice = _____
____:
print("Invalid details")