“”"
練習:根據輸入的季節獲取對應的月份
“”"
while True:
dict_season = dict([
("春", (1, 2, 3)),
("夏", (4, 5, 6)),
("秋", (7, 8, 9)),
("冬", (10, 11, 12))
])
season = input("請輸入季度:")
if season in dict_season:
for key, values in dict_season.items():
if key == season:
print(str(values)+"月份")
break
else:
print("輸入有誤!")
“”"
練習在控制檯中錄入多個學生的姓名、性別、成績、年齡
“”"
dict_students_info = {}
while True:
name = input("請輸入姓名:")
if name == "esc":
break
age = int(input("請輸入年齡:"))
source_num = int(input("請輸入成績科目數:"))
sources = []
for i in range(source_num):
source = float(input("請錄入第%d科的成績:" % (i+1)))
sources.append(source)
sex = input("請錄入學生的性別:")
dict_students_info[name] = [{
"年齡": age,
"成績": sources,
"性別": sex
}]
for key, values in dict_students_info.items():
print(key, values)
“”"
練習:創建調查問卷
輸入姓名(esc結束)
輸入喜好(esc結束)
調查後顯示所有信息
“”"
dict_questionnaire = {}
list_hobbys = []
while True:
name = input("請輸入姓名(esc結束):")
if name == "esc":
break
else:
while True:
hobby = input("請輸入喜好(esc結束):")
if hobby == "esc":
break
else:
list_hobbys.append(hobby)
dict_questionnaire[name] = list_hobbys
print(dict_questionnaire)
“”"
練習
1、將1970年至2050中的閏年,存入列表
“”"
list_leap_year = []
for i in range(1970, 2051):
if i % 4 == 0 and i % 100 != 0 or i % 400 == 0:
list_leap_year.append(i)
else:
pass
“”"
練習
2、描述多個商品信息(屠龍刀:100000元,蘋果:5999元)等
“”"
dict_goods = {"iphone x": 4900, "華爲": 3500, "魅族16spro": 2999}
for key, values in dict_goods.items():
print("商品名:"+key+"----------------"+"價格爲:"+str(values))
“”"
練習
3、存儲全國個個城市的景區與美食
北京:
景區:故宮,天安門,天壇
美食:烤鴨,炸醬麪,滷煮
四川:
景區:九寨溝,峨眉山
美食:火鍋,串串香,兔頭
在控制檯中顯示
“”"
dict_city = {
"北京": {
"景區": ["故宮", "天安門", "天壇"],
"美食": ["烤鴨", "炸醬麪", "滷煮"]
},
"四川": {
"景區": ["峨眉山", "九寨溝"],
"美食": ["兔頭", "串串香", "火鍋"]
}
}
for city, info in dict_city.items():
print(city)
for key, value in info.items():
print("%s:" % key)
for item in value:
print("%s" % item)
“”"
練習
4、計算一個字符串中的字符以及出現的次數
abcdeface
a 2
b 1
c 2
d 1
e 2
f 1
“”"
str_1 = "abcadefshgeghe"
dict_result = {}
for item in str_1:
# 判斷字符是否相同,相同加一
if item not in dict_result:
dict_result[item] = 1
else:
dict_result[item] += 1
print(dict_result)
“”"
猜拳遊戲:石頭剪刀布
系統隨機選擇一個
用戶輸入一個
判斷輸贏
提示:將勝利策略存入容器
石頭 戰勝 剪刀
剪刀 戰勝 布
布 戰勝 石頭
“”"
import random
wins = {
"石頭": "剪刀",
"剪刀": "布",
"布": "石頭",
}
tuple_items = ("剪刀", "布", "石頭")
reando_number = random.randint(0, 2)
str_sys_input = tuple_items[reando_number]
print(str_sys_input)
user_input = input("請輸入:")
if str_sys_input == user_input:
print("平局!!")
elif wins[user_input] == str_sys_input:
print("勝利!!")
else:
print("失敗!!")