Sequence Loops Selection (IF ELIF ELSE) Handling Strings Random Lists Reading and writing files
Procedures and Functions
Order of steps in a algorithm. The next step will not start until the previous step has completed.
Instructions performed are determined by the outcome of a condition ( age < 18 ... is an example of a condition)
Instructions can be repeated until a set condition has been met. (password == 'letmein' ... is an example of a condition)
1] Create a new folder called Car Number Plate Generator inside your Python folder.
2] Open Thonny ... Save the blank file as Car Number Plate Generator inside the folder you just created.
3] Video 1
4] Video 2
5] Video 3
6] Video 4
Challenge 1 - Can you add brief comments to each line of code to explain what is going on? ... Use the # to add comments
Challenge 2 - Can you create a program that generates a National Insurance Number? Click here to see what they are.
e.g. National Insurance Number - FD 12 43 98 C
Hint - You will need to create variables to store random letters and numbers - re-use some of the code from the number plate generator program
e.g. Variable 1 - To store the first random letter ... Variable 2 to store the second random letter ... Variable 3 to store the first random number ... etc
Challenge 3 - Can you add validation to the input. For example, what if someone enters 1 letter for the county in the number plate generator program?
Or what if someone enters 17 for the car year, when they should enter 2017?
Can you think of where else you can validate the user’s input?
1] Create a new folder called Number Guessing Game inside your Python folder. (if you already have a folder called this ... then just name the folder as Number Guessing Game 2)
2] Open Thonny ... Save the blank file as Number Guessing Game inside the folder you just created. (if you already have a file called this ... then just name the file as Number Guessing Game 2)
***********
You can open the following videos from the school network if you prefer ... remember to open them in Windows Media Player not Quicktime.
Resources > subjects > Computer Science > Year 9, 10, 11 > GCSE 9-1 > Programming Techniques > Number Guessing Game using both loops
***********
3] Video 1
4] Video 2
5] Video 3
6] Video 4
Challenge 1 - Can you add brief comments to each line of code to explain what is going on? ... Use the # to add comments
Challenge 2 - Can you create a variable that stores the total number of guesses throughout all 3 rounds
Challenge 3 - Can you get the program to display your accuracy as a percentage for each round?
Hint ... use LEN ( ) to find the length of a string ... use a WHILE LOOP
Hint ... use LEN ( ) to find the length of a string ... use a WHILE LOOP
1] Create a new folder called Conversion Program using Procedures inside your Python folder.
2] Open Thonny ... Save the blank file as Conversion Program using Procedures inside the folder you just created.
3] Video 1 ... WATCH / PAUSE / DO
4] Video 2 ... WATCH / PAUSE / DO
Challenge - Can you add an extra procedure that converts cm to inches and inches to cm? ... you will need to add extra options to the menu (e.g. Press 3 to convert CM to Inches ... Press 4 to convert Inches to CM ... you will also need to add the numbers 3 and 4 to the list of valid choices.
There are 0.39 inches in 1 cm
1] Open Thonny ... Save the blank file as Binary to Denary Program 1 inside your Python folder.
2] Video 1 ... WATCH / PAUSE / DO ... You will learn how to convert a binary number into a denary number
3] Video 2 ... WATCH / PAUSE / DO ... You will learn how to use a FOR LOOP to make your program run efficiently by executing fewer lines of code
Challenge 1 - Can you validate the INPUT so that only a binary number of 8 characters can be entered?
Challenge 2 - Can you validate the INPUT so that letters cannot be entered? HINT
Challenge 3 - Can you create a FUNCTION so that you pass in the binary number to the function, and it returns the denary number?
1] Open Thonny ... Save the blank file as Denary to Binary Program 1 inside your Python folder.
2] Video 1 ... WATCH / PAUSE / DO ... You will learn how to convert a denary number into a binary number
3] Video 2 ... WATCH / PAUSE / DO ... You will learn how to use a FOR LOOP to make your program run efficiently by executing fewer lines of code
Challenge 1 - Can you validate the INPUT so that only a number between 0 and 255 can be entered?
Challenge 2 - Can you validate the INPUT so that letters cannot be entered? HINT
Challenge 3 - Can you create a FUNCTION so that you pass in the denary number to the function, and it returns the binary number?
ULTIMATE CHALLENGE - Can you create a menu system so that if you enter 1 ... it will convert a binary number into a denary number ... and if you enter 2 ... it will convert a denary number into binary number? - You will be a coding god/goddess if you can do this :)
Learning Objectives
To learn how to write data to a text file in Python.
Click on this link to access the videos you will need for this lesson. The videos can also be found in ...
resources > subjects > Computer Science > Year 9, 10, 11 > GCSE 9-1 > Programming Techniques > File Handling > Writing to a text fileThe videos go through the basics of writing data to a text file. The videos also explain how to answer a typical GCSE pseudocode question that uses FUNCTIONS.
1] Watch video 1 - This explains the Pseudocode for writing to text files - watch this fully
2] Video 2 - 5 will show you how to write data to a text file and create a function. Follow the video carefully and do the skills (e.g. Creating a folder / Creating a text file / Coding in Thonny) ... watch / pause / do
Extra Task (You must watch the videos 1 - 5 and carried out the skills, before approaching this task)
Can you create a function that creates new computer usernames
1] The function will need to take as INPUT ... the student's firstname ... the student's lastname ... the year the student started in Year 7
2] The function will need to create a username that could look like the following ... 12simpshom ... assuming that the name was Homer Simpson and started Year 7 in 2012.
3] The program must then write the new username to a text file called newusernames.txt
Extension
Can you make the program ask the user to enter 10 new usernames and each time a username has been created it adds it to the text file?