WEBVTT

00:00.480 --> 00:01.080
All right.

00:01.080 --> 00:06.360
So now that you've learned all about dictionaries, we're going to put all of that knowledge to test

00:06.360 --> 00:10.560
and to use by building a blind auction program.

00:11.360 --> 00:12.640
So here's the program.

00:12.880 --> 00:19.440
When you first run it, it's just going to show you the logo art and maybe a line that tells you what

00:19.440 --> 00:20.400
program you're in.

00:20.560 --> 00:22.320
And then it's going to ask you for your name.

00:22.320 --> 00:24.880
So let's say we have three bidders, right.

00:24.960 --> 00:31.240
So first go I go to the computer, I type in my name, I type in my bid and I hit enter.

00:31.680 --> 00:33.680
It asks me if there are any other bidders.

00:33.680 --> 00:34.600
I look around the room.

00:34.600 --> 00:37.040
I see two other colleagues, so I type.

00:37.080 --> 00:37.600
Yes.

00:38.040 --> 00:43.480
And now it clears the entire screen so that other people can't see what my bid was.

00:43.840 --> 00:47.960
So now I leave the computer and my colleague comes over.

00:48.200 --> 00:52.240
They type in their name and also their bid.

00:53.360 --> 00:57.680
And again they look around the room and they see there's one other person who wants to bid.

00:57.680 --> 00:58.960
So they hit yes.

00:59.090 --> 00:59.490
again.

00:59.490 --> 01:00.650
It clears their bid.

01:00.850 --> 01:04.130
And finally, the last person makes their bid.

01:04.730 --> 01:06.490
At this point, everybody has bid.

01:06.490 --> 01:08.130
So she's going to type no.

01:08.570 --> 01:15.930
And it's at this moment when our program is going to work out who has made the highest bid and print

01:15.930 --> 01:18.730
out who the winner is and what their bid was.

01:19.170 --> 01:24.290
This way, instead of competing with each other on bids, we actually all just give the highest price

01:24.290 --> 01:25.330
we're willing to pay.

01:25.570 --> 01:29.290
And this is known as quite an efficient way of making a bid.

01:29.450 --> 01:34.850
And you see that a lot in, say, the construction industry and government, etc..

01:35.370 --> 01:42.010
Now, one of the most complex parts of actually trying to build this program is how do you clear the

01:42.010 --> 01:43.930
screen in between bids?

01:44.210 --> 01:53.890
Now, what I want you to do in your version of the code is I simply want you to print out a new line

01:53.890 --> 01:54.530
character.

01:54.530 --> 02:00.630
So if we print a string and the string happens to be just the backslash n.

02:00.630 --> 02:01.950
So just a new line.

02:02.150 --> 02:09.470
And then we can do this thing where we multiply this string by say 20 times or 100 times.

02:09.790 --> 02:14.750
And what this means is it's basically going to add 100 new lines.

02:14.830 --> 02:24.030
So if I go ahead and print hello and then I go ahead and have an input, um, type something.

02:24.430 --> 02:29.190
Now if I run this code, you'll see that hello was printed.

02:29.390 --> 02:33.950
And then immediately the next thing I did was to basically clear the screen.

02:33.950 --> 02:37.870
And I did that by typing backslash n 100 times.

02:38.030 --> 02:41.550
And this programmatically will scroll to this part.

02:41.550 --> 02:43.870
And now I can use this input.

02:43.870 --> 02:49.030
And the user won't be able to see the previous part unless they know how to scroll up.

02:49.230 --> 02:55.680
In addition, we've also got the starting art of the gavel logo, which you're going to put into the

02:55.680 --> 02:56.320
program.

02:56.600 --> 03:04.240
And the most important thing of all is that I want you to create a dictionary so that each person's

03:04.280 --> 03:09.960
name will be the key and their bid will be the value.

03:10.480 --> 03:15.600
And this dictionary is going to contain many of these key value pairs.

03:16.000 --> 03:21.960
And at the very end of the program, when everybody's done with bidding, then we're going to loop through

03:21.960 --> 03:25.680
that dictionary and figure out who has made the highest bid.

03:26.320 --> 03:33.560
If you want, this is a good time to analyze this completed project and see if you can create your own

03:33.560 --> 03:37.640
flow chart using something like Draw.io that I've shown you before.

03:37.880 --> 03:41.920
Or just take a piece of paper and a pen and draw out your flow chart.

