1
00:00:00,120 --> 00:00:02,850
All right, so how did you get on with today's project?

2
00:00:03,210 --> 00:00:07,190
Hopefully it went really well and you're just here to see how I did it.

3
00:00:07,860 --> 00:00:13,860
As I mentioned before, it's really useful when you're trying to create a large project to break it

4
00:00:13,860 --> 00:00:18,540
down into smaller pieces so that you can tackle each of the pieces one by one.

5
00:00:18,960 --> 00:00:22,270
And a great way of doing that is by creating your own flowchart.

6
00:00:22,770 --> 00:00:28,260
So as I've shown you before in the end of the last lesson, this is the flowchart that I've created

7
00:00:28,260 --> 00:00:34,560
for this particular program, and I'm going to follow it step by step to create this final blind auction

8
00:00:34,560 --> 00:00:35,100
program.

9
00:00:35,880 --> 00:00:41,820
The first thing we're going to do is we're going to show the logo from this file art.py, and notice

10
00:00:41,820 --> 00:00:45,480
how this logo is stored inside a variable called logo.

11
00:00:46,080 --> 00:00:51,990
The first thing we have to do is to fork our own copy of the starting project, and then we're going

12
00:00:51,990 --> 00:00:58,410
to look inside the art.py module and then we're going to import that logo.

13
00:00:58,980 --> 00:01:04,030
So now I can go ahead and print my logo.

14
00:01:04,830 --> 00:01:10,790
So that's the first step done. Next we're going to ask for a name.

15
00:01:10,800 --> 00:01:13,030
So that's going to be in the form of an input.

16
00:01:13,470 --> 00:01:19,020
So let's go ahead and create a variable called name and we're going to set it to be an input.

17
00:01:19,920 --> 00:01:23,760
And the input is going to say, what is your name?

18
00:01:25,310 --> 00:01:28,950
So they're going to type in their name and that's going to be stored inside this variable.

19
00:01:29,690 --> 00:01:35,780
Now, the next thing we're going to do is ask them for their bidding price. We'll say that inside a variable

20
00:01:35,780 --> 00:01:41,540
called price and we'll create another input to ask them what is your bid?

21
00:01:42,080 --> 00:01:48,050
And at the end of this, I'm going to add a dollar sign just so that when they are typing their bids,

22
00:01:48,140 --> 00:01:49,520
they'll see this line.

23
00:01:49,860 --> 00:01:55,100
And when they start typing, the cursor should be about here so they can see the dollar symbol before

24
00:01:55,100 --> 00:01:55,730
their price.

25
00:01:57,050 --> 00:02:03,300
So now we're down to here and we have to add our name and our bidding price into a dictionary.

26
00:02:03,800 --> 00:02:10,550
Let's go ahead and create it up here and we'll call it bids and it's going to start out as an empty dictionary.

27
00:02:11,150 --> 00:02:15,680
And then later on, once we've got the name and the price, we said that the name was going to be the

28
00:02:15,680 --> 00:02:22,850
key and the price was going to be the corresponding value. To add a new entry to our bids dictionary,

29
00:02:22,970 --> 00:02:28,580
we specify the dictionary, we add a set of square brackets, and then we add the key.

30
00:02:29,060 --> 00:02:33,230
The key is going to be the name and the value is going to be the price.

31
00:02:33,410 --> 00:02:34,940
So we would write it like this.

32
00:02:36,800 --> 00:02:42,410
Now, we get down to the slightly trickier part. We're going to ask if there are any other users who

33
00:02:42,410 --> 00:02:47,840
want to bid. And if the answer is yes, then we're going to clear the screen and we're going to loop

34
00:02:47,840 --> 00:02:51,260
back to where we ask for the next name and the next bid price.

35
00:02:52,190 --> 00:02:53,990
How do we achieve this?

36
00:02:56,090 --> 00:03:01,720
Well, we can put this into a while loop. Let's go ahead and indent this.

37
00:03:02,090 --> 00:03:09,260
So I just typed command and then the right-sided square bracket. Or on Windows, it will be control and the right

38
00:03:09,260 --> 00:03:10,270
side of the square bracket.

39
00:03:10,670 --> 00:03:13,070
And then we can wrap this into a while loop.

40
00:03:13,610 --> 00:03:18,950
If we had some sort of variable that would keep track whether if the bidding is finished.

41
00:03:19,370 --> 00:03:22,190
So let's call it bidding_finished.

42
00:03:23,300 --> 00:03:25,460
And of course, it starts out being False.

