简单的购物车小程序
1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
#!/usr/bin/env python
# -*- coding: utf-8 -*-
username = 'admin'
password = '123456'
shopping_menu = []
goods = [['电脑', 19999], ['鼠标', 20], ['美女', 9999], ['电视', 1234], ['手机', 2500]]
_username = input("username:")
_password = input("password:")
if _username == username and _password == password:
print("----Welcome", username, "----")
pay = int(input("你的工资是:"))
while True :
YN = 1; #判断是否能买
print("----商品列表如下----")
for index, p in enumerate(goods):
print("%s. %s %s" % (index, p[0], p[1]))
choice = input("输入你想买的商品编号:")
if choice.isdigit():
choice = int(choice)
if choice >= 0 and choice < len(goods):
if pay <goods[choice][1] :
print("对不起你的余额不足以买下此商品,请选择其他商品")
YN = 0;
if YN ==1 :
shopping_menu.append(goods[choice])
pay = pay - goods[choice][1]
print("\033[41;36m 您已成功选择购买 \033[0m", (goods[choice]), "\033[41;36m 剩余余额:\033[0m", pay)
else:
print("商品不存在,请重新选择")
elif choice == 'q':
if len(shopping_menu) > 0:
print("------您已将以下商品加入购物车------")
for index, p in enumerate(shopping_menu):
print("%s. %s %s" % (index, p[0], p[1]))
print("\033[41;36m您的余额为:\033[0m", pay)
exit()