Loading...
Hello.
My name's Mrs. Jones.
and I'm really pleased you decided to join this lesson today.
In this lesson, we'll look at improving your programme code to meet more of the functional requirements as well as looking at functions to refine your code further.
So let's get started.
Welcome to today's lesson.
Today's lesson is called Refining Code for a Programming Project from the unit Python Programming Project.
And by the end of this lesson, you'll be able to identify and make improvements to refine code.
There is one key word to today's lesson, modularity.
Modularity is breaking software into independent self-contained units or modules.
There are two sections to today's lesson.
The first is identify and make improvements and the second improve modularity and code structure.
So let's start with identify and make improvements.
Sofia says, "The functional requirements state that multiple games should be added to the review, but my code only accepts one game review." And you can see on the inputs here we had 1.
2, user inputs the name of each game to be reviewed and user inputs the review duration for each game.
Sofia also says that, "The functional requirements state that the programme should randomly select a time to display a discount code.
My code doesn't do this yet either." You can see on the processes randomly select a time from total stream time to display a discount code giveaway.
Sofia also says, "The functional requirements state that the programme should output the name and duration of each game.
My code doesn't do this yet either." You can see in the outputs, display the name and duration of each reviewed game.
"So perhaps I should review my code to identify improvements," which is a really good comment from Sofia.
The link there will take you to this code.
So this is the code we had and we need to start looking at this to identify improvements.
And there's the continued element right up to the end of what we worked up to.
How could Sofia make improvements to her code to meet all of the functional requirements? Well, this code could be improved by allowing the user to add more than one game review, displaying a stream schedule for the user to see the outline of the stream, ensuring that the programme randomly selects a time to display the discount code giveaway, and displaying the name and duration of each reviewed game.
Sofia says, "How can I change my code to allow a user to add more than one review easily? I don't want to change everything." And Sam says, "You could use a while loop, Sofia." In order to add multiple games, you need to use iteration to repeatedly ask the user to enter the game review.
The loop needs to repeat while the total_time is less than stream_seconds.
Let's have a quick check.
Which of the following conditions would check that the total_time is less than the stream_seconds? Is it A, B, or C? Pause the video to look at the syntax on each of those A, B, and C options and decide which one is the correct one, and then we'll check it.
And let's check your answer.
The answer was A, while total_time < than stream_seconds:.
Well done if you got that correct.
In order to randomly select a time to display the discount code giveaway, the programme will need to select a random time between 1 and total_time.
In Python, you can import a module called random, which provides us with pre-made functions to randomise data.
This code imports the random module.
You can see that on line one, import random.
Any imports must be done at the start of a programme, which is why it's at the top.
The function on line three lets you generate a random whole number.
It creates a random number between 1 and 5, including both 1 and 5, and stores it in the variable called number And on line four, you can see this prints the random number to the screen, 'cause it's printing what is stored inside that variable number.
Let's have a quick check.
What does the following code return? Is it A, a random integer between 5 and 9 inclusive, but never 10, B, a random integer between 5 and 10, inclusive, so would output either 6, 7, 8, or 9, or C, a random integer between 5 and 10 inclusive, would output either 5, 6, 7, 8, 9, or 10? Pause the video to consider your answer and then we'll check it.
Let's check your answer.
The answer was C.
A random integer between 5 and 10, inclusive.
So it could output any from those, 5, 6, 7, 8, 9 or 10.
Well done if you got that correct.
In order to display the stream schedule for the user to see the outline of the stream, you will need to use a series of print statements.
Let's remind ourselves of the outputs required.
We need to display the total number of games reviewed.
We need to indicate the time that the discount code will be given away during the stream, and we need to display the total plan stream duration in minutes and seconds.
The programme should produce an output similar to this example.
You can see there that the stream summary, and then underneath, the discount code will be announced at 10 minutes and 13 seconds into the stream.
Total stream duration, 20 minutes and 10 seconds.
Total games reviewed, two.
So this last bit here is 3.
1, displays the total number of games reviewed.
The first one there was indicate the time that the discount code will be given away during the stream.
And in the middle, display the total planned stream duration in minutes and seconds.
In order to display each game, review in the list, game_names and game_durations, a for loop can be used to iterate through the list.
For loops are count controlled, meaning that you specify the number of times that you wish the instructions to be repeated.
In Python, the range function can be used to specify the number of times that a for loop should iterate.
The for loop here will repeat for every game in the games_names list.
For i in range, length is len, L-E-N, of game.
You can see they're indented.
We would have name = game_names[i] so that i would pull the iteration number from zero and would output the items in the list from zero.
And the same with the game_durations, and then it will output them both.
Inside the for loop, we get the game_names and game_durations, and the current position i and store them in new variables called name and duration.
A print statement is then used to display each game and duration to the user.
Let's do the activity.
Number one, adapt your programmes so that multiple games can be added to the stream.
Two, it would also be helpful to the user to know when they have reached the total stream type.
Add a print statement to notify them.
Three, adapt your programmes so that stream schedule is displayed to the user.
Four, adapt your programmes so that it randomly selects a time and displays the discount code giveaway.
There's a hint.
The output should be in minutes and seconds.
And to do this, you will need to make use of the arithmetic operators.
Five, adapt your programme so that the name and duration of each review game is displayed.
Pause the video, go back through the slides, use your worksheet, and adapt your programme code, and then we'll go through a solution.
Let's have a at a solution.
So the first part, adapt your programme so that multiple games can be added to the stream.
We've got that while loop.
While total_time < than stream_seconds, and then all of the code underneath is indented inside that.
Two, it would also be helpful to the user to know when they have reached the total stream time and add a print statement to notify them.
And then we've got print enough games entered through the stream.
Three, adapt your programme so that stream schedule is displayed to the user.
Now we've got at the top, print ("Stream summary:").
Now we've got the stream duration in minutes, in seconds.
With those calculations, we also have the output in the format of the total stream duration and total games reviewed.
Part four was adapt your programme so that it randomly selects a time and displays the discount code giveaway.
we've got giveaway_time =, random integer at the top, random.
randint (1, stream_seconds) one comma stream_seconds.
And then we've also got the giveaway_minutes, giveaway_seconds, and then we've got the output in the format as well.
In part five, adopt your programmes so that the name and duration of each reviewed games is displayed, and that's using that for loop.
for i in range (len(game_names)): name = game_names[i] Same for duration, with game_durations, and then outputting in that format of the name and the seconds.
Well done if you've got those the same or similar, and pause the video and go back through those slides to look at the game code, to check yours and to review what you've done.
Let's move to the second part of today's lesson.
Improve modularity and code structure.
In structured programming, a programme can be divided into a set of smaller sections called subprograms. Each subprogram is designed to perform one specific task clearly and effectively.
This is sometimes referred to as modular programming.
And by splitting a programme, you can improve modularity.
There are lots of benefits of improving modularity in your programmes.
These include code readability, easier testing, easier maintenance, faster development time, reduced repetition.
Code readability.
It is much easier to read small chunks of code than one very large programme.
By breaking a large programme into smaller sections, it becomes easier to read and check through the code.
Easier testing.
It is much easier to test a sub-program that carries out single task.
Having less code makes it easier to find and fix mistakes.
Easier to maintain.
If a change needs to be made, it can be made and tested in one place, and the rest of the programme will not be affected.
This means you can update a programme without disrupting its main functions.
If the new changes don't work, the entire programme won't be affected.
Faster development time.
A large programme can be worked on by several programmers.
This means that sub-programs can be developed concurrently at the same time, speeding up the development process.
Lots of programmers work in teams, each taking a small part of a larger programme.
Reduced repetition.
Subprograms help avoid repeating the same code.
Instead of writing the same code over and over, it can be written once in a subprogram and then called whenever it is needed.
Let's have a quick check.
Which of the following is a key benefit of using subprograms in programming? Is it A, they make code harder to maintain, B, they require more lines of code, or C, they reduce code repetition? Pause the video to consider your answer, and then we'll check it.
Let's check your answer.
The answer was C.
They reduce code repetition.
Well done if you got that correct.
The stream scheduler programme has four distinct actions which are illustrated in the structure diagram.
Collect stream duration, add game reviews, choose a random prize giveaway, and display the stream details.
Each of these distinct actions could be handled by a subprogram.
This would improve modularity for your programme and split the main programme into smaller, more manageable sections.
A subprogram can be classified as a function or a procedure.
A function is different from a procedure because it returns a value or values to the main programme.
Python does not distinguish between procedures and functions.
The syntax for creating a function and a procedure is the same.
Def is used to define the function.
Def, D-E-F, is the first three letters of define.
You define the function.
Calculate is the identifier that is used to give the function a meaningful name.
You give the meaningful name and you choose it.
In this case, it's calculate.
{a, b} are the parameters In this function.
They are inside the brackets and separated by a comma.
Parameters define the data values that must be passed to a function when it is called.
The parameters are treated as local variables inside the function.
A function can have one more or no parameters.
In order to use a function in the main programme, it needs to be called.
To call a function, you write the name of the function followed by any arguments that need to be passed to it.
You can see here on line seven, total = calculate [num1, num2] These are the arguments that are passed into the function.
The function call contains arguments contained in the brackets, which are passed into the function via the parameters.
Here are the parameters.
Here are the arguments.
Note, you do not need to pass any arguments to a function if it is not expecting any parameters.
If the function is going to return value to the main programme, a return statement is needed.
You can see on line four the return statement.
Let's have a quick check.
True or false.
In python, different syntax statements are used to define functions and procedures.
Pause the video to consider if that is true or false, and then we'll go through the answer.
Let's check your answer.
The answer was false.
Python does not distinguish between functions and procedures.
Both are defined using the, D-E-F, def keyword.
Well done if you got that correct.
Good programming practise involves taking steps to make your code easier to read and understand for anyone who may need to improve, use or maintain it in the future.
You may not necessarily be working on a project with someone during development, but someone else may need to adapt to maintain your programme in the future.
Good programming practises include comments, programme layout, identifiers, and naming conventions.
Let's do the activity.
One, create a function to collect stream duration and replace the existing code with a function call instead.
Two, review the structure of your code to ensure you followed good programming practises.
If needed, add comments to explain code functionality, and ensure variables use meaningful identifiers.
If you have additional time, you could create extra subprograms and add these to your programme, display_stream_schedule, which show this game stream summary, add_game_reviews, adding the individual game reviews, and choose_giveaway, pick a random time for the giveaway.
Pause the video, go back through the slides, use your worksheet, and add the function to your code, and then we'll go through the solution.
Let's have a look at the solution here.
Here is the function to collect stream duration and replace the existing code with that function call instead.
You can see on line one, we now have define, def collect_stream_duration.
Well done if you've got those correct.
Pause the video to have a look at the code in more detail and review yours, but well done for completing that.
In summary, reviewing code means checking how well it works, how readable it is, and whether it meets the functional requirements.
Improving code may involve simplifying logic, breaking large sections of code into subprograms or correcting any errors.
Code should be readable and maintainable so that others can understand and work with it easily.
Well done for completing this lesson, Refining Code in a Programming Project.