43
00:03:26,120 --> 00:03:34,850
Now, while bidding is not finished, so while not bidding_finished, so basically while this is still

44
00:03:34,850 --> 00:03:39,190
false, then we're going to keep running this until forever.

45
00:03:39,770 --> 00:03:42,290
But of course, we need a way to stop this.

46
00:03:42,290 --> 00:03:49,880
So we're going to create a new input where we ask the user, "are there any other bidders?"

47
00:03:51,540 --> 00:03:54,030
Type "yes" or

48
00:03:55,360 --> 00:04:02,680
"no". This way we get to get an input which is going to be either the word yes or the word no, and

49
00:04:02,680 --> 00:04:07,400
we can save this inside a variable called maybe should_continue.

50
00:04:08,320 --> 00:04:17,740
So this way we can check this variable and we can see, well, if should_continue is equal to "no",

51
00:04:18,100 --> 00:04:24,400
well, in that case, that's when we're going to flip the switch and change this bidding_finished variable

52
00:04:24,670 --> 00:04:25,600
to true.

53
00:04:26,190 --> 00:04:32,350
So this means that when we get to the end of this block in the while loop, then when it checks this

54
00:04:32,560 --> 00:04:37,010
bidding_finished, it's not going to continue to loop anymore.

55
00:04:37,780 --> 00:04:44,620
Now, on the other hand, if should_continue was yes or in fact any other value, then we can keep continuing

56
00:04:44,620 --> 00:04:48,910
and we don't actually have to change this variable because it's already false.

57
00:04:50,500 --> 00:05:00,850
Now we have our code looping, which means that unless the user types in a 'no' at the very end and bidding

58
00:05:00,850 --> 00:05:03,700
becomes finished, this is this path.

59
00:05:04,030 --> 00:05:11,170
Otherwise then we're going to keep going through all of these lines of code which is basically all

60
00:05:11,170 --> 00:05:16,040
of this until we get to the point where this switch actually gets flipped.

61
00:05:16,780 --> 00:05:23,200
So if you notice that if the user actually answers yes, then we want to clear the screen before we

62
00:05:23,200 --> 00:05:24,040
repeat the loop.

63
00:05:24,550 --> 00:05:26,640
That means we can check to see

64
00:05:26,650 --> 00:05:30,190
well, elif should continue is equal to.

65
00:05:30,490 --> 00:05:31,140
yes,

66
00:05:31,570 --> 00:05:36,850
well, in this case, we're going to call that clear() function that I showed you in the hint at the very

67
00:05:36,850 --> 00:05:37,370
beginning.

68
00:05:37,810 --> 00:05:44,260
So this function comes from the replit module and it allows us to clear the output in the console so

69
00:05:44,260 --> 00:05:51,490
that the previous name and bid gets cleared on off the screen and it leaves a fresh start for the next

70
00:05:51,490 --> 00:05:52,030
user.

71
00:05:53,650 --> 00:06:01,090
The last thing we have to do is to actually address this right? So when they answered no, they get taken

72
00:06:01,090 --> 00:06:08,650
out of the while loop, but we still have to find the highest bidder from our dictionary and to declare

73
00:06:08,650 --> 00:06:09,860
them as the winner.

74
00:06:10,570 --> 00:06:11,770
How do we do this?

75
00:06:14,080 --> 00:06:21,340
Well, let's put this in a separate function. So we could create a new function called find_highest_bidder.

76
00:06:23,750 --> 00:06:32,870
And this is going to get the bidding_record as the inputs, so this will be the dictionary that represents

77
00:06:32,870 --> 00:06:40,400
all the bids, the names and the values. I think it's easier if we can visualize what this bidding_

78
00:06:40,400 --> 00:06:41,680
record might look like.

79
00:06:42,080 --> 00:06:51,060
So we know it's a dictionary of key-value pairs where each key is a name and each value is a number.

80
00:06:51,530 --> 00:06:55,580
So let's say we had only two bidders, Angela and James.

81
00:06:56,030 --> 00:06:58,370
Angela bid $123.

82
00:06:58,370 --> 00:07:00,990
James bid $321.

83
00:07:01,130 --> 00:07:03,700
So we can see who's clearly going to be the winner here.

84
00:07:04,250 --> 00:07:07,100
But how do we get our code to figure that out?

85
00:07:08,120 --> 00:07:14,680
We need to somehow loop through this dictionary, right? which is called bidding_record.

86
00:07:15,920 --> 00:07:25,040
We know that we can use a for loop to do this so we can say for each bidder in the bidding_record,

