All right.
So now that you've learned all about dictionaries, we're going to put all of that knowledge to test
and to use by building a blind auction program.
So here's the program.
When you first run it, it's just going to show you the logo art and maybe a line that tells you what
program you're in.
And then it's going to ask you for your name.
So let's say we have three bidders, right.
So first go I go to the computer, I type in my name, I type in my bid and I hit enter.
It asks me if there are any other bidders.
I look around the room.
I see two other colleagues, so I type.
Yes.
And now it clears the entire screen so that other people can't see what my bid was.
So now I leave the computer and my colleague comes over.
They type in their name and also their bid.
And again they look around the room and they see there's one other person who wants to bid.
So they hit yes.
again.
It clears their bid.
And finally, the last person makes their bid.
At this point, everybody has bid.
So she's going to type no.
And it's at this moment when our program is going to work out who has made the highest bid and print
out who the winner is and what their bid was.
This way, instead of competing with each other on bids, we actually all just give the highest price
we're willing to pay.
And this is known as quite an efficient way of making a bid.
And you see that a lot in, say, the construction industry and government, etc..
Now, one of the most complex parts of actually trying to build this program is how do you clear the
screen in between bids?
Now, what I want you to do in your version of the code is I simply want you to print out a new line
character.
So if we print a string and the string happens to be just the backslash n.
So just a new line.
And then we can do this thing where we multiply this string by say 20 times or 100 times.
And what this means is it's basically going to add 100 new lines.
So if I go ahead and print hello and then I go ahead and have an input, um, type something.
Now if I run this code, you'll see that hello was printed.
And then immediately the next thing I did was to basically clear the screen.
And I did that by typing backslash n 100 times.
And this programmatically will scroll to this part.
And now I can use this input.
And the user won't be able to see the previous part unless they know how to scroll up.
In addition, we've also got the starting art of the gavel logo, which you're going to put into the
program.
And the most important thing of all is that I want you to create a dictionary so that each person's
name will be the key and their bid will be the value.
And this dictionary is going to contain many of these key value pairs.
And at the very end of the program, when everybody's done with bidding, then we're going to loop through
that dictionary and figure out who has made the highest bid.
If you want, this is a good time to analyze this completed project and see if you can create your own
flow chart using something like Draw.io that I've shown you before.
Or just take a piece of paper and a pen and draw out your flow chart.
Now if you want a hint, then you can head over to the course resources and you'll get taken to the
flow chart that I've created for this program.
So spoiler alert I'm about to show you that flowchart.
So if you don't want to see it and you want to create your own because it's a relatively simple program,
then pause the video now and complete the challenge by yourself.
Now, for everybody else who's still here, I want to show you this is a flowchart I've created to represent
this program.
So we start at the beginning.
We first show the logo from that file, art dot Pi, and then we asked for a name input.
We asked for their price.
Now we're going to add the name and the bid into a dictionary, with the name as the key and the bid
as the value.
Now, once we're done with that, then we get to ask the user for another input and see if there are
any other users who want to bid.
So if they answer yes, then we're going to clear the screen and we're going to go back to here.
Ask for another name, another price and so on and so forth.
But if they say no, everybody's done with bidding, then we're going to call some sort of function
that we have, which is going to loop through our dictionary that was created here and find the person
who had the highest bid value and then declare them as the winner and print out their bid.
So it's a relatively simple program, at least compared to other things that you've built so far.
So this is the time to pause the video and give it a go.
All right.
Now I'm going to walk you through the solution, or at least my solution for the blind auction project.
So the first thing that I'd like to do when we have something that is a large project is to break it
down.
So you saw me draw a flowchart.
And the next step is I could translate that flowchart into several to do's in my code.
So the first thing I'll probably want to do is to ask the user for some inputs.
And the inputs that I need.
If you take a look at the demo is it asks for what is your name and then what is your bid.
And then it's got this dollar sign in front.
And then it asks, are there any other bidders type yes or no.
So we need a way of capturing these inputs right.
So that could be R to do number one.
Next we also need to save the data that the user gives us into a dictionary.
And that way we can later loop through the dictionary where we have a list of name and then price.
And we'll be able to figure out who is the name that had the highest price.
Now, the next thing we might want to do is to figure out whether, if the user wants to continue adding
new bids.
And this might involve some sort of a loop so that we can keep going back to ask the user for input.
But of course it will be a different user each time.
Now the final thing to do is to actually go ahead and compare all the different bids and then figure
out who has the highest bid in the dictionary.
So that's pretty much all there is to it.
And let's go ahead and tackle it one by one.
So firstly let's ask the user for some input.
So let's save their name into a variable called name.
And then we'll use an input to ask them, what is your name?
And then we'll add a colon, maybe a space so they can type their name here.
And then once they've typed their name we'll ask them for a price.
So again it's going to be an input.
And we're going to say what is your bid.
And now we're going to add a dollar sign or whatever currency you work with in front of their bid.
But remember that they're going to write a number in here.
And inputs are strings.
You can see that it's going to give you a string.
So in order for us to be able to save it into our dictionary as a number, I'm going to convert it to
an integer.
Now of course, if you want bids that have floating points decimal places, you could also convert this
into a float.
So now that we've got our number and our name, the next thing to do is to save that data into a dictionary.
So if we have a dictionary that we call, let's say we call it bids.
And we started out as an empty dictionary.
Then once we've gotten hold of the data from the user, the name and the price, we can then add to
this dictionary of bids.
So we can say bids.
And then the key is going to be their name.
And we can set it equal the value.
So if this is at all unfamiliar then you might need to go and review the lesson where we learnt about
dictionaries, how to modify, how to edit, how to add, how to create empty dictionaries.
But this is essentially the process to save the data into our dictionary in this format.
So now we need to ask the user whether if there are any new bids that need to be added.
So we could probably have a variable called should continue and we could set that to equal an input.
And our prompt could be.
Are there any other bidders.
And we can say type uh yes.
Um or no.
And then we could add a new line so that they type on the next line.
And now we have this variable should continue.
So how can we use this variable to go back to the beginning and ask for the new person's name price.
And save it to our bids variable.
Well we would probably need some form of a loop right.
So let's go ahead and use our friend the while loop.
Now what is the variable that we'll check.
Let's say that we had a variable called continue Bidding and we set it to equal true.
To start off while continue bidding is true.
Then we are going to keep asking the user for their name and price, and then we're also going to keep
saving their bids into this variable called bids.
And then finally we're also going to ask them what is their choice whether if there are any more people
who are going to enter this auction.
So we can now delete all these bits of repeated code.
And if you think about this dictionary, we need it to be outside of the while loop so that it accumulates
and it doesn't reset to zero every time the while loop runs.
If we had this inside the while loop, then every single time we repeat the loop we're going to lose
all of our previous data, so let's keep it outside.
Now finally, we need to compare the bids in the dictionary once the user is done.
So once the user is done, then we would get a no.
And if the user was not done, we would get a yes.
Now because this is going to be user input, yes or no, it's usually a good idea to add dot lower onto
this so that when we're comparing it using our if statement, we know that it's always going to be lowercase.
So if the should continue input is equal to no.
Well in that case we need to compare the bids in our dictionary.
Now, since this comparison in our dictionary is kind of like a separate piece of functionality, it's
probably a good idea to create a separate function for it.
That way we can reuse it, and we can also modularize our code so that it only does that function,
and we can keep it outside of the main body of our script.
So let's go ahead and create a function which I'll call find highest bidder.
And this is going to take an input in the sense of a bidding dictionary.
Now this is just a parameter.
We can pass anything we want in here.
But we know that what we're going to pass is actually this dictionary of bids.
So what we want to do is we want to loop through our dictionary bidding dictionary.
And we're going to look for each bit.
So for bid in bidding dictionary would be nice.
But as you might remember, when we loop through dictionaries, what happens is we actually loop through
each of the keys.
So if our dictionary is structured like this and we say for something in bidding dictionary, we're
going to be getting hold of each of the names.
So let's say for bidder in bidding dictionary.
That way we're not confused as to what this value will be.
Then we can set their bid amount to be equal to the bidding dictionary.
And then using the square brackets to tap into that key the current bidder.
And this is how we get hold of the value in a dictionary when we're looping through it.
And this is something we covered in previous lessons as well.
So now we need to figure out what is the largest number in terms of this bid amount when we loop through
the bidding dictionary.
Now, there are several ways of doing this.
And the most, uh, easy to logic through way is to simply set something called a highest bid.
And we can set it to equal zero.
And then every time we loop through a new bidder and we get hold of that bid amount, we can check to
see if the bid amount is greater than the current highest bid.
And in that case, then the highest bid is going to be set to the bid amount.
So now we can set a winner so we know who this person is, right?
If at this point of the loop our highest bid is equal to the bid amount, then at the same time the
winner is going to be the person who has that highest amount.
So we can also reset the winner to the current bidder.
Now outside of the for loop.
So once all of the loop has completed going through each of the entries in our dictionary, we've gone
through everybody's bids.
Then we can go ahead and print the winner.
So let's create an F string and say the winner is.
And then we can insert this winner variable.
And we can say with a bid of.
And then our dollar sign and the highest bid can go in here as well.
So that completes our finding highest bidder function.
And the thing that I said earlier where I said there's many ways of doing this, you can also actually
just simply use the max function.
Now Python has a max function that can figure out what is the maximum value of all of the values in
a dictionary.
So I've got my Python console open here just to build a separate dictionary.
So here I've got a dictionary representing the fruits that are in my house.
And I want to know which fruit I have the most of.
So what I can do is I can use the max function, pass in my dictionary fruit, and then after a comma,
I could set the key to equal the fruits dot get.
And what this line of code is going to do is it's going to look through the values of all of the items
in my dictionary, compare them against each other, do a whole bunch of number crunching, and then
give me the key that corresponds to the maximum value.
Now, if you did a bit of googling and went on to StackOverflow because you thought to yourself, surely
Python must be able to figure this out without me having to work it out?
Then you are right.
But I think given that most people are learning Python from scratch and they don't have any knowledge
of other programming languages, this is super mysterious, and I recommend to actually always try to
go from first principles and just simply work it out, because the code is not that complicated and
down the line, this will be a lot easier to figure out than some code you got off the internet.
And besides, we're learning at the moment, right?
So it's always good to write more code.
So we've got our dictionary comparison function.
We just need to use it in our while loop.
So if should continue is equal to no.
That means everybody's done with their that bids.
And this dictionary is now full, so we can now pass that dictionary over to the find highest bidder
function.
So we can firstly set continue bidding to false.
So we exit our loop.
And then we set our find highest bidder to work on our dictionary of bids.
Now you'll notice that at the moment I have this red underline.
And I'm not getting any autosuggest for my function down here.
And that's because we need to declare this function or create this function before we use it.
So if I move it all the way up there, then we now have a working bit of code.
So finally the only other thing we need to do is to check if they typed in.
Yes.
So if should continue is equal to yes that means they want to go back to the beginning of the loop and
enter the next person's name.
But remember that what we want to do is to clear the screen when that happens.
So if I type yes, I don't want the previous person to see the bid because it's a blind auction, right.
So as I mentioned previously, in order to clear the screen, we're simply just going to print out the
new line character.
So backslash n and we can print it out 20 times or 100 times or however many times you want.
And then it's going to loop back to the beginning of the while loop asking the next person.
So let's try this out.
Let's say that we have one person called Angela who wants to bid $12 for this half eaten apple.
And then we have another bidder who is called John, who wants to bid only $3.
And finally we have Kate, who only wants to pay $1.
So now if we go ahead and say no, there are no more bidders.
It tells us that the winner is Angela with a bid of $12.
And now all we need to do is to put in the final finishing touch, which is to import and print our
logo.
So import art and then we'll go ahead and print art dot logo.
And that will complete this project.
So as always you can go into the solution dot pi to view my solution.
You can improve the project to your desire.
And you can also go back and review some of your code to make sure that everything works as expected.
Now, tomorrow will be a new day and we'll be learning some more Python.
So get some rest and I'll see you soon.