Python 100 projects in 100 days — Learning Journal

Paul Zhao
Paul Zhao Projects
Published in
6 min readNov 17, 2020

--

Day nine— Secret Auction plus a bonus Nested Listings and Libraries project

Just your peace of mind, I would include installation for Python before we get started with our project

Here are a couple of ways you may put python in use.

1 Repl.it — Free registration and a bunch of features provided

2 Install Python locally (using Mac)

Installing Homebrew

Open terminal and type in

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
.
.
.
==> Installation successful!==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
https://docs.brew.sh/Analytics
No analytics data has been sent yet (or will be during this `install` run).==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
https://github.com/Homebrew/brew#donations==> Next steps:
- Run `brew help` to get started
- Further documentation:
https://docs.brew.sh

Verify Homebrew installation

$ brew --version
Homebrew 2.4.16
Homebrew/homebrew-core (git revision 23bea; last commit 2020-09-04)
Homebrew/homebrew-cask (git revision 5beb1; last commit 2020-09-05)

Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

2. Python (the version must be 3.7 to make sure that all functions will be available to present intended outcome) is required to be installed

Installing Python3

Now, we can install Python 3:

$ brew install python
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
==> Updated Casks
sessionWarning: python@3.8 3.8.5 is already installed and up-to-date
### my python was preinstalled, you may see different installation process. And it may take a while before python is fully installed

Verify is your python3 is installed

$ python3 --version
Python 3.8.5

Notes: you may set your default python as latest version by applying following code

$ unlink /usr/local/bin/python
$ ln -s /usr/local/bin/python3.8 /usr/local/bin/python

If using windows, please follow instruction below to install Chocolatey, then install Python using it

1 Install Chocolatey

2 Install Python

Let’s get started with our project now!

Instructions:

Flow Chart

from replit import clearfrom art import logoprint(logo)bids = {}bidding_finished = Falsedef find_highest_bidder(bidding_record):  highest_bid = 0  winner = ""# bidding_record = {"Angela": 123, "James", "321"}  for bidder in bidding_record:    bid_amount = bidding_record[bidder]    if bid_amount > highest_bid:      highest_bid = bid_amount      winner = bidder   print(f"The winner is {winner} with a bid of ${highest_bid}")while not bidding_finished:  name = input("What is your name?: ")  price = int(input("What is your bid?: $"))  bids[name] = price  should_continue = input("Are there any other bidders? Type 'yes or 'no'.\n")  if should_continue == "no":    bidding_finished = True    find_highest_bidder(bids)  elif should_continue == "yes":    clear()

Output:

___________
\ /
)_______(
|"""""""|_.-._,.---------.,_.-._
| | | | | | ''-.
| |_| |_ _| |_..-'
|_______| '-' `'---------'` '-'
)"""""""(
/_________\
.-------------.
/_______________\
What is your name?: Paul Zhao
What is your bid?: $300
Are there any other bidders? Type 'yes or 'no'.
yes

After clearing

What is your name?: Cindy Cain
What is your bid?: $220
Are there any other bidders? Type 'yes or 'no'.
no
The winner is Paul Zhao with a bid of $300

Notes:

1 To clear the screen, we may import function shown below and call it out to apply

from replit import clear
clear()

2 To print logo, we may import a value named logo from a file named art.py

from art import logoprint(logo)

3 We then create an empty library named bids . Here we also set up a value named bidding_finished as False, which is being used with while loop. When bidding_finished is set to True, then the while loop ends. Otherwise, it continues. Also, we define a new function named find_highest_bidder(bidding_record) . Then set up value highest_bid as 0 and winner as an empty string

bids = {}bidding_finished = Falsedef find_highest_bidder(bidding_record):  highest_bid = 0  winner = ""

