生活随笔
收集整理的這篇文章主要介紹了
python_day3作业
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、元素分類
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],
將所有大于 66 的值保存至字典的第一個key中,將小于 66 的值保存至第二個key的值中。
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
1 list_num = [11,22,33,44,55,66,77,88,99,90
]
2 dir_num =
{
3 'k1':[],
4 'k2':[],
5 }
6 for i
in list_num:
7 if i > 66
:
8 dir_num[
'k1'].append(i)
9 else:
10 dir_num[
'k2'].append(i)
11 print(dir_num)
View Code 二、查找列表中元素,移除每個元素的空格,并查找以 a或A開頭 并且以 c 結尾的所有元素。
1 li = [
"alec",
" aric",
"Alex",
"Tony",
"rain"]
2 tu = (
"alec",
" aric",
"Alex",
"Tony",
"rain")
3 dic = {
'k1':
"alex",
'k2':
' aric',
"k3":
"Alex",
"k4":
"Tony"}
4 for i
in li:
5 i =
i.strip()
6 if (i.startswith(
'A')
or i.startswith(
'a'))
and i.endswith(
'c') :
7 print(i)
列表 1 li = [
"alec",
" aric",
"Alex",
"Tony",
"rain"]
2 tu = (
"alec",
" aric",
"Alex",
"Tony",
"rain")
3 dic = {
'k1':
"alex",
'k2':
' aric',
"k3":
"Alex",
"k4":
"Tony"}
4 for i
in tu:
5 i =
i.strip()
6 if (i.startswith(
'A')
or i.startswith(
'a'))
and i.endswith(
'c') :
7 print(i)
元組 1 li = [
"alec",
" aric",
"Alex",
"Tony",
"rain"]
2 tu = (
"alec",
" aric",
"Alex",
"Tony",
"rain")
3 dic = {
'k1':
"alex",
'k2':
' aric',
"k3":
"Alex",
"k4":
"Tony"}
4 for i
in dic.values():
5 i =
i.strip()
6 if (i.startswith(
'A')
or i.startswith(
'a'))
and i.endswith(
'c') :
7 print(i)
字典 三、輸出商品列表,用戶輸入序號,顯示用戶選中的商品
1 li = [
"手機",
"電腦",
'鼠標墊',
'游艇']
2 for k,i
in enumerate(li,1
):
3 print(k,i)
4 user = int(input(
'請輸入商品序號:'))
5 print(li[user-1])
View Code 四、購物車
功能要求:
要求用戶輸入總資產,例如:2000
顯示商品列表,讓用戶根據序號選擇商品,加入購物車
購買,如果商品總額大于總資產,提示賬戶余額不足,否則,購買成功。
附加:可充值、某商品移除購物車
1 goods =
[
2 {
"name":
"電腦",
"price": 1999
},
3 {
"name":
"鼠標",
"price": 10
},
4 {
"name":
"游艇",
"price": 20
},
5 {
"name":
"美女",
"price": 998
},
6 ]
7 # 購物車
8 shopping_cart =
[]
9 # 購物總金額
10 shopping_money =
0
11 # 用戶資產
12 arg =
True
13 while arg:
14 user_property = input(
'總資產:')
#等待用戶輸入資產金額
15 if user_property.isdigit():
#判斷用戶輸入是不是數字
16 while arg:
#while 循環
17 print(
'商品列表:')
#界面顯示文字 '商品列表'
18 for k,i
in enumerate(goods,1):
#循環goods列表
19 print(
'序列:%s 名稱:%s 價格:%s '%(k,i[
'name'],i[
'price']))
#顯示goods商品列表
20 user_buy = input(
'選擇序號添加購物車,按q鍵退出>>>')
#user_buy 加入購物車
21 if user_buy.isdigit():
#判斷用戶輸入是否是數字
22 user_buy = int(user_buy)
#轉換int整數
23 if user_buy <= len(goods):
#判斷用戶輸入的數字是否在列表存在
24 shopping_cart.append(goods[user_buy-1])
#把用戶購買的商品加入購物車
25 print(
'----------------已購買商品----------------')
26 for z,i
in enumerate(shopping_cart,1):
#循環購物車,打印出已購買商品清單
27 print(
'序列:%s名稱:%s價格:%s'%(z,i[
'name'],i[
'price']))
28 print(
'------------------------------------------')
29 elif user_buy ==
'q':
#用戶輸入q退出商品列表界面
30 for n
in shopping_cart:
# 循環購物車商品,提取金額
31 shopping_money += (n[
'price'])
# 把金額添加到自定義變量shopping_money,得出購物總金額
32 user_property = int(user_property)
# 把user_property轉換int類型
33 arg = False
#退出循環
34 else:
35 print(
'只能輸入整數')
36 continue #用戶輸入資產金額,不是數字 直接continue返回從新輸入資產金額
37 while True:
#while 循環
38 while True:
39 ppa = input(
'1 充值\n2 選擇刪除購物車物品\n3 結算\n>>>>>')
#等待用戶輸入選擇
40 if ppa ==
'1':
#用戶選擇充值
41 while True:
42 pay = input(
'請輸入充值金額:')
#等待用戶輸入充值金額
43 if pay.isdigit():
#判斷用戶輸入是否是數字
44 pay = int(pay)
#把pay轉換成int類型
45 user_property += pay
#把用充值金額加到總資產金額里
46 print(
' 余額:',user_property)
#顯示用戶當前余額(包括已充值)
47 print(
'------------------------------------------')
48 break #充值完退出次循環
49 continue #用戶輸入的不是數字,返回從新輸入金額
50 elif ppa ==
'2':
#用戶選擇刪除
51 print(
'----------------已購買商品----------------')
52 for z,i
in enumerate(shopping_cart,1):
#顯示購物車列表
53 print(z,(i[
'name'],i[
'price']))
54 print(
'------------------------------------------')
55 while True:
56 cc = (input(
'選擇刪除物品的序列號\n退出按q鍵:'))
#等待用戶選擇刪除物品序列
57 if cc.isdigit():
#判斷用戶輸入是否是數字
58 cc = int(cc)
#把用戶輸入轉換int類型
59 shopping_money -= (shopping_cart[cc-1][
'price'])
#把用戶刪除物品價格從shopping_money中去除
60 del shopping_cart[cc-1]
#從購物車把物品刪除
61 for z,i
in enumerate(shopping_cart,1):
#循環顯示剩下購物車物品
62 print(z,(i[
'name'],i[
'price']))
63 else:
#按q鍵退出刪除選項
64 break
65 elif ppa ==
'3':
#用戶選擇結算
66 if shopping_money <= user_property:
#如果購物車金額小于等于總金額則購買成功
67 print(
'----------------已購買商品----------------')
68 for z, i
in enumerate(shopping_cart, 1
):
69 print(z, (i[
'name'], i[
'price']))
70 print(
'------------------------------------------')
71 print(
' 余額:%s元' % (user_property -
shopping_money))
72 break
73 else:
#如果購物車金額大于等于總金額則購買失敗
74 print(
'金額不足!'' 還差金額:', (shopping_money -
user_property))
75 print(
'------------------------------------------')
76 break #退出此次大循環 View Code 五、用戶交互,顯示省市縣三級聯動的選擇
1 dic =
{2
"河北": {3
"石家莊": [
"鹿泉",
"藁城",
"元氏"],4
"邯鄲": [
"永年",
"涉縣",
"磁縣"],5
},6
"河南": {
"鄭州": [
"不知道",
"少林寺",
"嵩山"],7
"邯鄲": [
"包拯",
"展昭",
"五鼠"]8
},9
"山西": {
'太原':[
'xxx',
'ooo',
'999'],
'運城':[
'撒旦',
'耶穌',
'華盛頓']}
10
}
11
for i
in dic:
12
print(i)
13 name = input(
'請輸入省份:')
14 dic_1 =
dic[name]
15
for z
in dic_1:
16
print(z)
17 name_1 = input(
'請輸入縣城:')
18 dic_2 =
dic_1[name_1]
19
for x
in dic_2:
20
print(x)
View Code ?
轉載于:https://www.cnblogs.com/zzn91/p/6985089.html
總結
以上是生活随笔為你收集整理的python_day3作业的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。