#Dictionaries
#{"key":"value"}
programming_dictionary = {
"Bug":"An error in program that prevent the program from running as expected.",
"Function":"A piece of code that you can easily call over and over again.",
123:"nothing"
}
'''
If we defined this dictionary without putting a string around each of these
keys,the it's going to error out and won't even let us run.
It's going to tell us undefined named bug because it thinks that this is a variable
that you've declared somewhere,but it's not.
In fact,what you wanted are these strings for keys
'''
#Retieving items from dictionary.
print(programming_dictionary[123])
#Adding new items from dictionary.
programming_dictionary["Loop"] = "The action of doing something over and over again."
print(programming_dictionary)
#Create an empty dictionary.
empty_dictionary = {}
#Wipe an existing dictionary
programming_dictionary = {}
#Edit an item in a dictionary
programming_dictionary["Bug"] = "A moth in a computer."
#Loop through a dictionary
for key in programming_dictionary:
print(key)
print(programming_dictionary[key])
#9.1 Grading Program
student_scores = {
"Harry":81,
"Rom":78,
"Hermione":99,
"Draco":74,
"Neville":62,
}
student_grades = {}
for student in student_scores:
score = student_scores[student]
if score > 90:
student_grades[student] = "Outstanding"
elif score > 80:
student_grades[student] = "Exceeds Expectations"
elif score >70:
student_grades[student] = "Acceptable"
else:
student_grades[student] = "Fail"
print(student_grades)
#Nesting
capitals = {
"France":"paris",
"Germany":"Berlin",
}
#Nesting a list in a dictionary
#Nesting Dictionary in a Dictionary
travel_log ={
"France":{"cities_visited":["Paris","lille","dijon"],"total_visits":12},
"Germany":{"cities_visited":["Berlin","Hamburg","Stuttgart"],"total_visit":5},
}
#You could also just nest a list in a list,but it's not quite as useful sas nesting a list in a dictionary or a dictionray
#Nesting Dictionary in a List
travel_log = [
{
"country":"France",
"cities_visited":["Paris","lille","dijon"],
"total_visits":12
},
{
"country":"Germany",
"cities_visited":["Berlin","Hamburg","Stuttgart"],
"total_visit":5
},
]
#9.2 Dictionary in List Exercise
travel_log = [
{
"country":"France",
"visits":12,
"cities":["Paris","lille","dijon"],
},
{
"country":"Germany",
"visits":5,
"cities":["Berlin","Hamburg","Stuttgart"],
},
]
def add_new_country(country_visited,times_visited,cities_visited):
new_country = {}
new_country["country"] = country_visited
new_country["visits"] = times_visited
new_country["cities"] = cities_visited
travel_log.append(new_country)
add_new_country("Russia",2,["Moscow","Saint Petersburg"])
print(travel_log)
#the Secret Auction Program
from replit import clear
#HINT:You can call clear() to clear the output in the consule
from art import logo
print(logo)
bids = {}
bidding_finished = False
def find_highest_bidder(bidding_record):
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"Thewinner is {winner} with a bid of ${highest_bid}")
while not bidding_finished:
name = input("WHat is your name?:")
price = int(input("What is your bids?:$"))
bids[name] = price
should_continue = input("Are there any other bidders?Type 'Yes' or 'No'.\n")
if should_continue == 'No':
bidding_finished = True
find_highest_bidder(bids)
elif bidding_finished == 'Yes':
clear()
day9_Udemy Python 100days
最新推荐文章于 2024-08-19 13:32:52 发布