#!usr/bin/env python # -*- coding:utf-8 -*- # 一、元素分类 # # 有如下值集合 [11,22,33,44,55,66,77,88,99,90...], # 将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。 # 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值} li = [11,22,33,44,55,66,77,88,99,100,101] dic = {'k1':[],'k2':[]} for i in li: if i >=66: dic['k1'].append(i) else: dic['k2'].append(i) print(dic)
#!usr/bin/env python # -*- coding:utf-8 -*- # 二、查找 # 查找列表中元素,移除每个元素的空格,并查找以 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 = ["alec", " aric", "Alex", "Tony", "rain"] for i in li: li_strip = i.strip() li_upper = li_strip.upper() if li_upper.startswith('A') and li_strip.endswith('c'): print(li_strip) tu = ("alec", " aric", "Alex", "Tony", "rain") for i in tu: tu_strip = i.strip() tu_upper = tu_strip.upper() if tu_upper.startswith('A') and tu_strip.endswith('c'): print(tu_strip) dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"} for key in dic: dic[key] = dic[key].strip() if dic[key].upper().startswith('A') and dic[key].endswith('c'): print(key,dic[key])
#!usr/bin/env python # -*- coding:utf-8 -*- # 三、输出商品列表,用户输入序号,显示用户选中的商品 # 商品 li = ["手机", "电脑", '鼠标垫', '游艇'] li = ["手机", "电脑", '鼠标垫', '游艇'] for i,j in enumerate(li,1): print(i,j) num = input('请输入您要购买的商品序号:') if num.isdigit() and int(num) > 0 and int(num) <= len(li): li_index = int(num) - 1 print(li[li_index]) else: print("您的商品不存在!")
#!usr/bin/env python # -*- coding:utf-8 -*- # 四、购物车(第一种方法) # 功能要求:goods已提供 # 要求用户输入总资产,例如:2000 # 显示商品列表,让用户根据序号选择商品,加入购物车 # 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。 # 附加:可充值、某商品移除购物车 goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] total = int(input('请输入总资产:')) for key,value in enumerate(goods): print(key+1,value['name'],value['price']) cart = [] while True: buy_strid = input('请输入您要购买的商品序号,按E键确认购买:') if buy_strid == 'E': break buy_id = int(buy_strid)-1 buy_good = goods[buy_id] cart.append(buy_good) print('您购物车里面的商品是:') buy_total = 0 for good in cart: buy_total += good['price'] print(good['name'],good['price']) print('当前价值为:', buy_total) while True: if total < buy_total: result = input('余额不足!按R进行充值,按D减少购物车商品,按其他任意键退出购买:') if result == 'R': money = input('请输入充值的金额:') total = total + int(money) elif result == 'D': for key,value in enumerate(cart): print(key+1,value['name'],value['price']) cancel_strid = input('请输入您要取消的商品序号:') cancel_id = int(cancel_strid)-1 cancel_price = cart[cancel_id]['price'] cart.pop(cancel_id) buy_total -= cancel_price else: break else: print('购买成功!') break
#!usr/bin/env python # -*- coding:utf-8 -*- # 四、购物车(第二中方法) # 功能要求:goods已提供 # 要求用户输入总资产,例如:2000 # 显示商品列表,让用户根据序号选择商品,加入购物车 # 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。 # 附加:可充值、某商品移除购物车 goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] asset_total = 0 i1 = input('请输入总资产:') asset_total = int(i1) for i in goods: print(i['name'],i['price']) cart = {} #目标字典{'电脑':{'price':1999,'num':1}} while True: i2 = input('请输入您加入购物车的商品(y/Y结束):') if i2.lower() == 'y': break for i in goods: if i2 in cart: num = cart[i2]['num'] + 1 else: num = 1 if i2 == i['name']: cart[i2] = {'price':i['price'],'num':num} sum_price = 0 print(cart) for i in cart: price = cart[i]['price'] * cart[i]['num'] sum_price += price if sum_price > asset_total: print("余额不足!") else: print("购买成功")