4 Here we come to the real meats — how we are able to use for loop to go through our bidding_record and find out the highest_bid and corresponding winner . Firstly, we keep a close eye on our bidder , which is known as a key our library. Then the bid_amount given is based on bidder provided, which is also known as value. For us to obtain highest_bid among all bids , we need to apply if statements. When a new bid is in, we compare it to highest_bid so far. If it is bigger than the highest_bid now, we will assign this bid_amount to highest_bid, which makes it directly a brand new highest_bid. Then the winner is the corresponding bidder.

for bidder in bidding_record:  bid_amount = bidding_record[bidder]  if bid_amount > highest_bid:    highest_bid = bid_amount    winner = bidder  print(f"The winner is {winner} with a bid of ${highest_bid}")

5 Now the rest of the coding is all in our while loop, which we’ve introduced as a way to end our loop when no more bidders. As shown below, while loop will go continue as long as not bidding_finished is still in play. With that said, we have both name and price set up as inputs . Here, bids[name] = price is the library featuring bids . When we recall our coding earlier

for bidder in bidding_record:
bid_amount = bidding_record[bidder]

then bidder is the key, which we call as name and bid_amount is the value, which we call as price

Now, let us move on to discuss how we end our while loop. As we set up our bidding_finished as False, we now set it to True when no bidders may participate. So the while loop is ended and prints out designated message. If more bidders in loop, then we will trigger clear() function to reset our project from scratch

highest_bid = 0winner = ""while not bidding_finished:  name = input("What is your name?: ")  price = int(input("What is your bid?: $"))  bids[name] = price  should_continue = input("Are there any other bidders? Type 'yes or 'no'.\n")  if should_continue == "no":    bidding_finished = True    find_highest_bidder(bids)  elif should_continue == "yes":    clear()

As we complete our main project, we now may have our desert for the day — nested listing and libraries.

Instructions:

You are going to write a program that adds to a travel_log. You can see a travel_log which is a List that contains 2 Dictionaries.

Write a function that will work with the following line of code on line 21 to add the entry for Russia to the travel_log.

add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])

You've visited Russia 2 times.

You've been to Moscow and Saint Petersburg.

DO NOT modify the travel_log directly. You need to create a function that modifies it.

travel_log = [{"country": "France","visits": 12,"cities": ["Paris", "Lille", "Dijon"]},{"country": "Germany","visits": 5,"cities": ["Berlin", "Hamburg", "Stuttgart"]},]#🚨 Do NOT change the code above#TODO: Write the function that will allow new countries#to be added to the travel_log. 👇def add_new_country(country_visited, times_visited, cities_visited):  new_country = {}  new_country["country"] = country_visited  new_country["visits"] = times_visited  new_country["cities"] = cities_visited  travel_log.append(new_country)#🚨 Do not change the code belowadd_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])print(travel_log)

Output:

[{'country': 'France', 'visits': 12, 'cities': ['Paris', 'Lille', 'Dijon']}, {'country': 'Germany', 'visits': 5, 'cities': ['Berlin', 'Hamburg', 'Stuttgart']}, {'country': 'Russia', 'visits': 2, 'cities': ['Moscow', 'Saint Petersburg']}]

Notes:

1 Here we define a new function named add_new_country with 3 different values country_visited, times_visited, cities_visited. Then we create a brand new library named new_country . We assign these 3 new values with 3 existing values from travel_log list. Finally, we apply .append function to add our library into the list

def add_new_country(country_visited, times_visited, cities_visited):new_country = {}new_country["country"] = country_visitednew_country["visits"] = times_visitednew_country["cities"] = cities_visitedtravel_log.append(new_country)

2 This project is a good example for how to add a library into a list, which can be very helpful when adding a library or a list into a library or a list, which is called nested libraries or lists

That’s all for today. Let us now take a breathe and reflect our achievements so far. We’ll build up more on what we’ve acquired as we move forward!

--

--

Paul Zhao
Paul Zhao Projects

Amazon Web Service Certified Solutions Architect Professional & Devops Engineer