其他分享
首页 > 其他分享> > 练习题以及答案

练习题以及答案

作者:互联网

一、元素分类:
有如下值集合 [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,90] a = [] b = [] for k in li: if k>66: a.append(k) else: b.append(k) k1 = {'大于66的所有值':a} k2 = {'小于66的所有值':b} print(k1,k2)%注意输出时要对齐
---{'大于66的所有值': [77, 88, 99, 90]} {'小于66的所有值': [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 = ["alec", " aric", "Alex", "Tony", "rain"] for i in li: v = i.strip() if (v.startswith("a") or v.startswith("A") and v.endswith("c")): print(v) ---alec aric tu = ("alec", " aric", "Alex", "Tony", "rain") for n in tu: v = n.strip() if (v.startswith("a") or v.startswith("A") and v.endswith("c")): print(v) ---alec aric dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"} for i in dic.values(): v = i.strip() if (v.startswith("a") or v.startswith("A") and v.endswith("c")): print(v) ----alex aric
三、输出商品列表,用户输入序号,显示用户选中的商品
商品 li = ["手机", "电脑", '鼠标垫', '游艇']
n = input("请输入序号:") li = ["手机", "电脑", '鼠标垫', '游艇'] for i ,a1 in enumerate(li,1): print(i,a1) b1 = int(n) print(li[b1 - 1])
四、购物车
功能要求:

要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车
goods = [ {"name": "电脑", "price": 3000}, {"name": "鼠标", "price": 103}, {"name": "游艇", "price": 200000}, {"name": "美女", "price": 998}, ] total = 0 pay = 0 shopping = {}#建立商品字典(keys商品、values数量) zongji = input("请输入总资产") if zongji.isdigit(): total = str(zongji) print("总资产为:" + total + "元") else: print("请输入数字!") while True: caozuo = input("请输入操作:1.显示商品,2.充值,3.退出") caozuo = int(caozuo) if int(caozuo) == 1: for g in enumerate(goods,1): print(g[0],":",g[1]["name"],g[1]["price"],"元") while True: buy_good = input("请依次输入购买商品编号(结账输入g,移除商品输入d)") if buy_good.isdigit(): buy = int(buy_good) if buy >0 and buy <= len(goods): if goods[buy-1]["name"] in shopping.keys():#.has_key(k)是否存在 shopping[goods[buy-1]["name"]] = int(shopping[goods[buy-1]["name"]])+1 pay += int(goods[buy-1]["price"]) print("已选购:") for keys, values in shopping.items(): print(str(keys), "数量为", str(values)) else: shopping.setdefault(goods[buy-1]["name"],1) #setdefault添加 pay += int(goods[buy - 1]["price"]) print("已选购:") for keys,values in shopping.items(): print(str(keys),"数量为",str(values)) elif buy_good == "g": if int(pay) <= int(total): print("购买成功,共计消费"+str(pay)+"元,余额为"+str(int(total)-int(pay))+"元") break else: print("共消费"+str(pay)+"元,总资产为"+str(total)+"元,欠"+str(int(pay)-int(total))+"元,请充值!") chongzhi = input("请输入充值金额") if chongzhi.isdigit(): total = int(total) + int(chongzhi) print("充值成功,总资产为:" + str(total) + "元。") # 字符串+数字 会报错 else: print("请输入数字!") elif buy_good == "d": print("已选购:") for keys, values in shopping.items(): print(str(keys), "数量为", str(values)) del_good = input("请输入移除商品编号") if del_good.isdigit() and int(del_good) > 0 and int(del_good) <= len(goods): del_good = int(del_good) if goods[int(del_good) - 1]["name"] in shopping.keys(): # .has_key(k)是否存在 if int(shopping[goods[del_good-1]["name"]]) == 1: shopping.pop(goods[del_good-1]["name"]) print("移除成功,已选购:") for keys, values in shopping.items(): print(str(keys), "数量为", str(values)) else: shopping[goods[del_good - 1]["name"]] = int(shopping[goods[del_good - 1]["name"]]) - 1 pay -= int(goods[del_good - 1]["price"]) print("已选购:") for keys, values in shopping.items(): print(str(keys), "数量为", str(values)) else: print("无此商品!") else: print("输入错误!") else: print("输入错误!") break elif int(caozuo) == 2: chongzhi = input("请输入充值金额") if chongzhi.isdigit(): print("充值成功,总资产为:" + str(int(total)+int(chongzhi)) + "元。")#字符串+数字 会报错 else: print("请输入数字!") elif int(caozuo) == 3: print("退出操作!") break else: print("输入错误!")

标签:练习题,startswith,答案,以及,aric,li,66,print,alec
来源: https://www.cnblogs.com/zhoujie228/p/13965194.html