Day 9 - Dictionary


Posted by pei_______ on 2022-04-19

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


{Key: Value}

(1) Retreiving items from dic
print(dic["Key"]) = "value"

(2) Add new items to dic
dic["new Key"] = "value"

(3) Creat an empty dic
empty_dic = { }

(4) Wipe an existing dec
dic = { }

(5) Edit an item in a dic
dic["Key"] = "value"

(6) Loop through a duc
for things in dic:
print(things) => key
print(dic[things]) => value


(7) Nesting list/dictionary in dictionary
nest_dictionary = {
Key: [list]
Keys: {dictionary}
}

(8)Nesting dictoinary in list
nest_list = [
{
Key1: value
Key2: value
}
{
Key3: value
Key4: value
}


# Grading Program

student_scores = {
  "Harry": 81,
  "Ron": 78,
  "Hermione": 99, 
  "Draco": 74,
  "Neville": 62,
}

for student in student_scores:
    if student_scores[student] <= 70:
        student_grades[student] = "Fail"
    elif student_scores[student] <= 80:
        student_grades[student] = "Acceptable"
    elif student_scores[student] <= 90:
        student_grades[student] = "Exceeds Expectations"
    else:
        student_grades[student] = "Outstanding"

print(student_grades)

#  Dictionary in List

travel_log = [
{
  "country": "France",
  "visits": 12,
  "cities": ["Paris", "Lille", "Dijon"]
}]

def add_new_country(name_country,num_visits,list_cities):
    new_country = {
        "country":name_country,
        "visits":num_visits,
        "cities":list_cities,
    }
    travel_log.append(new_country)

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

# blind auction

from replit import clear

keep_loading = True
bidding_info = {}
while keep_loading:
  name = input("What is your name?: ")
  bid = int(input("What is your bid? $"))
  bidding_info[name] = bid

  result = input("Are there any other bidders? Type \'yes or \'no.")
  if result == "no":
    keep_loading = False
  else:
    clear()

highest_bid = -1
winner = "no_one"
compete = True
while compete:
  for bidder in bidding_info:
    if bidding_info[bidder] == highest_bid and winner == bidder:
      compete = False
    if bidding_info[bidder] > highest_bid:
      highest_bid = bidding_info[bidder]
      winner = bidder

print(f"The winner is {winner} with a bid of ${highest_bid}.")

#Python #課堂筆記 #100 Days of Code







Related Posts

各種在瀏覽器上發送 request 的方法

各種在瀏覽器上發送 request 的方法

[React 02] Component 、 JSX、事件機制

[React 02] Component 、 JSX、事件機制

Array 傳入值總是被覆蓋

Array 傳入值總是被覆蓋


Comments