87
00:07:25,940 --> 00:07:33,410
and this is a good point to remember that when you use a for loop on a dictionary, instead of looping

88
00:07:33,410 --> 00:07:39,560
through each item in the dictionary, like each of these key-value pairs, it actually loops through

89
00:07:39,560 --> 00:07:40,940
each of the keys.

90
00:07:41,360 --> 00:07:47,090
And we covered this in the very first lesson of today where we talked about how the for loop works

91
00:07:47,090 --> 00:07:53,150
when we're looping through a dictionary. Now, we're going to get hold of each of the bidders inside

92
00:07:53,180 --> 00:07:54,530
this dictionary.

93
00:07:55,130 --> 00:07:56,540
So what do we do with it?

94
00:07:57,260 --> 00:08:00,410
Well, we want to use the key to get the value.

95
00:08:00,560 --> 00:08:05,630
Let's take the bidding_record and pass in the key,

96
00:08:06,890 --> 00:08:09,330
and this should give us the value.

97
00:08:09,710 --> 00:08:16,770
So now we can actually save this inside a variable which I'm going to call bid_amount.

98
00:08:17,810 --> 00:08:23,090
So the first time this for loop runs, the bid_amount is going to equal 123

99
00:08:23,690 --> 00:08:31,040
and the next time the for loop runs, the bid amount is going to be equal to 321. If we had some sort

100
00:08:31,040 --> 00:08:33,320
of way outside of the for loop

101
00:08:33,330 --> 00:08:42,110
that keeps track of the highest bid, well then we could use this number to check against the current

102
00:08:42,110 --> 00:08:42,950
bidding amount.

103
00:08:43,610 --> 00:08:45,560
We can use an if statement to check

104
00:08:45,560 --> 00:08:53,600
well, if the bid amount is greater than the highest bid, well, in that case, then the highest bid

105
00:08:53,600 --> 00:08:55,220
becomes the bid amount.

106
00:08:57,290 --> 00:09:04,700
So what this means is that we start out with a zero. The first time the for loop is run, the bidder

107
00:09:04,700 --> 00:09:06,280
is going to be equal to Angela,

108
00:09:06,470 --> 00:09:12,410
the bid amount is going to be equal to Angela as the key passed into this dictionary, which is going

109
00:09:12,410 --> 00:09:18,960
to get us the value 123. If 123 is greater than zero,

110
00:09:19,100 --> 00:09:26,960
well, then this highest bid is now going to become 123. The next time the for loop runs

111
00:09:26,960 --> 00:09:32,060
the bidder is now James and the bid amount is now 321.

112
00:09:32,570 --> 00:09:37,330
Now we're checking if 321 is greater than the highest bid.

113
00:09:37,520 --> 00:09:40,990
Remember this, in the last round, got set to 123.

114
00:09:41,330 --> 00:09:42,680
So this is going to be true.

115
00:09:42,920 --> 00:09:48,410
And this means James' bid, 321, is now going to be the highest bid.

116
00:09:49,590 --> 00:09:56,850
In addition to keeping track of the highest bid, we can keep track of the winner. If this is indeed

117
00:09:56,850 --> 00:10:04,110
true, if the current bidder has the highest bid, well then at the same time as setting the highest

118
00:10:04,110 --> 00:10:04,390
bid,

119
00:10:04,620 --> 00:10:07,650
we should also set the winner to the key,

120
00:10:07,860 --> 00:10:09,210
so the current bidder.

121
00:10:11,750 --> 00:10:18,500
And once we've gone through this for loop and we've looped through all of the records in the bidding record,

122
00:10:18,770 --> 00:10:21,380
well then finally we can actually print

123
00:10:22,670 --> 00:10:25,100
"The winner is..."

124
00:10:26,140 --> 00:10:34,750
the winner that's stored in this variable. And we're going to say they won with a bid of... and let's add

125
00:10:34,750 --> 00:10:38,830
a dollar sign and then we're going to pass in the highest_bid.

126
00:10:40,270 --> 00:10:46,990
Now, we can turn this into an fstring and it will use both of these variables, which is storying

127
00:10:46,990 --> 00:10:51,580
the highest bid and the winner's name to print out who the winner is.

128
00:10:52,060 --> 00:10:58,210
So down here, when should_continue is no going down this path,

129
00:10:58,540 --> 00:11:04,330
we can now print and declare the winner. In addition to saying that bidding finished is true,

130
00:11:04,600 --> 00:11:11,200
we're also going to call that function, find_highest_bidder, and pass in all of the bids that we

