-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.py
161 lines (123 loc) · 3.44 KB
/
day09.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Day 9: Dictionaries and nesting
# curly braces denote dictionaries
programming_dictionary = {
"Bug": "An error in a program that prevents the program from running as expected.",
"Function": "A piece of code that you can easily call over and over again.",
}
# retrieve elements
print(programming_dictionary["Bug"])
# Adding new items
programming_dictionary["Loop"] = "The action of doing something over and over again."
# empty dictionary
empty_dictionary = {}
# wipe existing dictionary
programming_dictionary = {}
# edit item
programming_dictionary["Bug"] = "A moth in your computer."
# loop through dictionary
for key in programming_dictionary:
print(key)
print(programming_dictionary[key])
####################
# Exercise 1: Grading program
student_scores = {
"Harry": 81,
"Ron": 78,
"Hermione": 99,
"Draco": 74,
"Neville": 62,
}
# 🚨 Don't change the code above 👆
#TODO-1: Create an empty dictionary called student_grades.
student_grades = {}
#TODO-2: Write your code below to add the grades to student_grades.👇
for student in student_scores:
if student_scores[student] > 90:
student_grades[student] = "Outstanding"
elif student_scores[student] > 80:
student_grades[student] = "Exceeds Expectations"
elif student_scores[student] > 70:
student_grades[student] = "Acceptable"
else:
student_grades[student] = "Fail"
# 🚨 Don't change the code below 👇
print(student_grades)
######################
# Nesting
capitals = {
"France": "Paris",
"Germany": "Berlin"
}
# nesting a list in a dictionary
travel_log = {
"France": ["Paris", "Lille", "Dijon"]
"Germany": ["Berlin", "Hamburg", "Stuttgart"]
}
# nesting a dictionary in a dictionary
travel_log = {
"France": {"cities_visited": ["Paris", "Lille", "Dijon"], "total_visits": 12}
"Germany": ["Berlin", "Hamburg", "Stuttgart"]
}
# dictionary in a list
travel_log = [
{
"country": "France",
"cities_visited": ["Paris", "Lille", "Dijon"],
"total_visits" = 12
},
{
"country": "Germany",
"cities_visited": ["Berlin", "Hamburg", "Stuttgart"],
"total_visits" = 5
},
]
#####################
# Exercise 2
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, visits, cities_visited):
new_dictionary = {
"country": country,
"visits": visits,
"cities": cities_visited
}
travel_log.append(new_dictionary)
#🚨 Do not change the code below
add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])
print(travel_log)
###################
# Silent Auction program
from replit import clear
#HINT: You can call clear() to clear the output in the console.
import art
print(art.logo)
print("Welcome to the silent auction program.")
another_one = True
bids = {}
while another_one:
name = input("What is your name? ")
bid = int(input("What is your bid? "))
bids[name] = bid
another = input("Are there any other bidders? Type 'yes' or 'no'. ")
clear()
if another == "no":
another_one = False
winning_bet = 0
for bidder in bids:
if bids[bidder] > winning_bet:
winning_bet = bids[bidder]
winner = bidder
print(f"{winner} is the winner with a bid of ${winning_bet}.")