Loading...
Hello, my name is Mrs. Holborow, and welcome to Computing.
I'm so pleased you've decided to join me for the lesson today.
In today's lesson, we are going to be looking at how we can take input into a programme and how we can display output from a programme.
Welcome to today's lesson from the unit Programming Sequence.
This lesson is called Input and Output.
And by the end of today's lesson, you'll be able to explain how input and output devices allow users to interact with a computer system.
Shall we make a start? We will be exploring these keywords in today's lesson.
Input.
Input, data that is sent to a programme to be processed.
Output.
Output, information produced by a computer system as a result of processing data.
Look out for these keywords throughout today's lesson.
Today's lesson is divided into two sections.
We'll start by describing how to use input in a programme.
We'll then move on to explain different outputs from a programme.
Let's make a start by describing how to use input in a programme.
Computers interact with users through input and output.
A keyboard is a common input device to write programmes.
Programmes written in a programming language must follow the syntax of that language so that it can be understood.
Python provides functions to handle user input and display output.
Learning to use input and output is essential for interactive programmes.
Sequencing is one of the fundamental concepts in programming.
In Python, code runs line by line from top to bottom unless something changes the order, like repeating steps or making decisions.
So here, I've got five lines of code each with a print statement, and they're telling the story of "Goldilocks and the Three Bears." The sequence of this programme means that line 1 will run first.
So the first thing the programme will do is print, "Once upon a time, three bears lived in a little house." The code will then follow on from each other line after line.
Time to check your understanding.
All programming languages have rules for, A, spelling, B, translation or C, syntax.
Pause the video whilst you have a think.
That's right, all programming languages have rules for syntax.
Well done.
The following Python programme demonstrates sequencing by printing a short story in order.
The output appears in the same order as the instructions in the programme.
You have seen how to output information from a programme using the print function.
So we have the word print in all lowercase, we open our brackets or parenthesis, and then in speech marks, if we are printing text, we have the text that we want to print.
So in this case, we're printing out, "Hello world." You will now see how a user can input data into a programme using the input function.
This programme uses the input function to take the name the user enters and then display it back on the screen as part of a sentence using the print function.
Let's pause here and watch the video to see the code being run in the code editor.
<v Narrator>Here are the three lines of code in the editor.
</v> Let's see what happens when we run the code.
So it's run line 1, so it's asking what is your name? So I'll put my name in.
So this will become the value that is stored in the user variable, so Matt.
Press Enter.
And then it runs line 3, so it outputs hello and the value that's held in the variable user, which in this case is Matt.
<v ->If the user enters the name Aisha when prompted,</v> this is what would be displayed on the screen when the code is run.
So you can see here the first line, what is your name is printed.
The user has responded with Aisha in the blue text, and then the programme prints out a message which says, "Hello Aisha," so it's printing the word hello followed by what is held in the variable user.
If the user enters a different name, for example, Lucas, the output will change.
Time to check your understanding.
What is the name of the function that can take in information a user enters into a Python programme? Is it, A, output, B, print, or C, input? Pause the video whilst you have a think.
Did you get that right? I'm sure you did.
C, input is the correct answer.
Okay, we are coming to our first task of today's lesson, and you're doing a great job so far, so well done.
I'd like you to start by describing how a keyboard is used for both writing Python code and providing input to a running Python programme and explain how that user input is shown back to the user on the screen.
Pause the video here whilst you complete the activity.
Okay, let's have a look at a sample answer together.
You were asked to describe how a keyboard is used for both writing Python code and providing input to a running Python programme, and explain how that user input is then shown back to the user on the screen.
The keyboard is used to write Python code and to provide input when a programme runs.
The input function allows a user to type information and the print function then displays that information back to the user on the screen.
Remember, if you need to pause your video now and add any detail to your answer, you can do that now before moving on to the next step.
Okay, we are now moving on to the second part of today's lesson, where we are going to explain different outputs from a programme.
In programming, output is how a computer communicates information to the user.
One of the most common ways to display output is by using the print function in Python.
So here we have an example.
We have the word print in lowercase, we open our parentheses, and then we have the question, "What is your name?" which is in speech marks because it's text.
A string is a piece of text inside the quotation or speech marks.
Strings can contain words, numbers, and symbols, and they're used to output messages to the user.
Let's see if we can read through and explain this code.
So line 1 displays a message on the screen asking for the user's name.
Line 2 uses the input function to allow the user to type in their name.
Line 3 outputs a greeting that includes the name entered by the user.
The f-string, or formatted string, inserts text directly into another string to format the output using the curly brackets.
So you can see there we have print and then we have f, which indicates the f-string, and then we have the text hello, and then in curly brackets we have the variable which we want to print out, which is user.
So this will join whatever the user enters next to the word hello.
So for example, hello Aisha.
Why do we use f-strings in outputs? Well, they're easier to read.
Instead of writing print("Hello" + user), f-strings let us include text inside the string itself.
They're more efficient, they're faster and more straightforward than older methods like using the + operator.
They're also really useful for dynamic output.
They allow us to format and include multiple pieces of text easily.
Time to check your understanding.
What is the advantage of using f-strings in Python? Is it A, they are slower than older string formatting methods, B, they are faster and more straightforward than older string formatting methods, or C, they make the code more complex? Pause the video whilst you have a think.
Did you select B? Great work.
Using f-strings are faster and more straightforward than older string formatting methods.
If you haven't used an f-string yet in your code, maybe have a go.
When you run a Python programme, you can get different types of output.
So you can get the expected output, the result you intended, such as printed messages.
You may get error messages.
If something is wrong in your code, Python will display an error message to help you fix it.
Errors can happen for many reasons, but one of the most common is syntax errors.
In Python, it's easy to make syntax errors, and all programmers make syntax errors.
Syntax errors can be frustrating when you start learning a text-based programming language like Python.
So here, you can see I have a print message for what is your name, but the programme hasn't run correctly and the text output is showing that I have a syntax error.
Can you identify the syntax error in this code? Is it A, a bracket is missing? Is it B, print should have the capital P, or C, a speech mark is missing? Pause the video whilst you have a think.
Did you spot it? That's right, the closed bracket is missing.
So I've opened the bracket after the print statement, but I've forgotten to close it at the end.
Which of the following pieces of code would output hello followed by the name the user enters? Is it A, B or C? Pause the video here whilst you look carefully at the code.
Did you select A? Well done.
A is the correct piece of code B is not correct because you'll see under user we're actually typing the name Aisha, so that piece of code will always print hello Aisha rather than the name the user enters.
The programme for C will also do the same because although we are taking in the user's input on line 3, in the print statement on line 3, we're printing out hello Aisha.
So again, this programme will always print hello Aisha no matter what the user enters.
The Raspberry Pi Code Editor lets you write Python code directly into the browser.
Let's watch the video clip to have a tour of the editor and see how it works.
You can access the Raspberry Pi Foundation Code Editor at editor.
raspberrypi.
org.
If you want to be able to save and download your files, you need a Raspberry Pi Foundation account and you can sign up and log in here.
Click Start Coding Python to begin.
The Raspberry Pi Foundation Code Editor will automatically load with a blank project.
The code editor has three main panels: the sidebar panel on the left, the code panel in the middle, and a preview panel on the right.
You can resize these panels using the handles.
The layout will adapt if you resize your browser window or if you're working on a device with a smaller screen.
The code editor offers accessibility features, which can be accessed in the settings menu.
You can switch between light and dark mode and then you can increase the text size.
The project bar is at the top and it shows the name of your project with an edit button, so you can change the project name, a button to download the project files, and a link to login if you haven't already done so.
Each project will automatically have a file called main.
py.
This is the code that will run for your project.
Let's have a look at some of the features of the code editor.
When you add new lines of code, line numbers will automatically be added for you.
Syntax highlighting improves the readability of your code by applying colours to different elements.
The code editor will automatically format your code with auto indentation.
Let's click Run and see the code in the preview panel.
Okay, we are coming onto our last set of tasks for today's lesson, and you're doing a fantastic job so far, so well done.
I'd like you to identify the syntax errors for the lines of Python code by completing the table.
So the first one has been done for you.
So the first one says the error is a missing speech mark after the f for the f-string.
For part two, I'd like you to write the correct syntax for the code.
Pause the video whilst you have a go at the activities.
For part three, I'd like you to write your first programme.
Write a programme that implements this algorithm.
Ask the user to input their favourite colour, ask the user to input their favourite animal, create a message using an f-string that combines their favourite colour and animal in a sentence.
So here's some sample output.
The text on the screen says, "What is your favourite colour?" The user has inputted, "Red." We've then output the message or question, "What is your favourite animal?" The user has entered, "Cat," and then we've printed the message, "I love red cats!" Pause the video whilst you have a go at the activity.
Finally for part four, I'd like you to explain two different types of output a Python programme can produce and describe how a student might use these different outputs to improve their code when working with f-strings.
Pause the video whilst you answer the question.
Okay, let's go through these answers together.
So for part one, you were asked to identify the syntax errors.
The first one had been done for you.
So the second example, the error is that there's a capital P on the word Print.
Print must be lowercase in Python.
The third line of code was missing the curly brackets around the variable user.
You were then asked to write the correct syntax for the code.
So the correct syntax should be print all in lowercase, open bracket f, open speech marks the word hello, open curly bracket, the variable name user, close curly bracket, close speech marks, close bracket.
You were then asked to write a programme that implemented this algorithm: ask the user to input their favourite colour, ask the user to input their favourite animal, and then create a message using an f-string that combines their favourite colour and animal in a sentence.
So here's some sample code.
We have a print statement which says, "What is your favourite animal?" On line 2, we have a variable initialised called animal, and we are assigning that as user input.
On line 3, we have another print statement which says, "What is your favourite colour?" On line 4, we're storing the user's input in the variable colour.
And then on line 5, we have a print statement with an f-string which says, "I love," and then we're returning those values held in the variable colour and animal.
If your code looks slightly different or works slightly differently, then that's absolutely fine.
For the final part, you were asked to explain two different types of output a Python programme can produce and describe how a student might improve their code when using f-strings.
Here's a sample answer.
Python programmes produce expected outputs like formatted messages from f-strings and error messages, such as syntax errors.
Students use expected outputs to check their f-strings, display variables correctly, and error messages to identify and fix syntax errors within their f-string code.
Remember, if you need to pause your video here and add any extra detail to your answer, you can do that now.
Okay, we've come to the end of today's lesson, and you've done a fantastic job, so well done.
Let's summarise what we've learned in this lesson.
Users can provide input into a computer by using the keyboard.
This allows them to enter text, numbers, and functions in code.
Computers communicate information back to the user through various methods of output.
This output can take forms such as displaying text or error messages.
I hope you've enjoyed today's lesson, and I hope you'll join me again soon.
Bye.