标签:ppi 提醒 author python基础2 ase mac opp lin 详细
本节内容
1.定义列表
names = [‘Alex‘,"Tenglan",‘Eric‘]
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表 它只有2个方法,一个是count,一个是index,完毕。
请闭眼写出以下程序。 程序:购物车程序 需求: 1. 启动程序后,让用户输入工资,然后打印商品列表 2. 允许用户根据商品编号购买商品 3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额
# coding=utf-8 # Author:L product_list = [ ("Iphone",5800), ("Mac pro",1200), ("Bike",100), ("watch",1000), ("Coffee",12), ("Alex Python",120) ] shopping_list = [] salary = input("Input your salary: ") if salary.isdigit(): salary = int(salary) while True: for index,item in enumerate(product_list): #print(product_list.index(item),item) print(index,item) user_choic = input("选择要买的?》》》:") if user_choic.isdigit(): user_choic = int(user_choic) if user_choic < len(product_list) and user_choic >= 0: p_item = product_list[user_choic] if p_item[1] <= salary: #买的起 shopping_list.append(p_item) salary -= p_item[1] print("Added %s into your shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) ) else: print("\033[32;1m你的余额不足,只有[%s]\033[0m"%(salary)) else: print("product code [%s] is not exist",user_choic) elif user_choic == "q": print("--------shopping list-------") for p in shopping_list: print(p) print("----------------------------") print("Your current balance:",salary) exit() else: print("invalid option") else: print("please enter number,try again...")
知识小点:
特性:不可修改
字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。
字典的特性:
1.语法: info = { ‘stu1101‘: "TengLan Wu", ‘stu1102‘: "LongZe Luola", ‘stu1103‘: "XiaoZe Maliya", }
# coding=utf-8 # Author:L data = { "北京":{ "朝阳区":{}, "天安区":{}, "玄武区":{}, }, "西安":{ "雁塔区":{ "电子正街":["鸿星尔克","特步"], "高新区":["百度","腾讯"], "科技路":["西部","欧朋"], }, "碑林区":{ "太白路":["西安理工","西安交大"], "金华路":["火腿","飞信"], "南二环":["后卫寨","鱼化寨"] }, "长安区":{ "111":["aa","agag"], "222":["afdas","ag"], "333":["afd","ag"] }, }, "上海":{ "虹桥区":{}, "陆家嘴区":{}, "海港区":{}, }, } while True: for i in data: print(i) choice = input("选择进入1>>:") if choice in data: while True: for i2 in data[choice]: print("\t",i2) choice2 = input("选择进入2>>:") if choice2 in data[choice]: while True: for i3 in data[choice][choice2]: print("\t\t",i3) choice3 = input("选择进入3>>:") if choice3 in data[choice][choice2]: for i4 in data[choice][choice2][choice3]: print("\t\t\t",i4) choice4 = input("this is laster:按b返回>>:") if choice4 == "b": pass #什么也不做,占位符,防止出错 if choice3 == "b": break if choice2 == "b": break
# coding=utf-8 # Author:L data = { "北京":{ "朝阳区":{}, "天安区":{}, "玄武区":{}, }, "西安":{ "雁塔区":{ "电子正街":["鸿星尔克","特步"], "高新区":["百度","腾讯"], "科技路":["西部","欧朋"], }, "碑林区":{ "太白路":["西安理工","西安交大"], "金华路":["火腿","飞信"], "南二环":["后卫寨","鱼化寨"] }, "长安区":{ "111":["aa","agag"], "222":["afdas","ag"], "333":["afd","ag"] }, }, "上海":{ "虹桥区":{}, "陆家嘴区":{}, "海港区":{}, }, } exit_flag = False while not exit_flag: for i in data: print(i) choice = input("选择进入1>>:") if choice in data: while not exit_flag: for i2 in data[choice]: print("\t",i2) choice2 = input("选择进入2>>:") if choice2 in data[choice]: while not exit_flag: for i3 in data[choice][choice2]: print("\t\t",i3) choice3 = input("选择进入3>>:") if choice3 in data[choice][choice2]: for i4 in data[choice][choice2][choice3]: print("\t\t\t",i4) choice4 = input("this is laster:按b返回>>:") if choice4 == "b": pass #什么也不做,占位符,防止出错 elif choice4 == "q": exit_flag = True if choice3 == "b": break elif choice3 == "q": exit_flag = True if choice2 == "b": break elif choice2 == "q": exit_flag = True elif choice == "q": exit_flag = True
标签:ppi 提醒 author python基础2 ase mac opp lin 详细
原文地址:http://www.cnblogs.com/venicid/p/7186834.html