第一次作业

一、元素分类

有如下值集合[11,22,33,44,55,66,77,88,99],将所有大于66 的值保存在字典的第一个key中,将小于66的值放在另一个key中,即:{‘k1’:大于66的所有值,‘k2’:小于66的所有值}

 li = [11,22,33,44,55,66,77,88,99]
dic={
     "k1":[],
     "k2":[]
 }
for i in li:
   if i<=66:
       dic['k1'].append(i)
    else:
        dic['k2'].append(i)
print(dic)
#输出结果
{'k2': [77, 88, 99], 'k1': [11, 22, 33, 44, 55, 66]}

 

二、查找

查找列表中元素,移动空格,并查找以a或者A开头并且以c结尾的所有元素

  li=["alec", "aric", "Alex", "Tony", "rain"]

  tu=("alec", "aric", "Alex", "Tony", "rain")

  dic={'k1': "alex", 'k2': 'aric', 'k3': "Alex", "k4": "Tony"}

li=["aleb","alec", "aric", "Alex", "Tony", "rain"]
for i in li:
    #print(i)    #i表示每一个元素
    new_i = i.strip()
    #if判断的顺序,从前到后,OR,自己成功就行,AND
    if (new_i.startswith('a') or new_i.startswith('A')) and new_i.endswith('c'):
        print(i)
#输出结果
alec
aric
tu=("alec", "aric", "Alex", "Tony", "rain")
for i in tu:
    #print(i)    #i表示每一个元素
    new_i = i.strip()
    #if判断的顺序,从前到后,OR,自己成功就行,AND
    if (new_i.startswith('a') or new_i.startswith('A')) and new_i.endswith('c'):
        print(i)
#输出
alec
aric
dic={'k1': "alex", 'k2': 'aric', 'k3': "Alex", "k4": "Tony"}
for i in dic.values():
    #print(i)    #i表示每一个元素
    new_i = i.strip()
    #if判断的顺序,从前到后,OR,自己成功就行,AND
    if (new_i.startswith('a') or new_i.startswith('A')) and new_i.endswith('c'):
        print(i)
#输出
aric

 

三、输出商品列表,用户输入序号,显示用户选中的商品

  商品li=["手机","电脑","鼠标垫", "游艇"]

 

li = ["手机","电脑","鼠标垫","游艇"]
for i,j in enumerate(li):
    print(i+1,j)
num = input('num: ')
num = int(num)
len_li=len(li)
if num>0 and num<=len_li:
    #索引
    good = li[num-1]
    print(good)
else:
    print("商品不存在!")

 

四、购物车

 功能要求:

  要求用户输入总资产,例如:2000

  显示商品列表,让用户根据序号选择商品,加入购物车

  购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功

  附加:课重置、某商品移除购物车

 goods=[
         {"name":"电脑","price":1999},
         {"name":"鼠标","price":19},
         {"name":"游艇","price":998},
         {"name":"键盘","price":20}
 ]        

 解答一

asset_all = 0

i1= input("请输入总资产:")
asset_all = int(i1)
car_list = []
# {
#     "电脑":{'price': "单个商品的价格:","num":"购买多少个!"}
# }
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 19},
    {"name": "游艇", "price": 998},
    {"name": "键盘", "price": 20}
]

for i in goods:
    #i,每一个列表的元素,字典
    #["price": 1999,'name': "电脑']
    print(i['name'],i['price'])

while True:
    i2 = input("请选择商品(Y/y结算):")
    if i2.lower() == "y":
        break
    for j in goods:
        if j['name'] == i2:
            #print(j)
            car_list.append(j)
            #print(car_dict)
#总和
print(car_list)
all_price=0
for item in car_list:
    p=item['price']
    all_price += p

print(asset_all,all_price)
if all_price > asset_all:
    print("余额不足!")
else:
    print("购买成功!")
#输出结果
请输入总资产:1000
电脑 1999
鼠标 19
游艇 998
键盘 20
请选择商品(Y/y结算):电脑
请选择商品(Y/y结算):y
[{'name': '电脑', 'price': 1999}]
1000 1999
余额不足!

解答二(用字典的方式保存商品)

#总资产
asset_all = 0
i1= input("请输入总资产:")
asset_all = int(i1)

goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 19},
    {"name": "游艇", "price": 998},
    {"name": "键盘", "price": 20}
]

for i in goods:
    #i,每一个列表的元素,字典
    #["price": 1999,'name': "电脑']
    print(i['name'],i['price'])
car_dict={}
# car_dict={
#     "电脑":{'price':123,'num':345}
# }
while True:
    i2 = input("请选择商品(Y/y结算):")
    if i2.lower() == "y":
        break
    #循环所有的商品,查找需要的商品
    for item in goods:
        if item["name"] == i2:
            name = item['name']
            #判断购物车是否已经有该商品,有,num+1;
            if name in car_dict.keys():
                car_dict[name]['num'] = car_dict[name]['num'] + 1
            else:
                car_dict[name] = {'num': 1, 'single_price': item['price']}

print(car_dict)
# {
#     '鼠标':{'single_price':10,'num':1},#1*10
#     '电脑':{'single_price':1999,'num':9}
#
# }
all_price = 0
for k,v in car_dict.items():
    n = v['single_price']
    m = v['num']
    all_sum = m * n
    all_price = all_price + all_sum
if all_price > asset_all:
    print("余额不足")
else:
    print("购买成功")
#输出结果
请输入总资产:100000
电脑 1999
鼠标 19
游艇 998
键盘 20
请选择商品(Y/y结算):电脑
请选择商品(Y/y结算):电脑
请选择商品(Y/y结算):电脑
请选择商品(Y/y结算):y
{'电脑': {'num': 3, 'single_price': 1999}}
购买成功

 

五、用户交互,显示省市县三级联动的选择

dic = {
    "河北": {
        "石家庄": ["鹿泉","邺城" "元氏"],
        "邯郸": ["永年", "涉县", "磁县"]
    },
    "湖南":{
        "长沙":["岳麓","开福","芙蓉"],
        "株洲":["中车", "608", "331"]
    },
    "江西":{
        "南昌":["红谷滩","新建","青山湖"],
        "抚州":["临川","广昌","鹰潭"]
    },
}
#解答
dic = { "河北": { "石家庄": ["鹿泉","邺城" "元氏"], "邯郸": ["永年", "涉县", "磁县"] }, "湖南":{ "长沙":["岳麓","开福","芙蓉"], "株洲":["中车", "608", "331"] }, "江西":{ "南昌":["红谷滩","新建","青山湖"], "抚州":["临川","广昌","鹰潭"] }, } #循环输出所有的省 for x in dic: print(x) i1 = input("请输入省份:") a = dic[i1] #循环所有的市 for j in a: print(j) i2 = input("请输入市:") b=dic[i1][i2] print(b) #list __str__ for z in b: print(z)

 

posted @ 2018-06-29 16:19  砍柴人Ryan  阅读(277)  评论(0编辑  收藏  举报