131
00:11:11,200 --> 00:11:13,240
have been keeping track of over here.

132
00:11:14,480 --> 00:11:19,940
Now, just before we run our code, let's go through each line and make sure that everything looks all

133
00:11:19,940 --> 00:11:20,270
right.

134
00:11:20,780 --> 00:11:23,160
Firstly here, where it says, 'what is your name?'

135
00:11:23,190 --> 00:11:25,940
maybe we can add a colon and then a space

136
00:11:26,240 --> 00:11:28,180
so the user starts typing here.

137
00:11:28,790 --> 00:11:30,240
Then it asks, 'what is your bid?'

138
00:11:30,320 --> 00:11:35,390
We can add a colon as well and the user will start typing after the dollar sign.

139
00:11:36,080 --> 00:11:41,720
Now, I can already see a problem that's actually going to happen because of this line 23.

140
00:11:42,050 --> 00:11:47,210
I wonder if you can spot the issue here, especially if you think about what we're going to do with

141
00:11:47,210 --> 00:11:48,560
this price later on.

142
00:11:49,730 --> 00:11:56,300
But I'm going to add one more new line here and then I'm going to run the code and we'll see what actually

143
00:11:56,300 --> 00:11:56,860
happens.

144
00:11:58,030 --> 00:11:59,950
So now let's go ahead and run the code.

145
00:12:02,930 --> 00:12:10,220
And you can see our gavel printed and then it asks us for our name, so I'm just going to put my name and

146
00:12:10,220 --> 00:12:16,440
then 123. And then one other bidder who's going to be James, 321.

147
00:12:16,460 --> 00:12:19,570
And now if I say no, there are no more bidders,

148
00:12:19,940 --> 00:12:21,650
see what happens when I hit enter.

149
00:12:22,370 --> 00:12:23,600
I get a type error

150
00:12:23,720 --> 00:12:30,790
and it tells us that the greater sign is not supported between instances of a string and an int

151
00:12:31,040 --> 00:12:33,500
and this happened on line 16.

152
00:12:35,130 --> 00:12:42,870
If we take a look at line 16, basically we're comparing the bid amount, which is the value that came

153
00:12:42,870 --> 00:12:46,490
out of the bidding record, which is what's being passed in here,

154
00:12:46,950 --> 00:12:53,760
and that, of course, is a dictionary where the keys are the names of the bidders and the value is

155
00:12:53,790 --> 00:12:54,390
the price.

156
00:12:54,780 --> 00:12:56,310
So what's actually happening here?

157
00:12:56,610 --> 00:13:02,820
Well, it's because when I took this price input in, I kept it as a normal input.

158
00:13:03,060 --> 00:13:06,780
Instead, it should be turned into an integer.

159
00:13:07,200 --> 00:13:14,370
So that way we can work with it as a number so that when we're comparing it against, say, zero or

160
00:13:14,370 --> 00:13:18,190
whatever other number later on, it can actually do the comparison.

161
00:13:18,570 --> 00:13:26,100
It doesn't actually make sense to the computer when you're comparing a string versus a number. That doesn't

162
00:13:26,100 --> 00:13:26,940
actually work.

163
00:13:26,970 --> 00:13:30,990
And this was covered when we talked about data types in Python.

164
00:13:31,680 --> 00:13:33,810
Now, if we hit run again

165
00:13:35,350 --> 00:13:42,700
and we do the same thing, and when I say no, you can see that it tells me the winner is James with

166
00:13:42,700 --> 00:13:45,160
a bid of 321.

167
00:13:46,060 --> 00:13:53,500
Essentially, the hardest part of this challenge is figuring out how to loop through a dictionary and

168
00:13:53,500 --> 00:14:00,640
get hold of each of the bidders and each of their values and compare them historically against previous

169
00:14:00,640 --> 00:14:03,070
values inside this dictionary.

170
00:14:03,790 --> 00:14:11,020
If this function is confusing to you, then I really recommend either going over to Thonny, pasting

171
00:14:11,020 --> 00:14:14,720
the code in and exploring it by going through it step by step.

172
00:14:15,250 --> 00:14:20,680
Alternatively, if you can't install Thonny or you don't have access to your normal computer, then you

173
00:14:20,680 --> 00:14:23,800
can also go to a website called pythontutor.com.

174
00:14:24,670 --> 00:14:31,420
And this also lets you visualize your code, although in a slightly more simplified way. If I go ahead

175
00:14:31,420 --> 00:14:38,740
and paste in my function, find_highest_bidder, passing in the bid record, let's go ahead and create

