1.需求
数据结构:
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
......
]
功能要求:
基础要求:
1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
扩展需求:
1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
2、允许查询之前的消费记录
2.普通流程图
# -*- coding:utf-8 -*-
product = [['Iphone8', 6888], ['MacPro', 14800], ['小米6', 2499], ['Coffee', 31], ['Book', 80], ['Nike Shoes', 799]]
shopping_cart = []
flag = False # 标志位
while not flag:
print("----------商品列表 --------")
for index, item in enumerate(product):
msg = "%s. %s %s" % (index, item[0], item[-1])
print(msg)
choice = input("输入你要买的商品编号|退出q :")
if choice.isdigit():
choice = int(choice)
if choice < len(product):
shopping_cart.append(product[choice])
print('-----你购买了',product[choice])
else:
print('你输入的商品不存在')
elif choice == 'q':
if len(shopping_cart) > 0:
print("------你的购物车---------")
for index, item in enumerate(shopping_cart):
msg = "%s. %s %s" % (index, item[0], item[-1])
print(msg)
flag = True
# break
else:
print('你输入的有误,请重新输入')
3.流程图
4.基本需求版本
#-*- coding:utf-8 -*-
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}
]
shopping_cart = []
_username = 'alex'
_password = '123'
while True: # 用户名密码循环1
username = input("请您输入用户名:").strip()
password = input("请您输入密码:").strip()
if username == _username and password == _password:
print("