03:42.400 --> 03:48.480
Now if you want a hint, then you can head over to the course resources and you'll get taken to the

03:48.480 --> 03:51.200
flow chart that I've created for this program.

03:51.480 --> 03:54.780
So spoiler alert I'm about to show you that flowchart.

03:54.980 --> 03:59.340
So if you don't want to see it and you want to create your own because it's a relatively simple program,

03:59.340 --> 04:02.620
then pause the video now and complete the challenge by yourself.

04:03.180 --> 04:09.140
Now, for everybody else who's still here, I want to show you this is a flowchart I've created to represent

04:09.140 --> 04:10.060
this program.

04:10.700 --> 04:12.300
So we start at the beginning.

04:12.300 --> 04:17.700
We first show the logo from that file, art dot Pi, and then we asked for a name input.

04:17.740 --> 04:19.020
We asked for their price.

04:19.420 --> 04:24.500
Now we're going to add the name and the bid into a dictionary, with the name as the key and the bid

04:24.500 --> 04:25.460
as the value.

04:25.900 --> 04:31.140
Now, once we're done with that, then we get to ask the user for another input and see if there are

04:31.140 --> 04:33.020
any other users who want to bid.

04:33.020 --> 04:37.900
So if they answer yes, then we're going to clear the screen and we're going to go back to here.

04:37.940 --> 04:41.180
Ask for another name, another price and so on and so forth.

04:41.500 --> 04:46.420
But if they say no, everybody's done with bidding, then we're going to call some sort of function

04:46.420 --> 04:52.550
that we have, which is going to loop through our dictionary that was created here and find the person

04:52.550 --> 04:58.310
who had the highest bid value and then declare them as the winner and print out their bid.

04:58.750 --> 05:03.030
So it's a relatively simple program, at least compared to other things that you've built so far.

05:03.350 --> 05:06.870
So this is the time to pause the video and give it a go.

05:25.110 --> 05:25.510
All right.

05:25.550 --> 05:32.150
Now I'm going to walk you through the solution, or at least my solution for the blind auction project.

05:32.630 --> 05:38.950
So the first thing that I'd like to do when we have something that is a large project is to break it

05:38.950 --> 05:39.310
down.

05:39.310 --> 05:42.510
So you saw me draw a flowchart.

05:42.790 --> 05:48.990
And the next step is I could translate that flowchart into several to do's in my code.

05:49.470 --> 05:56.360
So the first thing I'll probably want to do is to ask the user for some inputs.

05:56.840 --> 05:58.800
And the inputs that I need.

05:58.840 --> 06:07.360
If you take a look at the demo is it asks for what is your name and then what is your bid.

06:07.360 --> 06:09.920
And then it's got this dollar sign in front.

06:10.600 --> 06:14.600
And then it asks, are there any other bidders type yes or no.

06:14.920 --> 06:18.720
So we need a way of capturing these inputs right.

06:19.000 --> 06:21.760
So that could be R to do number one.

06:22.520 --> 06:31.040
Next we also need to save the data that the user gives us into a dictionary.

06:32.800 --> 06:41.280
And that way we can later loop through the dictionary where we have a list of name and then price.

06:42.080 --> 06:46.880
And we'll be able to figure out who is the name that had the highest price.

06:47.820 --> 06:56.260
Now, the next thing we might want to do is to figure out whether, if the user wants to continue adding

06:56.260 --> 06:57.140
new bids.

07:03.060 --> 07:09.860
And this might involve some sort of a loop so that we can keep going back to ask the user for input.

07:09.900 --> 07:13.180
But of course it will be a different user each time.

07:13.900 --> 07:22.140
Now the final thing to do is to actually go ahead and compare all the different bids and then figure

07:22.140 --> 07:26.300
out who has the highest bid in the dictionary.

07:28.860 --> 07:31.180
So that's pretty much all there is to it.

07:31.180 --> 07:34.980
And let's go ahead and tackle it one by one.

07:35.580 --> 07:39.500
So firstly let's ask the user for some input.

07:39.500 --> 07:43.700
So let's save their name into a variable called name.

07:44.060 --> 07:48.110
And then we'll use an input to ask them, what is your name?

07:49.150 --> 07:53.550
And then we'll add a colon, maybe a space so they can type their name here.

07:54.390 --> 07:59.110
And then once they've typed their name we'll ask them for a price.

07:59.430 --> 08:01.750
So again it's going to be an input.

