程序:购物车程序
需求:
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品和余额
=============方法1================双重列表=============方法1============
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Huanglinsheng product_list = [ ("IPhone", 5000), ("mac pro", 8900), ("bick", 800), ("watch", 10600), ("python book", 120), ] shopping_list = [] salary = input("input your salary:") if salary.isdigit(): salary = int(salary) while True: for index,item in enumerate(product_list): print(index,item) ''' for item in product_list: print(product_list.index(item),item) ''' user_choice = input("input the number of your choice:") #输入的是商品序号 if user_choice.isdigit(): user_choice = int(user_choice) # 输入的是存在的商品序号 if user_choice <len(product_list) and user_choice >= 0: p_item = product_list[user_choice] if p_item[1] <= salary:#买的起 shopping_list.append(p_item) salary -= p_item[1] print("Added %s into your car,your courrent balance is 33[31;1m%s 33[0m"%(p_item,salary)) else:#买不起 print("