100 Days of Code: Day 9
Oluwatoyin Ariyo
Posted on January 23, 2023
Day 9 of Angela Yu's Python bootcamp was about dictionaries and how they can used in lists and even inside another dictionary (nested dictionaries). This led to building a program where the user enters their name and how much money they are going to bid for an auction. The user can then either choose to allow more bidders or to be the only bidder, which would result in them winning the bid. Here is the Python code for that (done in Pycharm instead of Replit as that is where I feel more comfortable coding with Python):
import os
from art import logo
def cls(): # Cross-platform clear screen
os.system('cls' if os.name == 'nt' else 'clear')
print(logo)
bids = {} # Create an empty dictionary for the bids
bidding_finished = False # Create a boolean for when the bidding has finished
def find_highest_bidder(bidding_record): # Argument for the bids empty dictionary
highest_bid = 0
winner = ""
for bidder in bidding_record:
bid_amount = bidding_record[bidder]
if bid_amount > highest_bid:
highest_bid = bid_amount
winner = bidder
print(f"Going once! Going twice! Sold to {winner} with a bid of £{highest_bid}")
while not bidding_finished:
name = input("What is your name?\n")
price = int(input("How much are you bidding? £"))
bids[name] = price
continue_bidding = input("Are there any other bidders? Type 'yes' or 'no'.\n")
if continue_bidding == "no":
bidding_finished = True
find_highest_bidder(bids)
elif continue_bidding == "yes":
cls()
I of course also converted it to C#, which can be seen here:
Dictionary<string, int> bids = new Dictionary<string, int>(); // Create an empty dictionary for the bids
bool bidding_finished = false; // Create a boolean for when the bidding has finished
void find_highest_bidder(Dictionary<string, int> bidding_record) // Argument for the bids empty dictionary
{
int highest_bid = 0;
string winner = "";
foreach (var bidder in bidding_record)
{
int bid_amount = bidder.Value;
if (bid_amount > highest_bid)
{
highest_bid = bid_amount;
winner = bidder.Key;
}
}
Console.WriteLine("Going once! Going twice! Sold to {0} with a bid of £{1}", winner, highest_bid);
}
while (!bidding_finished)
{
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("How much are you bidding? £");
int price = int.Parse(Console.ReadLine());
bids[name] = price;
Console.WriteLine("Are there any other bidders? Type 'yes' or 'no'.");
string continue_bidding = Console.ReadLine();
if (continue_bidding == "no")
{
bidding_finished = true;
find_highest_bidder(bids);
}
else if (continue_bidding == "yes")
{
Console.WriteLine("\n" + 20);
}
}
I will continue with Day 10 in the middle of the week.
💖 💪 🙅 🚩
Oluwatoyin Ariyo
Posted on January 23, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.