Udemy課程:[100 Days Of Code(Dr. Angela Yu)](https://www.udemy.com/course/100-days-of-code/)
# Day 9 - Beginner - Dictionaries, Nesting and the Secret Auction
###### tags: `python` `Udemy` `100 Days Of Code`
2021.02.22(Mon.)
## ● 前言 / 心得
這次我覺得教的內容不算難,但project的部分結合到之前學得各種東西,所以變得滿困難的,所以這次完全是跟著老師的影片,一步一步跟著打,但打的同時也去思考,這個步驟為甚麼要這樣做。
## ● 上課筆記
## 0.code
[day-9-start](https://repl.it/@tina0915tw/day-9-start#main.py)
[day-9-1-exercise](https://repl.it/@tina0915tw/day-9-1-exercise#README.md)
[day-9-end](https://repl.it/@tina0915tw/day-9-end#main.py)
[day-9-2-exercise](https://repl.it/@tina0915tw/day-9-2-exercise#README.md)
[blind-auction-start](https://repl.it/@tina0915tw/blind-auction-start#main.py)
[blind-auction-completed](https://repl.it/@tina0915tw/blind-auction-completed#main.py)
## 1. Dictionary (字典)
可參考網站:[Python 初學第九講 — 字典](https://medium.com/ccclub/ccclub-python-for-beginners-tutorial-533b8d8d96f3)
> 注意:list陣列是用中括號[ ];dictionary字典是用大括號{ }。
* 表示語法:
```python=
dictionaryName = {"key":"value"}
dictionaryName["key"] #這樣便可以取得這個key的value
#新增新的key跟value
dictionaryName["key1"] = "value1"
#清除所有key跟value
dictionaryName = {}
#編輯現有的key跟value
dictionaryName["key"] = "newValue"
#對此dictionary做loop迴圈
for thing in dictionary:
print(thing) #印出dictionary裡面所有的key
print(dictionaryName[thing]) #印出dictionary裡面所有的value
```
* 舉例:
| Key | Value |
| -------- | ------------------------------------------------------------------------- |
| 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. |
| Loop | The action of doing something over and over again.|
```python=
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.",
}
print(programming_dictionary["Bug"])#這樣便可以取得Bug的value
#新增Loop跟Loop的value
programming_dictionary["Loop"] = "The action of doing something over and over again."
```
## 2.[day-9-1-exercise](https://repl.it/@tina0915tw/day-9-1-exercise#README.md)
* 作法:
```python=
student_scores = {
"Harry": 81,
"Ron": 78,
"Hermione": 99,
"Draco": 74,
"Neville": 62,
}
#TODO-1:
student_grades = {}
#TODO-2:
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)
```
## 3.Nesting Lists And Dictionaries
1. Nesting
```python=
capitals = {
"France": "Paris",
"Germany": "Berlin",
}
```
2. Nesting a List in a Dictionary
```python=
travel_log = {
"France": ["Paris", "Lille", "Dijon"],
"Germany": ["Berlin", "Hamburg", "Stuttgart"],
}
```
3. Nesting a Dictionary in a Dictionary
```python=
travel_log = {
"France": {"cities_visited": ["Paris", "Lille", "Dijon"], "total_visits": 12},
"Germany": {"cities_visited": ["Berlin", "Hamburg", "Stuttgart"], "total_visits": 5},
}
```
4. Nesting Dictionaries in Lists
```python=
travel_log = [
{
"country": "France",
"cities_visited": ["Paris", "Lille", "Dijon"],
"total_visits": 12,
},
{
"country": "Germany",
"cities_visited": ["Berlin", "Hamburg", "Stuttgart"],
"total_visits": 5,
},
]
```
## 4.Project
* Flowchart流程圖

* 老師作法:
```python=
from replit import clear
#HINT: You can call clear() to clear the output in the console.
from art import logo
print(logo)
bids = {}
bidding_finshed = False
def find_hightest_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"The winner is {winner} with a bid of ${highest_bid}.")
while not bidding_finshed:
name = input("What is your name?")
price = int(input("What is your bid? $"))
bids[name] = price
should_continue = input("Are there any other bidders? Type 'yes' or 'no'\n")
if should_continue == "no":
bidding_finshed = True
find_hightest_bidder(bids)
elif should_continue == "yes":
clear()
```