100 Days of Code: Day 9

toyinariyo

Oluwatoyin Ariyo

Posted on January 23, 2023

100 Days of Code: Day 9

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()
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

I will continue with Day 10 in the middle of the week.

💖 💪 🙅 🚩
toyinariyo
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.

Related

100 Days of Code Day 11
100daysofcode 100 Days of Code Day 11

February 10, 2023

100 Days of Code: Day 9
100daysofcode 100 Days of Code: Day 9

January 23, 2023

100 Days of Code: Day 8
100daysofcode 100 Days of Code: Day 8

January 21, 2023

100 Days of Code Challenge: Day 4
100daysofcode 100 Days of Code Challenge: Day 4

January 10, 2023

100 Days of Code Challenge: Day 3
100daysofcode 100 Days of Code Challenge: Day 3

January 6, 2023