08:01.750 --> 08:04.390
And we're going to say what is your bid.

08:05.070 --> 08:11.950
And now we're going to add a dollar sign or whatever currency you work with in front of their bid.

08:12.190 --> 08:14.990
But remember that they're going to write a number in here.

08:14.990 --> 08:17.670
And inputs are strings.

08:17.910 --> 08:20.350
You can see that it's going to give you a string.

08:20.350 --> 08:26.870
So in order for us to be able to save it into our dictionary as a number, I'm going to convert it to

08:26.910 --> 08:27.950
an integer.

08:27.990 --> 08:33.110
Now of course, if you want bids that have floating points decimal places, you could also convert this

08:33.150 --> 08:34.190
into a float.

08:34.670 --> 08:42.990
So now that we've got our number and our name, the next thing to do is to save that data into a dictionary.

08:43.930 --> 08:49.650
So if we have a dictionary that we call, let's say we call it bids.

08:50.170 --> 08:53.010
And we started out as an empty dictionary.

08:53.530 --> 09:00.370
Then once we've gotten hold of the data from the user, the name and the price, we can then add to

09:00.410 --> 09:02.290
this dictionary of bids.

09:02.490 --> 09:03.970
So we can say bids.

09:03.970 --> 09:06.610
And then the key is going to be their name.

09:06.930 --> 09:09.570
And we can set it equal the value.

09:09.930 --> 09:16.930
So if this is at all unfamiliar then you might need to go and review the lesson where we learnt about

09:16.930 --> 09:22.530
dictionaries, how to modify, how to edit, how to add, how to create empty dictionaries.

09:22.810 --> 09:29.130
But this is essentially the process to save the data into our dictionary in this format.

09:29.730 --> 09:36.690
So now we need to ask the user whether if there are any new bids that need to be added.

09:37.090 --> 09:44.500
So we could probably have a variable called should continue and we could set that to equal an input.

09:44.860 --> 09:46.380
And our prompt could be.

09:46.700 --> 09:49.820
Are there any other bidders.

09:50.620 --> 09:53.740
And we can say type uh yes.

09:55.620 --> 09:59.380
Um or no.

10:01.940 --> 10:06.020
And then we could add a new line so that they type on the next line.

10:06.300 --> 10:09.300
And now we have this variable should continue.

10:09.820 --> 10:18.900
So how can we use this variable to go back to the beginning and ask for the new person's name price.

10:18.900 --> 10:21.860
And save it to our bids variable.

10:22.220 --> 10:27.300
Well we would probably need some form of a loop right.

10:27.780 --> 10:32.060
So let's go ahead and use our friend the while loop.

10:32.540 --> 10:35.140
Now what is the variable that we'll check.

10:35.140 --> 10:43.520
Let's say that we had a variable called continue Bidding and we set it to equal true.

10:43.560 --> 10:47.840
To start off while continue bidding is true.

10:48.280 --> 10:56.800
Then we are going to keep asking the user for their name and price, and then we're also going to keep

10:56.800 --> 11:02.280
saving their bids into this variable called bids.

11:03.280 --> 11:12.640
And then finally we're also going to ask them what is their choice whether if there are any more people

11:12.640 --> 11:15.440
who are going to enter this auction.

11:15.880 --> 11:19.720
So we can now delete all these bits of repeated code.

11:20.200 --> 11:27.960
And if you think about this dictionary, we need it to be outside of the while loop so that it accumulates

11:27.960 --> 11:31.920
and it doesn't reset to zero every time the while loop runs.

11:32.360 --> 11:39.250
If we had this inside the while loop, then every single time we repeat the loop we're going to lose

11:39.250 --> 11:43.090
all of our previous data, so let's keep it outside.

11:44.450 --> 11:51.970
Now finally, we need to compare the bids in the dictionary once the user is done.

11:52.290 --> 11:57.210
So once the user is done, then we would get a no.

11:57.650 --> 12:02.050
And if the user was not done, we would get a yes.

12:02.490 --> 12:11.170
Now because this is going to be user input, yes or no, it's usually a good idea to add dot lower onto

12:11.170 --> 12:17.730
this so that when we're comparing it using our if statement, we know that it's always going to be lowercase.

12:18.330 --> 12:23.690
So if the should continue input is equal to no.

12:24.010 --> 12:28.770
Well in that case we need to compare the bids in our dictionary.

12:29.130 --> 12:36.370
Now, since this comparison in our dictionary is kind of like a separate piece of functionality, it's

