All right, so how did you get on with today's project?
Hopefully it went really well and you're just here to see how I did it.
As I mentioned before, it's really useful when you're trying to create a large project to break it
down into smaller pieces so that you can tackle each of the pieces one by one.
And a great way of doing that is by creating your own flowchart.
So as I've shown you before in the end of the last lesson, this is the flowchart that I've created
for this particular program, and I'm going to follow it step by step to create this final blind auction
program.
The first thing we're going to do is we're going to show the logo from this file art.py, and notice
how this logo is stored inside a variable called logo.
The first thing we have to do is to fork our own copy of the starting project, and then we're going
to look inside the art.py module and then we're going to import that logo.
So now I can go ahead and print my logo.
So that's the first step done. Next we're going to ask for a name.
So that's going to be in the form of an input.
So let's go ahead and create a variable called name and we're going to set it to be an input.
And the input is going to say, what is your name?
So they're going to type in their name and that's going to be stored inside this variable.
Now, the next thing we're going to do is ask them for their bidding price. We'll say that inside a variable
called price and we'll create another input to ask them what is your bid?
And at the end of this, I'm going to add a dollar sign just so that when they are typing their bids,
they'll see this line.
And when they start typing, the cursor should be about here so they can see the dollar symbol before
their price.
So now we're down to here and we have to add our name and our bidding price into a dictionary.
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.
And then later on, once we've got the name and the price, we said that the name was going to be the
key and the price was going to be the corresponding value. To add a new entry to our bids dictionary,
we specify the dictionary, we add a set of square brackets, and then we add the key.
The key is going to be the name and the value is going to be the price.
So we would write it like this.
Now, we get down to the slightly trickier part. We're going to ask if there are any other users who
want to bid. And if the answer is yes, then we're going to clear the screen and we're going to loop
back to where we ask for the next name and the next bid price.
How do we achieve this?
Well, we can put this into a while loop. Let's go ahead and indent this.
So I just typed command and then the right-sided square bracket. Or on Windows, it will be control and the right
side of the square bracket.
And then we can wrap this into a while loop.
If we had some sort of variable that would keep track whether if the bidding is finished.
So let's call it bidding_finished.
And of course, it starts out being False.
Now, while bidding is not finished, so while not bidding_finished, so basically while this is still
false, then we're going to keep running this until forever.
But of course, we need a way to stop this.
So we're going to create a new input where we ask the user, "are there any other bidders?"
Type "yes" or
"no". This way we get to get an input which is going to be either the word yes or the word no, and
we can save this inside a variable called maybe should_continue.
So this way we can check this variable and we can see, well, if should_continue is equal to "no",
well, in that case, that's when we're going to flip the switch and change this bidding_finished variable
to true.
So this means that when we get to the end of this block in the while loop, then when it checks this
bidding_finished, it's not going to continue to loop anymore.
Now, on the other hand, if should_continue was yes or in fact any other value, then we can keep continuing
and we don't actually have to change this variable because it's already false.
Now we have our code looping, which means that unless the user types in a 'no' at the very end and bidding
becomes finished, this is this path.
Otherwise then we're going to keep going through all of these lines of code which is basically all
of this until we get to the point where this switch actually gets flipped.
So if you notice that if the user actually answers yes, then we want to clear the screen before we
repeat the loop.
That means we can check to see
well, elif should continue is equal to.
yes,
well, in this case, we're going to call that clear() function that I showed you in the hint at the very
beginning.
So this function comes from the replit module and it allows us to clear the output in the console so
that the previous name and bid gets cleared on off the screen and it leaves a fresh start for the next
user.
The last thing we have to do is to actually address this right? So when they answered no, they get taken
out of the while loop, but we still have to find the highest bidder from our dictionary and to declare
them as the winner.
How do we do this?
Well, let's put this in a separate function. So we could create a new function called find_highest_bidder.
And this is going to get the bidding_record as the inputs, so this will be the dictionary that represents
all the bids, the names and the values. I think it's easier if we can visualize what this bidding_
record might look like.
So we know it's a dictionary of key-value pairs where each key is a name and each value is a number.
So let's say we had only two bidders, Angela and James.
Angela bid $123.
James bid $321.
So we can see who's clearly going to be the winner here.
But how do we get our code to figure that out?
We need to somehow loop through this dictionary, right? which is called bidding_record.
We know that we can use a for loop to do this so we can say for each bidder in the bidding_record,
and this is a good point to remember that when you use a for loop on a dictionary, instead of looping
through each item in the dictionary, like each of these key-value pairs, it actually loops through
each of the keys.
And we covered this in the very first lesson of today where we talked about how the for loop works
when we're looping through a dictionary. Now, we're going to get hold of each of the bidders inside
this dictionary.
So what do we do with it?
Well, we want to use the key to get the value.
Let's take the bidding_record and pass in the key,
and this should give us the value.
So now we can actually save this inside a variable which I'm going to call bid_amount.
So the first time this for loop runs, the bid_amount is going to equal 123
and the next time the for loop runs, the bid amount is going to be equal to 321. If we had some sort
of way outside of the for loop
that keeps track of the highest bid, well then we could use this number to check against the current
bidding amount.
We can use an if statement to check
well, if the bid amount is greater than the highest bid, well, in that case, then the highest bid
becomes the bid amount.
So what this means is that we start out with a zero. The first time the for loop is run, the bidder
is going to be equal to Angela,
the bid amount is going to be equal to Angela as the key passed into this dictionary, which is going
to get us the value 123. If 123 is greater than zero,
well, then this highest bid is now going to become 123. The next time the for loop runs
the bidder is now James and the bid amount is now 321.
Now we're checking if 321 is greater than the highest bid.
Remember this, in the last round, got set to 123.
So this is going to be true.
And this means James' bid, 321, is now going to be the highest bid.
In addition to keeping track of the highest bid, we can keep track of the winner. If this is indeed
true, if the current bidder has the highest bid, well then at the same time as setting the highest
bid,
we should also set the winner to the key,
so the current bidder.
And once we've gone through this for loop and we've looped through all of the records in the bidding record,
well then finally we can actually print
"The winner is..."
the winner that's stored in this variable. And we're going to say they won with a bid of... and let's add
a dollar sign and then we're going to pass in the highest_bid.
Now, we can turn this into an fstring and it will use both of these variables, which is storying
the highest bid and the winner's name to print out who the winner is.
So down here, when should_continue is no going down this path,
we can now print and declare the winner. In addition to saying that bidding finished is true,
we're also going to call that function, find_highest_bidder, and pass in all of the bids that we
have been keeping track of over here.
Now, just before we run our code, let's go through each line and make sure that everything looks all
right.
Firstly here, where it says, 'what is your name?'
maybe we can add a colon and then a space
so the user starts typing here.
Then it asks, 'what is your bid?'
We can add a colon as well and the user will start typing after the dollar sign.
Now, I can already see a problem that's actually going to happen because of this line 23.
I wonder if you can spot the issue here, especially if you think about what we're going to do with
this price later on.
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
happens.
So now let's go ahead and run the code.
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
then 123. And then one other bidder who's going to be James, 321.
And now if I say no, there are no more bidders,
see what happens when I hit enter.
I get a type error
and it tells us that the greater sign is not supported between instances of a string and an int
and this happened on line 16.
If we take a look at line 16, basically we're comparing the bid amount, which is the value that came
out of the bidding record, which is what's being passed in here,
and that, of course, is a dictionary where the keys are the names of the bidders and the value is
the price.
So what's actually happening here?
Well, it's because when I took this price input in, I kept it as a normal input.
Instead, it should be turned into an integer.
So that way we can work with it as a number so that when we're comparing it against, say, zero or
whatever other number later on, it can actually do the comparison.
It doesn't actually make sense to the computer when you're comparing a string versus a number. That doesn't
actually work.
And this was covered when we talked about data types in Python.
Now, if we hit run again
and we do the same thing, and when I say no, you can see that it tells me the winner is James with
a bid of 321.
Essentially, the hardest part of this challenge is figuring out how to loop through a dictionary and
get hold of each of the bidders and each of their values and compare them historically against previous
values inside this dictionary.
If this function is confusing to you, then I really recommend either going over to Thonny, pasting
the code in and exploring it by going through it step by step.
Alternatively, if you can't install Thonny or you don't have access to your normal computer, then you
can also go to a website called pythontutor.com.
And this also lets you visualize your code, although in a slightly more simplified way. If I go ahead
and paste in my function, find_highest_bidder, passing in the bid record, let's go ahead and create
a new dictionary of bids.
So now we have a dictionary of bids with two key-value pairs just as we entered before,
and then I'm going to call this function, find_highest_bidder, and pass in these bids.
Now, if we go ahead and click visualize execution, you can see it's going to go line by line, and we
can click next to see how it's jumping through this.
Firstly, it's bypassing this function, creating this next dictionary.
So now it's got a function stored somewhere, which is this one,
it's got a dictionary called bids stored somewhere else.
And now we actually get to the line, which is going to execute that function and it's going to pass
over this dictionary of bids to this function.
And if you step through this line by line, you can see it creating these new variables and populating
them. The first time we run it
the highest bidder is zero, the winner is an empty string, and the bidder in the for loop is now Angela.
It's the first key from the dictionary as it points to here.
Now if we keep clicking
next, it works out the bid amount which is the value corresponding to that key, and then it works
out the highest bid by comparing whether if the current bid amount is greater than the current highest
bid.
So 123 is greater than zero
so when I click next, you'll see highest bid gets replaced with the bid amount and the winner is now
going to be set as Angela. The next time the loop runs the bidder is
now James, and if I keep going through this, you can see that James actually ends up with the highest
bid and becomes the winner.
So now the for loop is done because it's looped through all items in the dictionary and it's now going
to print the output, which is 'The winner is James with a bid of $321.'
This is another way that's really, really helpful to visualize your code.
And the other great thing is that even if while you're visualizing there's still something you don't
understand, you can actually click on this button to get live help from users across the Internet.
And if you want to practice your own debugging, just as we did in today's lesson, you can also click
to help some of these users who are looking for help with their code.
And you can practice reading code, understanding code and helping other people with their bugs and
their issues.
Finally, if you want to take a look at the completed code for today's project, then as always, you
can head over tor repl.it/@appbrewery/blind-auction-completed.
And it's important to remember that this is just one way of completing this project.
There are lots and lots of other ways. But as long as you test your program thoroughly and it works
in the same way, then it doesn't matter too much how you've done it.
But it's important that you've absorbed the knowledge in today's lessons, and the most important of
which is this concept of dictionaries, adding to dictionaries, and also looping through dictionaries.
So have a look through the completed code and also have a play around with Python Tutor, which is
a really great resource for your learning.
And I'll see you tomorrow for more lessons on Python.