Lesson video

In progress...

Loading...

Hello, my name is Ms. 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 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.

Sophia 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.

Sophia 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.

Sophia 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 Sophia.

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.

There's the continued element right up to the end of what we worked up to.

How could Sophia 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 personally and duration of each reviewed game.

Sophia 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 Sophia.

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.

Let's check your answer.

The answer was A.

While total time is less 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 one 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 one and five including both one and five and stores it in the variable called number.

And on line four you can see this prints the random number to the screen as 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 five and nine inclusive but never 10? B, a random integer between five and 10 inclusive so you would output either six, seven, eight or nine? Or C, a random integer between five and 10 inclusive.

We would output either five, six, seven, eight, nine 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 five and 10 inclusive so it could output any from those five, six, seven, eight, nine 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 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 planned stream duration in minutes and seconds.

The programme should produce an output similar to this example.

You can see there that the stream 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 the one 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 game's names list.

For I in range length is len, l-e-n of game names.

You can see they're indented we would have name equals 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 programme 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 time.

Add a print statement to notify them.

Three, adapt your programme so that stream schedule is displayed to the user.

Four, adapt your programme 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 look 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 is less 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 for the stream.

Three, adapt your programme so that stream schedule is displayed to the user, and then we've got at the top print stream summary and then 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 equals random integer at the top random dot randint 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, adapt your programme so that the name and duration of each reviewed game is displayed, and that's using that for loop for i in range and len game names name equals game names with i inside the square brackets.

Same for duration with game durations, and then outputting in that format of the name and the seconds.

Well done if you 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 subroutines.

Each subroutine 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 and these include code readability, easier testing, easier maintenance, faster development time and 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 subroutine than carries out a 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 subroutines 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, subroutines help avoid repeating the same code.

Instead of writing the same code over and over it can be written once in a subroutine, and then called whenever it is needed.

Let's have a quick check.

Which of the following is a key benefit of using subroutines in programming? Is it a.

they make code harder to maintain, b.

they require more lines of code, 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 this structure diagram.

We collect stream duration, we add game reviews, choose a random prize giveaway time and display the stream details.

Each of these distinct actions could be handled by a subroutine and this would improve modularity of your programme and split the main programme into smaller more manageable sections.

A subroutine 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 at the beginning is used to define the function and def as you can see is the first three letters of define.

We're defining the function.

Calculate here is the identifier that is used to give the function a meaningful name.

You choose the name that is given there.

A and B here are the parameters in this function and they're inside the brackets 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 there on line seven.

Total equals calculate.

You can see that is the name of the function, and inside the brackets we're using num1 and num2 which have been defined on line five and six which would be the arguments that pass to it.

The function call contains arguments contained in 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 a value to the main programme a return statement is needed.

You can see that on line four there.

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 check your answer.

Let's check your answer.

The answer was false.

Python does not distinguish between functions and procedures.

Both are defined using the def keyword d-e-f.

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 and maintain your programme in the future.

Good programming practises include comments, programme layout, identifiers and naming conventions.

Let's do the activity.

1.

Create a function to collect stream duration and replace the existing code with a function call instead.

2.

Review the structure of your code to ensure you have 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 subroutines and add these to your programme.

Display stream schedule showing the game stream summary, add game reviews adding the individual game reviews, then choose giveaway pick a random time for the giveaway.

Pause the video, go back through the slides, use your worksheet and have a go at creating these functions and structures to your code and then we'll go through a solution.

Let's have a look at a solution.

So here we've got create a function to collect stream duration and replace the existing code with a function call instead.

You can see there on line 1 we're now def, so defining collect stream duration is the identifier, and then inside the function we've got the loop, and the extra details they're added on line seven which is the comment, send the valid number back to the main programme.

Well done if you did that correct.

In summary, reviewing code means checking how well it works and how readable it is and whether it meets the functional requirements.

Improving code may involve simplifying logic, breaking large sections of code into subroutines 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 for a programming project.