12:36.500 --> 12:40.340
probably a good idea to create a separate function for it.

12:40.580 --> 12:48.220
That way we can reuse it, and we can also modularize our code so that it only does that function,

12:48.220 --> 12:53.100
and we can keep it outside of the main body of our script.

12:53.700 --> 12:59.500
So let's go ahead and create a function which I'll call find highest bidder.

13:00.420 --> 13:06.260
And this is going to take an input in the sense of a bidding dictionary.

13:07.140 --> 13:09.700
Now this is just a parameter.

13:09.700 --> 13:12.340
We can pass anything we want in here.

13:12.500 --> 13:17.180
But we know that what we're going to pass is actually this dictionary of bids.

13:17.780 --> 13:24.620
So what we want to do is we want to loop through our dictionary bidding dictionary.

13:25.140 --> 13:29.380
And we're going to look for each bit.

13:29.500 --> 13:33.580
So for bid in bidding dictionary would be nice.

13:33.580 --> 13:41.040
But as you might remember, when we loop through dictionaries, what happens is we actually loop through

13:41.080 --> 13:42.400
each of the keys.

13:42.880 --> 13:49.440
So if our dictionary is structured like this and we say for something in bidding dictionary, we're

13:49.440 --> 13:52.160
going to be getting hold of each of the names.

13:52.360 --> 13:57.040
So let's say for bidder in bidding dictionary.

13:57.040 --> 14:00.840
That way we're not confused as to what this value will be.

14:01.360 --> 14:10.280
Then we can set their bid amount to be equal to the bidding dictionary.

14:11.360 --> 14:16.080
And then using the square brackets to tap into that key the current bidder.

14:16.360 --> 14:22.760
And this is how we get hold of the value in a dictionary when we're looping through it.

14:22.760 --> 14:25.920
And this is something we covered in previous lessons as well.

14:26.800 --> 14:34.530
So now we need to figure out what is the largest number in terms of this bid amount when we loop through

14:34.730 --> 14:36.250
the bidding dictionary.

14:36.570 --> 14:38.690
Now, there are several ways of doing this.

14:38.730 --> 14:48.490
And the most, uh, easy to logic through way is to simply set something called a highest bid.

14:49.090 --> 14:51.290
And we can set it to equal zero.

14:51.650 --> 14:58.010
And then every time we loop through a new bidder and we get hold of that bid amount, we can check to

14:58.050 --> 15:04.490
see if the bid amount is greater than the current highest bid.

15:05.010 --> 15:12.610
And in that case, then the highest bid is going to be set to the bid amount.

15:13.570 --> 15:20.050
So now we can set a winner so we know who this person is, right?

15:20.250 --> 15:29.090
If at this point of the loop our highest bid is equal to the bid amount, then at the same time the

15:29.210 --> 15:33.270
winner is going to be the person who has that highest amount.

15:33.550 --> 15:38.950
So we can also reset the winner to the current bidder.

15:41.790 --> 15:44.390
Now outside of the for loop.

15:44.390 --> 15:51.870
So once all of the loop has completed going through each of the entries in our dictionary, we've gone

15:51.870 --> 15:53.750
through everybody's bids.

15:54.110 --> 15:57.070
Then we can go ahead and print the winner.

15:57.070 --> 16:00.950
So let's create an F string and say the winner is.

16:01.110 --> 16:04.670
And then we can insert this winner variable.

16:05.110 --> 16:08.390
And we can say with a bid of.

16:08.630 --> 16:14.430
And then our dollar sign and the highest bid can go in here as well.

16:15.030 --> 16:19.550
So that completes our finding highest bidder function.

16:19.950 --> 16:26.910
And the thing that I said earlier where I said there's many ways of doing this, you can also actually

16:26.910 --> 16:29.720
just simply use the max function.

16:29.720 --> 16:39.000
Now Python has a max function that can figure out what is the maximum value of all of the values in

16:39.000 --> 16:40.000
a dictionary.

16:40.440 --> 16:46.360
So I've got my Python console open here just to build a separate dictionary.

16:46.680 --> 16:51.000
So here I've got a dictionary representing the fruits that are in my house.

16:51.200 --> 16:54.400
And I want to know which fruit I have the most of.

16:54.840 --> 17:03.120
So what I can do is I can use the max function, pass in my dictionary fruit, and then after a comma,

