python的第四天(练习题)

1、有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}

temp = [11,22,33,44,55,66,77,88,99,90]
top = {'key1':[],
       'key2':[]
       }
for i in temp:
    if i <= 66:
        top['key1'].append(i)
    else:
        top['key2'].append(i)
print(top)

输出结果:{'key1': [11, 22, 33, 44, 55, 66], 'key2': [77, 88, 99, 90]}
遇到的问题:在创建字典的时候对于value一定要创建一个列表,而不能是一个空None

 


2、查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。

li = ["alec", " aric", "Alex", "Tony", "rain"]
    tu = ("alec", " aric", "Alex", "Tony", "rain") 
    dic = {'k1': "alex", 'k2': ' aric',  "k3": "Alex", "k4": "Tony"}
eg:
li = ["alec", " aric", "Alex", "Tonc", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {'k1': "alex", 'k2': ' aric',  "k3": "Alex", "k4": "Tony"}
top = []
for i in li:
    key = i.title().strip()
    if (key[0] == 'A') and (key[len(key)-1] == 'c'):
        top.append(i)
for i in tu:
    key = i.title().strip()
    if (key[0] == 'A') and (key[len(key)-1] == 'c'):
        top.append(i)
for i in dic.values():
    key = i.title().strip()
    if (key[0] == 'A') and (key[len(key)-1] == 'c'):
        top.append(i)
print(top)

输出结果:['alec', ' aric', 'alec', ' aric', ' aric']

 

 

三、输出商品列表,用户输入序号,显示用户选中的商品
商品 li = ["手机", "电脑", '鼠标垫', '游艇']
eg:
li = ["手机", "电脑", '鼠标垫', '游艇']

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

for k,i in enumerate(li):
    print(k,i)
top = int(input("请输出端口的序列号:"))
print(li[top])

输出结果:0 手机
1 电脑
2 鼠标垫
3 游艇
请输出端口的序列号:3
游艇

 

 

四、购物车
功能要求:

要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车

goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]
eg:
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
         ]
goods_new = []
top = int(input("请输入你的总资产:"))
top_new = 0
for k, i in enumerate(goods):
    print(k, i)
while True:
    key = int(input("请输入你需要购买的商品序列号:"))
    goods_new.append(goods[key])
    for k,i in enumerate(goods_new):
        print(k,i)
    top_new += goods[key]["price"]
    print("目前购物车的总金额:")
    print(top_new)
    if top_new > top:
        xz=int(input("由于你所选择的商品的总金额大于了你的总资产请充值或者移除你的商品请输入你的选项1代表充值,2代表移除,3代表放弃本次购买"))
        if xz == 1:
            top_li = int(input("请输入你充值的金额:"))
            top += top_li
            goods_new.clear()
            top_new = 0
            continue
        if xz == 2:
            for k,i in enumerate(goods_new):
                 print(k,i)
            while True:
                key_li = int(input("请输入你想移除的商品序列号结果请安888:"))
                if key_li == 888:
                    break
                top_li = goods_new.pop(key_li)
                top_new -= top_li['price']
            if top_new < top:
                print("下面是你购买的商品")
                print(goods_new)
                top -= top_new
                print("恭喜你购买成功,剩余的钱数为:")
                print(top)
                break
        else :
            print("感谢你的浏览")
            break
    else:
        r =input("如果你选择好了请按yes,继续请按no:")
        if r == "yes":
            print("恭喜你购买成功,剩余金额为:")
            top -= top_new
            print(top)
            break

自己写的感觉很乱

 

posted @ 2018-02-27 16:40  小白的崛起  阅读(265)  评论(0)    收藏  举报