176
00:14:38,740 --> 00:14:41,100
a new dictionary of bids.

177
00:14:42,160 --> 00:14:47,770
So now we have a dictionary of bids with two key-value pairs just as we entered before,

178
00:14:48,160 --> 00:14:56,410
and then I'm going to call this function, find_highest_bidder, and pass in these bids.

179
00:14:57,370 --> 00:15:03,170
Now, if we go ahead and click visualize execution, you can see it's going to go line by line, and we

180
00:15:03,170 --> 00:15:06,460
can click next to see how it's jumping through this.

181
00:15:06,910 --> 00:15:12,980
Firstly, it's bypassing this function, creating this next dictionary.

182
00:15:13,750 --> 00:15:17,710
So now it's got a function stored somewhere, which is this one,

183
00:15:17,950 --> 00:15:21,010
it's got a dictionary called bids stored somewhere else.

184
00:15:21,520 --> 00:15:26,950
And now we actually get to the line, which is going to execute that function and it's going to pass

185
00:15:26,950 --> 00:15:31,330
over this dictionary of bids to this function.

186
00:15:31,990 --> 00:15:40,390
And if you step through this line by line, you can see it creating these new variables and populating

187
00:15:40,420 --> 00:15:42,720
them. The first time we run it

188
00:15:42,730 --> 00:15:50,400
the highest bidder is zero, the winner is an empty string, and the bidder in the for loop is now Angela.

189
00:15:50,410 --> 00:15:54,010
It's the first key from the dictionary as it points to here.

190
00:15:54,610 --> 00:15:55,660
Now if we keep clicking

191
00:15:55,660 --> 00:16:01,660
next, it works out the bid amount which is the value corresponding to that key, and then it works

192
00:16:01,660 --> 00:16:08,920
out the highest bid by comparing whether if the current bid amount is greater than the current highest

193
00:16:08,920 --> 00:16:09,220
bid.

194
00:16:09,610 --> 00:16:11,900
So 123 is greater than zero

195
00:16:12,190 --> 00:16:18,820
so when I click next, you'll see highest bid gets replaced with the bid amount and the winner is now

196
00:16:18,820 --> 00:16:23,830
going to be set as Angela. The next time the loop runs the bidder is

197
00:16:23,830 --> 00:16:30,040
now James, and if I keep going through this, you can see that James actually ends up with the highest

198
00:16:30,040 --> 00:16:31,960
bid and becomes the winner.

199
00:16:32,500 --> 00:16:37,990
So now the for loop is done because it's looped through all items in the dictionary and it's now going

200
00:16:37,990 --> 00:16:43,190
to print the output, which is 'The winner is James with a bid of $321.'

201
00:16:43,750 --> 00:16:48,040
This is another way that's really, really helpful to visualize your code.

202
00:16:48,550 --> 00:16:52,990
And the other great thing is that even if while you're visualizing there's still something you don't

203
00:16:52,990 --> 00:16:58,350
understand, you can actually click on this button to get live help from users across the Internet.

204
00:16:58,840 --> 00:17:05,109
And if you want to practice your own debugging, just as we did in today's lesson, you can also click

205
00:17:05,109 --> 00:17:09,160
to help some of these users who are looking for help with their code.

206
00:17:09,310 --> 00:17:14,410
And you can practice reading code, understanding code and helping other people with their bugs and

207
00:17:14,410 --> 00:17:15,119
their issues.

208
00:17:16,540 --> 00:17:22,329
Finally, if you want to take a look at the completed code for today's project, then as always, you

209
00:17:22,329 --> 00:17:27,339
can head over tor repl.it/@appbrewery/blind-auction-completed.

210
00:17:27,910 --> 00:17:34,090
And it's important to remember that this is just one way of completing this project.

211
00:17:34,120 --> 00:17:40,660
There are lots and lots of other ways. But as long as you test your program thoroughly and it works

212
00:17:40,660 --> 00:17:44,670
in the same way, then it doesn't matter too much how you've done it.

213
00:17:45,520 --> 00:17:51,940
But it's important that you've absorbed the knowledge in today's lessons, and the most important of

214
00:17:51,940 --> 00:18:00,550
which is this concept of dictionaries, adding to dictionaries, and also looping through dictionaries.

215
00:18:01,330 --> 00:18:07,030
So have a look through the completed code and also have a play around with Python Tutor, which is

216
00:18:07,030 --> 00:18:09,460
a really great resource for your learning.

217
00:18:09,940 --> 00:18:13,240
And I'll see you tomorrow for more lessons on Python.

