day9_Udemy Python 100days

#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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值