Day 9 – Dictionaries

  • Day 9 Goals
  • The Python Dictionary
  • Coding Exercise: Grading Scores
  • Nesting List and Dictionaries
  • Project: The Secret Auction

Coding Exercise – Grading Scores.

# You have access to a database of student_scores in the format of a dictionary. 
# The keys in student_scores are the names of the students
# The values are their exam scores. 

# Write a program that converts their scores to grades.
# **DO NOT** modify lines 1-7 to change the existing student_scores dictionary. 

Write a program that converts their scores to grades.
student_scores = {
    'Harry': 88,
    'Ron': 78,
    'Hermione': 95,
    'Draco': 75,
    'Neville': 60
}

student_grades = {}

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

print(student_grades)

Project: Blind Auction

from art import logo

# TODO-1: Ask the user for input
# TODO-2: Save data into dictionary {name: price}
# TODO-3: Whether if new bids need to be added
# TODO-4: Compare bids in dictionary

# gather all the bids
all_bidders = {}
print(logo)
more_bidders = True
while more_bidders:
    name = input("What is your name?: ")
    bid = int(input("What is your bid?: $"))
    # add bids to a list
    all_bidders[name] = bid
    more_bidders = input("Are there any other bidders? Type 'yes or 'no'.\n").lower()
    print("\n" * 20)
    print(logo)
    if more_bidders == "no":
        more_bidders = False

# find the highest bid
max_value = 0
max_key = ""
for key, value in all_bidders.items():
    print(f"{key} bid ${value}")
    if value > max_value:
        max_value = value
        max_key = key

# print the winners details
print(f"the winner was {max_key} with a bid of ${max_value}")