17:03.120 --> 17:10.600
I could set the key to equal the fruits dot get.

17:11.120 --> 17:17.040
And what this line of code is going to do is it's going to look through the values of all of the items

17:17.040 --> 17:22.480
in my dictionary, compare them against each other, do a whole bunch of number crunching, and then

17:22.480 --> 17:27.200
give me the key that corresponds to the maximum value.

17:27.570 --> 17:32.930
Now, if you did a bit of googling and went on to StackOverflow because you thought to yourself, surely

17:32.930 --> 17:36.650
Python must be able to figure this out without me having to work it out?

17:36.650 --> 17:38.210
Then you are right.

17:38.530 --> 17:44.890
But I think given that most people are learning Python from scratch and they don't have any knowledge

17:44.890 --> 17:54.690
of other programming languages, this is super mysterious, and I recommend to actually always try to

17:54.730 --> 18:02.170
go from first principles and just simply work it out, because the code is not that complicated and

18:02.170 --> 18:07.490
down the line, this will be a lot easier to figure out than some code you got off the internet.

18:07.570 --> 18:09.810
And besides, we're learning at the moment, right?

18:09.970 --> 18:12.450
So it's always good to write more code.

18:13.050 --> 18:17.250
So we've got our dictionary comparison function.

18:17.250 --> 18:20.210
We just need to use it in our while loop.

18:20.690 --> 18:23.570
So if should continue is equal to no.

18:23.610 --> 18:25.950
That means everybody's done with their that bids.

18:26.190 --> 18:35.590
And this dictionary is now full, so we can now pass that dictionary over to the find highest bidder

18:35.590 --> 18:36.350
function.

18:36.750 --> 18:41.510
So we can firstly set continue bidding to false.

18:41.510 --> 18:43.270
So we exit our loop.

18:43.710 --> 18:52.150
And then we set our find highest bidder to work on our dictionary of bids.

18:52.590 --> 18:56.110
Now you'll notice that at the moment I have this red underline.

18:56.110 --> 19:00.390
And I'm not getting any autosuggest for my function down here.

19:00.670 --> 19:07.150
And that's because we need to declare this function or create this function before we use it.

19:07.150 --> 19:12.750
So if I move it all the way up there, then we now have a working bit of code.

19:13.390 --> 19:20.350
So finally the only other thing we need to do is to check if they typed in.

19:20.390 --> 19:20.830
Yes.

19:20.830 --> 19:27.720
So if should continue is equal to yes that means they want to go back to the beginning of the loop and

19:27.720 --> 19:29.760
enter the next person's name.

19:30.160 --> 19:36.920
But remember that what we want to do is to clear the screen when that happens.

19:37.120 --> 19:44.960
So if I type yes, I don't want the previous person to see the bid because it's a blind auction, right.

19:45.000 --> 19:51.040
So as I mentioned previously, in order to clear the screen, we're simply just going to print out the

19:51.040 --> 19:52.320
new line character.

19:52.320 --> 19:57.960
So backslash n and we can print it out 20 times or 100 times or however many times you want.

19:58.400 --> 20:02.800
And then it's going to loop back to the beginning of the while loop asking the next person.

20:03.240 --> 20:05.000
So let's try this out.

20:05.160 --> 20:13.120
Let's say that we have one person called Angela who wants to bid $12 for this half eaten apple.

20:13.760 --> 20:22.260
And then we have another bidder who is called John, who wants to bid only $3.

20:22.700 --> 20:28.700
And finally we have Kate, who only wants to pay $1.

20:29.140 --> 20:32.860
So now if we go ahead and say no, there are no more bidders.

20:32.860 --> 20:38.180
It tells us that the winner is Angela with a bid of $12.

20:38.460 --> 20:45.100
And now all we need to do is to put in the final finishing touch, which is to import and print our

20:45.100 --> 20:45.700
logo.

20:46.100 --> 20:52.380
So import art and then we'll go ahead and print art dot logo.

20:52.820 --> 20:56.620
And that will complete this project.

20:56.980 --> 21:02.020
So as always you can go into the solution dot pi to view my solution.

21:02.260 --> 21:05.460
You can improve the project to your desire.

21:05.660 --> 21:12.180
And you can also go back and review some of your code to make sure that everything works as expected.

21:13.180 --> 21:18.100
Now, tomorrow will be a new day and we'll be learning some more Python.

21:18.260 --> 21:20.540
So get some rest and I'll see you soon.
