py脚本汇集
1.商场购物
脚本运行时,打印商品列表,根据用户的交互式信息实现大体的购物流程;
1 # Author: kazihuo 2 salary = input("Please input your salary:") 3 product_list = [ 4 ('iphone',5800), 5 ('laptop',7700), 6 ('bike',500), 7 ('book',53), 8 ('watch',15) 9 ] 10 shopping_list = [] 11 if salary.isdigit(): 12 salary = int(salary) 13 while True: 14 for item in product_list: 15 print(product_list.index(item),item) 16 user_choice = input("Please choice:") 17 if user_choice.isdigit(): 18 user_choice = int(user_choice) 19 if user_choice > -1 and user_choice < len(product_list): 20 p_item = product_list[user_choice] 21 if p_item[1] <= salary: 22 shopping_list.append(p_item) 23 salary -= p_item[1] 24 #print("Added %s into cart,current balance is "%p_item,salary ) 25 print("Added %s into shopping cart,your current balance is \033[31m%s\033[0m" %(p_item,salary)) 26 else: 27 print("your balance is \033[41;37m%s\033[0m,not enough!" %salary) 28 else: 29 print("product code %s is not exist!"%user_choice) 30 elif user_choice == 'q': 31 print("---------shopping list---------") 32 for p in shopping_list: 33 print(p) 34 print("your current balance:",salary) 35 exit() 36 else: 37 print("Invalid option!")
2.三级菜单
利用字典特性实现三级菜单的功能,用户根据选择一级一级的进入;
1 #Author: kazihuo 2 data = { 3 "BeiJing":{ 4 "Changpin":{ 5 "ShaHe":["TangShen","SunWuKong"], 6 "TianTong":["Lianjia","WoJia"] 7 }, 8 "ChaoYang":{ 9 "WangJing":["BenChi","MoMo"], 10 "GuoMao":["CICC","Hp"], 11 "DongZhiMen":["Advent","FeiXin"], 12 }, 13 "HaiDing":{ 14 "Kazihuo":["Richer"] 15 } 16 }, 17 "ShanDong":{ 18 "DeZhou":{}, 19 "QingDao":{}, 20 "JiNan":{} 21 }, 22 "JiangXi":{ 23 "NanChang":["JXUT","BanFen"], 24 "JiAn":["WuJi","SuanLuob"] 25 } 26 } 27 28 Flag = False 29 while not False: 30 for key in data: 31 print(key) 32 choice = input("Choose your want>>>>:") 33 if choice in data: 34 while not False: 35 for key2 in data[choice]: 36 print(key2) 37 choice2 = input("Choose your want>>>>:") 38 if choice2 in data[choice]: 39 while not False: 40 for key3 in data[choice][choice2]: 41 print(key3) 42 tishi = input("The end,Please input b to back!!!") 43 if tishi == 'b': 44 break 45 elif tishi =='q': 46 exit() 47 elif choice2 == 'q': 48 exit() 49 elif choice2 == 'b': 50 break 51 elif choice == 'q': 52 break
========================================
作者:罗穆瑞
转载请保留此段声明,且在文章页面明显位置给出原文链接,谢谢!
==============================================================================
^_^ 如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,您的“推荐”将是我最大的写作动力 ^_^
==============================================================================
浙公网安备 33010602011771号