Learn | Python | After Scratch | Exercise | Design


Let’s use Scratch to build a Python
program with a practice exercise. Now we’re going to use scratch for the
design and then challenge you to write the Python code. So you’ve seen already
the programming concepts that you already know from scratch and we shown
you the Python code of those concepts. What is your challenge? Well you need to
create a league of teams and these teams have points. You create matches and you
get the match scores from the user, so the person will enter the match scores. Now for each match you need to calculate the points awarded for each team, if a team
wins they get three points, if they draw they get one point and if they lose they
don’t get any points. Now you calculated the match points, update those points in
the league table. So for an example we can use the current English Premier
League and here’s the top six teams. Liverpool Manchester City Arsenal
Leicester Chelsea and Crystal Palace. We’re going to use scratch to design the
program and your challenge is to code it in Python. So we can go through the
answer after we’ve done the design. First in scratch we’re going to show you
the challenge and then after that you can either build it yourself in scratch
if you wish or you can just watch our explanation to see the design of the
program. Your challenge is to write the Python code. So let’s have a look at the
challenge in scratch. There is of course many ways you can do this but let’s have
a look at a way that we did it in scratch. Here we have the league of teams the six that we showed you with the top of
the English Premier League and their points, so we got Liverpool when City
Arsenal Leicester Chelsea Crystal Palace, and the points 24 16 15 and 14 for the
three teams in fourth, fifth and sixth. Now when we enter a match we need to
enter the home team and the away team, and also the amount of goals they scored.
Once we enter that we calculate the points for the match. And once we’ve got the points for the match and we can update the league. We’re
only just updating the points we’re not sorting the league at the moment. Now to
check the program works we need to check a home win and away win and a draw match,
so in this example we’ve got three matches. So we’ve got the team in first
place Liverpool playing the team of sixth place, Crystal Palace, the team in
fifth place Chelsea, against the team Arsenal in third
place and Leicester who are in fourth v Man City the team in second. We are going to enter three matches the home team the away team the home score away
score. If you look you can see the points will have been update here and these
will update in the league. So let’s have a look at the program running. So let’s
start. So how many matches do we want to enter? We want to enter three, so enter
the home team the first match between Liverpool number one and Crystal Palace
number six, and the score in this match is going to be two nil,
a home win, so you see our home team points three, away team zero, nd Liverpool’s points in the
league move from 24 to 27. Now enter home team Chelsea in five, away team Arsenal are three and we want
this to be a draw. So let’s try one one. So our points change to one point each
and Arsenal and Chelsea’s points are increased by one in the league. Now the
last match was Leicester and number four, the away team Man City number two, and this we
need to try an away win, so let’s try one nil to the away team, zero one. So
now the away team get three points home team don’t get any points, Leicester stays
the same but Man city moved from 16 to 19. Here’s an example we’ve entered
the matches, we’ve calculated the match points, and we’ve added the points to the
league. So we start off with entering the team
and the team scores which is input, which we know we can use the ask the question
and wait, and then we get the answer. So if you want you to put a home score in, that
would be a variable we can make a variable in data, so we could call the
variable home score. Okay and then we could set that to answer. So if we put
enter home score and then when the user puts in the home score set home score to
answer. We need to do that again for the away team, so the away score, make a
variable it could be a away score variable and then we’re going to set that to
be the answer the second question . We can get the home score and the away score. Now the second part we calculated the
points so we can use condition or the control blocks with the if-then and else
block. So what we want to know is we want to know if the home score is greater
than the away score. So we can use the operators so we want to know home score
is greater than the away score. So if that happens then we know the home score
is greater than the away score we’re going to have the points for the home
team is going to equal three so we can create another variable with home score points. So we can set the home score points
to equal three if the home team wins Notice here that I’ve used an
underscore because in Python when you name a variable you don’t want to leave
a space in between the words we use an underscore. So we can do the same
with home score greater than away score. We can duplicate that, we want to know if
the away teams won or if it’s a draw so we can use another if-else statement so
first of all if home team wins we make the home score points equals three now if
the away score is greater than the home score you need to set the away score
points to equal three. So again let’s go back to make a variable so we have away
should be away team team points. OK, so set a away team points to equal
three. Now if neither scores are greater than the other one it means it’s a draw,
so we can then set the home team and away team points to one. It should say home team points but it
doesn’t mind we’re just going through the design of the blocks. So that’s how
we calculate the points we can use the if-else and if-else nested inside that
in this example. So we’ve used our input and we’ve used our control if-else now
if we know the points we need to update the table so the update
table we need the table itself which is going to be a list. So we had a list of
teams so imagine if we have our teams we had
Liverpool for example and then we had Man City and also we had a list called league with the points in. So the points were
Liverpool had 24, Mam City had 16. For now we’re just going to enter them as an
example so we’ve got our scores, we’ve got calculate points and now we need to
update the list so these have already got a value in, so we need to replace the
item of the league with something. So the home team points we’ve called them home score
points we need to add that so we need to get the item in league and then we need
to add the points they’ve got from the match. So we need the operator plus, so we
have the item, let’s remove that. If for example the team in position 1 was
playing at home we could say as I add the home team points and if you
duplicate that the team in position 2 if they were the away team and then we would add their points. So if
we entered in a one or draw then the points would be one for home team and
points one for away team and let’s assume for now that the team in position
one is the home team, position two is the away team and these points would add by
one. So if we run that we would probably use functions
rather than run all of this code together. We would start off by creating the
league by setting up the league first because it has the points in, then we
would get the score of the matches, calculate the points and then we would
update the table, so it gives you an idea of how we design our code, so let’s just
try that with a one all draw, one into home score one inter we score should be a
away score is one so therefore we had homes points one, away points one, and
24:16 increased to 25, 17. So that gives you an idea of what
we need to do, we need to set up the table first, then we need to get the
input from the user, then we need to calculate the points then we use need to
update the table. in the demonstration we entered three
matches so let’s have a look at the full range of blocks we did for the
demonstration. So as you can see here we start off by setting up the table, in
scratch we can have some values still remaining from the program where we run
it before so we need to set up the two tables and this one we’ve got teams and
league this was the original program, so we delete any previous values and
then enter the details into those two lists. Then we get the scores we ask for
how many matches which we entered three in a demonstration and then we loop
three times to get the home team, away team, home team score, and away score. So let’s just move these out of the way. Now once we’ve got the match details then we
calculate points which is the same as we demonstrated before the only difference
is at the end we’re going to broadcast update points. So when we update the
points, you can see here if I just move these out the way, very similar we see
before, replace the item with the home team so position 1 2 3 4 5 6 with those
points plus what’s over whatever value is already in the list do that for the
home team and the away team, so that’s updating
the points. So we set the table up, we’ve got the scores, we’ve got the scores we
calculate to the points, once we calculated the points we update the
table. It’s quite a challenge to build that in
Python but you understand the concepts if you’ve watched the videos you might
need to go back to reference some of the Python code in the videos but good luck.
We will bring out another video showing you the solution and that will be coming
soon. So thank you for watching and if you
haven’t already please subscribe. Bye for now.

3 Comments

  1. Do you know when you will give the solution?

  2. This is the first video of an exercise to test the knowledge from the video series. It is not easy so this video explains the design and the next video will go through how to build the python code. Please ask if you need help, this is a challenging task.

  3. This is the first video introducing a challenging exercise. The task should be completed in parts and tested along the way. To help design the code this video demonstrates the result of the challenge in Scratch, explains the design and shows all the complete program in Scratch. The solution will be explained in the next video coming soon.

Leave a Reply

Your email address will not be published. Required fields are marked *