Loading...
Hello, my name's Mrs. Jones and I'm really pleased you decided to join this lesson today.
In this lesson, we will look at functional requirements and look at developing code to meet these requirements like using inputs, validation, and lists.
So let's get started.
Welcome to today's lesson.
Today's lesson is called Developing code for a Programming Project from the unit Python Programming Project.
And by the end of this lesson, you'll be able to develop and write code that meets functional requirements.
There are three key words to today's lesson, functional requirements.
Functional requirements define the specific actions or behaviours that a software system or application must perform.
Validation.
Validation is checking that data is acceptable by comparing it against a set of criteria.
List.
List is a dynamic data structure that can contain items of different data types.
There are two sections to today's lesson.
The first is develop code to collect stream details, and the second is develop code to add a single game review.
So let's start with develop code to collect stream details.
Over the course of this project, you'll be developing a computer programme for a project called StreamScheduler, which will help a content creator plan their game review stream.
A content creator is preparing a live stream where they review a series of upcoming games to people online.
They want a computer programme that will help them plan the duration of their stream.
The programme will last for the total duration of the stream and how long they will spend reviewing each game.
Once the total review time reaches the planned stream duration, the schedule is complete.
Sofia says, "Okay, so I have completed my analysis and my designs, and I think I'm ready to code, but where should I start?" The first step is to collect stream duration.
This step involves gathering information from the user and preparing it for use in the rest of the programme.
Consider the functional requirements from the analysis, one was inputs, so 1.
1 was user inputs the total duration of the stream in minutes, and two, the processes, we had 2.
1, validate that the total duration is between 20 and 40 minutes, 2.
2, store the total duration of the stream, and 2.
3 convert the total duration to seconds.
Well, let's focus on the input section first.
So the user needs to input the total duration of the stream in minutes.
This programme uses the input function to take the name the user enters and then displays it back on the screen as part of the sentence using the print function.
And we can see there on line one print, what is your name? Line two, user equals input.
And line three we have print and we've got the format of hello and user.
The print and the input functions can be combined into one line and you can see that here on line one, we have user equals input and inside the brackets, what is your name? So the difference between the first option and the second is we're not using the print function, we're putting the question that is displayed to the user inside the brackets of the input.
Let's have a quick check.
Which of the following is the correct syntax for an input statement? Is it A, user equals input inside the brackets, what is your name? B, user equals inside the brackets, what is your name? C, user equals input, what is your name? Pause the video to consider your answer and then we'll check it.
Let's check your answer.
The answer was A.
Well done if you got that correct.
Let's now focus on the processes section.
So we need to validate that the total duration is between 20 and 40 minutes, store the total duration of the stream, and convert the total duration to seconds.
Date validation is the process of checking whether input data is acceptable before the programme uses it.
For example, if a user is asked to enter their age, the programme should check whether the input is a number and within a realistic range.
A crash happens when a programme suddenly stops running because of an error it cannot process or recover from.
If a programme prompts the user for a number and the user types dog, the programme doesn't know what to do so it crashes.
Using validation ensures data is reasonable and helps prevent crashes in the programme, helps maintain data accuracy and reliability, improves user experience by catching mistakes early.
Without validation, someone might enter dog as their age and the programme would crash.
Validation is usually used inside a loop that keeps asking for input until it is valid.
Often validation uses if statements to test the input.
You can see in this example here, if age.
isdigit checks whether the user entered only digits.
If they typed something like 12, that would be true.
If they typed 12 as in the written 12 or left it blank, it would be false.
This programme will keep asking the user to enter an age until a numerical value between 18 and 16 is entered.
You can see the while loop and you can see the if statements within there, the validation on line five, and then the check between it's between 18 and 60 on seven.
Validation checks often use relational operators to check whether the values input by a user meet conditions.
These are common relational operators.
You have the double equals, which means equal to, exclamation mark equals not equal to.
You have less than, which is the arrow pointing to the left.
And then you have the same with the equals, which is less than or equal to.
You have the arrow pointing the other way, greater than, and with the equals, greater than or equal to.
Logical operators are used when you need to compare multiple values with relational operators to return a single value of true or false.
And it's true when both relational operators are true or true when either or both relational operators are true, and not true when the relational operator is false, not true.
Let's have a quick check.
Which condition would you use to check whether the entered score is between zero and 100? Is it A, if score is less than zero or score is greater than 101? B, if score is greater than zero and score is less than 101? Or C, if score is greater than zero or score less than 101? Pause the video to consider your answer and then we'll check it.
Let's check your answer.
The answer was B, if score is greater than zero and score is less than 101.
Well done if you've got that correct.
Let's do the activity.
Develop code to perform the following tasks, one, output a welcome message, two, collect the stream duration from the user and there's a hint, the stream duration will need to be stored as an integer value for processing later on in the programme.
Three, validate the stream duration entered by the user to check whether the length of the stream is at least 20 minutes and no more than 40 minutes.
Hint, iteration could be used to only accept a valid duration.
And the fourth task on this is currently the programme stores the stream duration in minutes, the user will enter each game review in seconds and the programme will need to ensure it does not exceed the total stream duration time, therefore, the stream duration value needs to be converted to seconds using a calculation.
Pause the video, go back through the slides, use your worksheet, and then we'll go through a solution.
Let's have a look at a solution.
So the first part was output a welcome message, and this can be a print statement using saying, Welcome to StreamScheduler.
The second part was collect the stream duration from the user.
So now we've got line two and three added, stream input equals input, enter the stream duration 20 to 40 minutes, and then on line three, stream minutes equals integer and we have stream input.
And here's the third part.
So looking here, we've now got more added in.
We've got the while loop, we have the validation, checking that it is a digit, that it is an integer being used, and we've also, as you can see on line eight, we've got that check to make sure that it is between 20 and 40 minutes.
And then for four, we've got that added on at the end, stream seconds equals stream minutes multiplied by 60.
Well done if you've got that correct.
Pause the video to have a look at the code in more detail, and then we'll carry on.
Let's move to the second part of today's lesson.
Develop code to add a single game review.
The next part of the programme focuses on collecting game reviews and meets the following functional requirements from the analysis.
We have the inputs, user inputs the name of each game to be reviewed and user inputs the review duration for each game in seconds.
Note, at this stage of the code development, you are going to focus on only adding one game review.
Later on, you'll develop the code to allow multiple games to be reviewed.
The processes, well, we need to store the name of the game being reviewed and the duration, we need to store the running total of games reviewed, we need to validate that running total is between 20 and 40 minutes, and we need to stop asking for more games once total review time is more than or equal to stream duration.
In the StreamScheduler programme, we do not know how many games will be added to a schedule.
What we do know is that we need to store the information about multiple games, which includes the game name and the number of seconds to review the game.
In Python, one type of data structure that allows you to store more than one piece of information is a list.
A list is a data structure.
Data structures are organised collections of data.
This means it holds a collection of data items or elements in a sequence one after another.
A list is a dynamic data structure that can grow or shrink as you add or remove items. Each item in the list has a position called an index, starting at zero.
You can see the example here, the index zero, one, two, and the value, Monday, Tuesday, Wednesday.
In Python, the syntax for a list is a comma separated list of values, items in square brackets.
You can see here we have the identifier, which is the name given to the list and the list items inside those square brackets and separated by a comma.
In this example, the list items are string literals, pieces of text, so they need to be in quotation marks.
Note, the list is going over more than one line due to the length of the items. When the programme is run, this is what the list will look like when stored in memory.
"The first item in the list is at position zero, not one," Sam says, which is correct.
You can see on the right here, you've got zero through to six and what is stored in each of those positions.
To access an item in a list, you need to specify the index, the position that holds the item.
Lucas says, "This code here will print Monday." And Sam corrects him and says, "No.
Remember, lists use zero-based indexing, so this will print Tuesday." And the syntax on line five, you can see there, print and inside the brackets you've got the name of the list and the inside the square brackets the position value.
Let's have a quick check.
In the programme here, what day is held in index position two? Is it A, Monday, B, Tuesday, or C, Wednesday? Pause the video to consider your answer and then we'll check it.
Let's check your answer.
The answer was C, Wednesday.
Well done if you've got that correct.
As lists are dynamic, you can add new items to them during the execution of your programmes.
Adding a new item to the end of a list is known as appending the item to the list.
You can see here item zero, one, two, three, and then adding item four, append it to the end.
Here is an empty shopping list.
You can see there, shopping equals nothing inside those square brackets.
You can add new items to the end of the shopping list by using the append method.
On line two, you can see shopping.
append and inside the brackets what we want added to the list, and in this case bread, and because it is a string, inside the quotation marks.
When you print this list, you can see that the first it is empty, and then it holds the item bread after the append method has been called.
So you can see that code on the left there, but then on the right you can see the output is empty square brackets and then the brackets with bread inside.
Let's have a quick check.
True or false? The append method will overwrite the contents of an existing list.
Pause the video to consider if that is true or false.
Let's check your answer.
The answer was false, and that's because the append method will add an item to the end of an existing list.
It will not replace or overwrite any existing items in a list.
Well done if you got that correct.
Let's do the activity.
We are gonna develop code to perform the following tasks, one, create a new empty list called game names, two, in order to store the game durations of each game review, these also need to be stored in a list.
Create a second empty list called game duration.
Three, once the user has entered the game name and duration in seconds, the values need to be added to each list.
Use the append method to add items to each list.
In this case, add the values stored in the variables game name and review seconds.
Four, the programme needs to track the time entered for each game review to ensure it doesn't exceed stream seconds.
To do this, create a variable called total time and set its initial value to zero.
Five, check that the lists work by using a print statement to output the contents of each list.
The output should look similar to the output in figure one.
Finally, we need to store the running total of games reviewed.
To do this, the value stored in review seconds needs to be added to total time to keep a running total of games reviewed.
And here is figure one, it's a sample output of what the user should see.
Welcome to StreamScheduler, enter the stream duration, enter the game name, enter review duration in seconds, and then what has been stored inside those lists.
The items in blue is what the user has added.
Pause the video, go back through the slides, use your worksheet, and work through those activity tasks and then we'll look at a possible solution.
Let's have a look at a solution.
Pause the video to look at the code in more detail as we go through.
You can see here the welcome statement, we've got the validation, we've got the entering the stream minutes, but then after all of that that we created in task A, we've got from line 17, we've got the setup of the two lists and the variable total time.
Let's move to the next slide to look at the rest of the game code.
We've got the game name equals input.
game_names.
append that game name, review duration equals input, review seconds equals interger review duration, game_durations.
append review seconds, and then we are outputting what's stored in those two game names and game durations.
And then on line 28, we've got the total time equals the total time, plus the review time seconds.
Well done if you've got those correct.
In summary, functional requirements define the specific actions or behaviours that a software system or application must perform.
Functional requirements can be split into the inputs, processes, and outputs of a programme.
Validation can be used to check that the data is acceptable by comparing it against a set of criteria.
A list is a dynamic data structure that can contain items of different data types.
Well done for completing this lesson, Developing Code for a Programming Project.