Loading...
Welcome to today's lesson from the unit "Programming: dictionaries and data files." This lesson is called "Careers in programming," and by the end of today's lesson, you'll be able to identify the skills and habits good programmers develop and use a range of approaches to develop a.
We'll be looking at a number of keywords in today's lesson.
Let's take a look at them now.
Programmer.
Programmer, a person who writes computer programmes Solution.
Solution, a means of solving a problem.
Append.
Append, a file handling mode that allows a programme to write extra data to the end of a file without erasing the existing content, file.
Look out for these keywords throughout today's lesson.
Today's lesson is split into three sections.
We'll start by determining the good habits of a programmer.
We'll then move on to explore approaches to programming solutions, and then we'll finish up by appending, CSV file.
Let's make a start by determining the good habits of a programmer.
Think about your experience of programming and what you've learned about programming so far.
Make a list of good habits a programmer should adopt.
Pause the video whilst you do that now.
What did you have on your list? Let's have a look at some sample answers.
Laura says, "Break a problem down into subroutines." Lucas says, "Test often and keep backups." Jun says, "Write code that is easy for others to read." And Izzy says, "Comment on code to make it clear what each part does." There's some great things that you may have included on your list.
Do you have anything different? Use meaningful identifiers.
You want to make a variable that holds a score for a game, but you name it bob.
Will the programme still work? Yes, the programme will still work, but it's not a good idea.
It's not a good idea to name a variable that holds a score bob because it will make the code difficult to read and understand, and you may lose track of what the variable does later on in the programme.
At the moment, it is difficult to know the purpose of this code.
This is because meaningful identifiers have not been used.
By changing the identifiers to something meaningful, the code becomes easier to understand.
So we now can clearly see that the variable is holding a score, and if the answer is "yes", then we're incrementing that score by the value of 2.
Following naming conventions.
Programming languages use naming conventions that are typically specified in their documentation.
This is to make the code easier to read and to keep consistency across programmes.
So you can see here we have a variable called LEVELSCORE, and it's been named in various different ways.
Time to check your understanding.
Which of these variables has been declared using the correct Python conventions, a, b, c or d? Pause the video whilst you have a think.
Did you select d? Well done.
Would the other variable names still work? Yes, they would, but they don't follow Python conventions.
In Python, variables are named with lowercase letters and separated by underscores.
Commenting.
Code should be clear and readable.
Adding comments to help provide extra insight into what the code is doing.
This doesn't mean that you should comment on every single line of your code.
The amount of comments in this code are unnecessary.
A sensible amount to add clarity is helpful.
Start small.
Problems can often feel too big to solve.
All programmers experience this at varying levels.
The trick is to start small and decompose the problem.
Break a large problem down into smaller subproblems. If a subproblem is still too challenging, break it down even further.
Time to check your understanding.
The process of breaking a large problem down into smaller subproblems is known as, a, iteration, b, decomposition, or c, algorithmic thinking.
Pause the video whilst you have a think.
Did you select b, decomposition? Well done.
Process of breaking a large problem down into smaller subproblems is known as decomposition.
For example, if you were asked to create a "guess the number" game that will select a random number between 1 and 100, allow for five guesses, give feedback like higher or lower, tell the user if they won or lost, and reveal the number of guesses, you might start by seeing if you can check if the guess is correct by writing some code.
Once this bit is working, you'll then move on to the next small chunk.
What do programmers say? Maimuna Ahmed is a software engineer at the Raspberry Pi Foundation.
She says, "Always make sure your code is accessible for anyone.
This means no matter what level of programming someone is at, they should be able to easily read and understand what you have done.
Don't forget to include comments! They are helpful for understanding and explaining code steps, especially when starting out programming." There's some really good advice there.
Okay, we're moving on to the first task of today's lesson.
I'd like you to explain good habits that a programmer should adopt and explain why these habits are important.
Pause the video whilst you complete the task.
How did you get on? Did you manage to explain some good habits that a programmer should adopt? Let's have a look at a sample answer together.
Programmers should get into the habit of commenting code.
Comments do not need to be added to every line of code.
Adding a sensible amount of comments can help to provide extra insight into what the code is doing.
Programmers should also adopt naming conventions for variables and use meaningful identifier names to ensure the values held in variables are clear.
Finally, programmers should decompose large problems into smaller subproblems and solve each part individually before moving, the next.
Did you have a similar answer? Remember, you can always pause your video here and add extra detail if you want to.
So we've determined the good habits of a programmer.
Let's now move on to explore approaches to programming solutions.
When writing programmes, there are always plenty of ways to reach the final solution.
Taking the time to explore and experiment can make a real difference to your own skills and the solutions you develop.
During this unit, you've learned quite specific ways to find solutions, but there are always alternatives.
You'll become more confident with using different approaches as your programming skills develop.
In some lessons, you will have used the input() function in this way.
It's broken down into two steps.
So print("Enter your name:").
Name is equal input().
So we provide the instruction and then we prompt for the input.
You've learned to do it this way because it allows you to see that there are two separate steps taking place, and in some programming languages, you must handle user input this way.
In Python though, there is an alternative approach.
You can pass the instruction as an argument.
So here we've combined two lines of code into a single line.
Name is equal to input, open brackets, and then speech marks that prompt for your name.
When you execute this programme, it will allow the user to type their name right next to the prompt.
You can see here we have the example where the user has typed their name, Izzy.
This is how you've learned how to increment a variable in Python.
So score is equal to 4, score is equal to score + 1.
You've learned it this way so that you can clearly see that you are adding a value to the value score.
This is an alternative approach, score is equal to 4, score += 1.
Seeing this as a beginner programmer can be a little confusing, but as your confidence builds, you find it much easier to use this approach instead.
You have been using f-strings to display variables within a string.
So for example, we have a print line on line 3 here, which uses an f-string, print, open brackets, f and speech marks, Name:, the variable {name}, which is held in curly brackets, Score:, {score}, which is held in curly brackets, closed speech marks, closed bracket.
This is a really clean way to present variables, but there are alternative approaches that you might find helpful in some situations.
One approach is to use commas to separate the variables and the string.
So this example on line 3 now has print, "Name:", comma, name, comma, "Score:", comma, score.
Close your brackets.
Does exactly the same thing.
It's just formatted.
You can also concatenate the string using a + sign.
This requires an extra step if any of your variables are not of the string data type.
You can see here we've used a + instead of comma.
If our variable is an integer and not a string, we need to cast it as a string before we use this print statement.
This programme is designed to iterate through a list from the fourth item to the final item.
A separate variable, which is called numbers_length, has been used to hold the length of the list.
Again, this approach steps through the process for you, making it easy to understand what the code is doing.
Here is an alternative approach.
There is no need to have a separate variable because len(), len() function can be called directly inside the parameters.
So you can see here on line 3, we have for x in range, open bracket, 3, comma, len(numbers), closed bracket again.
Okay, we're moving on to our second task of today's lesson.
I'd like you to open the starter code at oak.
link/alternative-solution.
For part two, execute the code to make sure you know how it works.
For part three, change the variable names so that they use meaningful identifiers if they don't already use them.
For part four, add comments to the code to improve readability and clarity.
For part five, amend the code to provide an alternative solution.
Here are some ideas.
Display the instruction and input prompt on one line.
Increment a value using += 1.
Display variables within a print statement using commas to separate the variables.
Pause the video here whilst you complete the.
How did you get on? Did you manage to use some alternative solutions? Here's some sample code.
Remember, you may have done something slightly different, and that's absolutely fine.
If you want to look at this code in more detail and run it, you can go to oak.
link/ alternative-solution-solution.
Let's have a look at some key things in this code.
So on line 6 and line 18, we've added some comments in.
We've ensured that all of the variable use sensible, meaningful identifiers, and we've made some amendments in the way that we've done things.
So on line 11, we have guesses += 1 to increment the guesses value by 1.
On line 19, we're using commas in our print statement instead of an f-string.
We're now moving on to our final part of today's lesson where we're going to append to a CSV file.
You have learned how to write to a CSV file.
You'll now see how to append to a CSV file and complete a programming challenge.
Remember, appending to a file means adding to the existing content rather than replacing or overwriting it.
Appending to a CSV file is just like appending to a standard text file.
So you can see on line 1, we're opening the file numbers.
csv, but we're opening it in append mode.
So we've used the "a" character.
Just make sure that you use an "a" instead of a "w" for the mode.
And to check your understanding, why is it important to open a file in the correct mode? Pause the video whilst you have a think.
It's important to open a file in the correct mode because opening the file in write mode will delete any existing file and replace it with a blank one, which means you could lose data that you don't want.
If you want to check the original contents of the CSV file and add to the file, then depending on the original content, you can do this in two stages.
One, read the contents of the CSV file.
Two, append to the contents of the CSV file.
This programme opens a list that contains the colours in the rainbow.
It reads the colours in the file.
It then asks the user to enter a colour.
If the colour is on the list in the file, the text "Correct" is appended to the list rainbow.
It then appends the data held in rainbow to the file.
Let's have a look at a sample output from the programme.
So we have the programme running.
It asks the user to enter a colour, red.
It then asks the user to enter another colour, orange, another colour, yellow, another colour, green, another colour, blue, another colour, violet, and then final colour, and they've entered indigo.
We then have a look at the CSV file.
We have the original colours in the rainbow on the first line and then on the second line have the list which either inputs "Correct" or "Incorrect" if the user has entered the right order in the rainbow.
So you can see that the user has mixed up indigo and violet.
So the last two values in the list are written as incorrect.
And to check your understanding, true or false? The "r" mode allows you to read and write to a file.
Pause the video whilst you have a think.
Did you select false? Well done.
The "r" mode only allows you to read the content, file.
The "w" or "a" mode allows you to write or append to a file.
Okay, we're moving on to our final activity of today's lesson.
Open the starter programme at oak.
link/spellings.
A teacher has a list of eight spellings that they would like to use as a test for learners.
They will shout out the spelling in the classroom and the learner must type their spelling of the word into the programme.
The programme should, a, read the data from the spellings.
csv file, b, append the data to a list, c, allow the learner to enter each spelling one at a time, d, check if the entry matches the spelling in the list, e, record whether the learner was correct or incorrect, and f, append the results data to the spellings.
csv file.
This activity is really similar to the rainbow activity you've seen previously in this lesson.
So if you want to go back and have a look at that code to help you, remember to do that.
Pause the video whilst you complete the activity.
How did you get on? Did you manage to code the spelling solution? Let's have a look at some sample code together.
If you want to open the full solution, you can go to oak.
link/spellings-solution.
On line 2, we're opening the file spellings.
csv in read mode and we're reading the contents of the file into a variable called words on line 3, and we close the file once we've done that.
On line 7, we're assigning words to a list.
So words is equal to words.
split, and we're splitting it at the comma character.
We're then initiating an empty list called results.
We then use a for loop to check the spelling of each word and store the results.
So for item in words, word is equal to input("Enter word: "), asking the word.
If word is equal to item, then we're going to append "Correct" to the results list, else we're going to append "Incorrect." On line 19, we're joining the results into a single string, data is equal, new line + ",".
join(results).
And then lastly, on line 22 to 24, we're appending the results to the file.
Remember, really important, the append mode here to keep the existing contents of the file.
Okay, we've come to the end of today's lesson, "Careers in programming," and you've done a fantastic job, so well done.
Let's summarise what we've learned today.
There are good habits that a programmer can adopt.
These include adding comments to code, using meaningful identifiers, and following naming conventions.
There is normally one approach to solve a programming problem, and it's good to experiment with different approaches.
You can append to a CSV file in the same way as you can a text file.
I hope you've enjoyed today's lesson, and I hope you'll join me again soon.
Bye.