购物车程序
需求
1.启动程序后,让用户输入工资,然后打印商品列表
2.允许用户根据商品编号购买商品
3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4.可随时退出,退出时,打印已购买商品和余额
‘’’
_username=‘cml’
_password=‘123’
username=input(“会员卡账号:”)
password=input(“会员卡密码:”)
if username == _username and password == _password:
print(“登陆成功”)
else:
print(“账号或密码错误,请重新运行”)
exit()
goods = [(‘iphone’,6000),(‘ipad’,5000),(‘bicycle’,800)]
shopping_list = []
income = int(input(“请输入您的工资:”))
while True:
for i,item in enumerate(goods):#生成商品列表
print(i,item)#生成商品列表
print(“选择q退出程序”)
choice = input(“选择要买的商品编号:”)
if choice.isdigit():#检查参数c是否为阿拉伯数字0到9(检查参数是否为十进制数字字符)
choice=int(choice)
if choice<len(goods) and choice>=0:#len(goods) goods中元素的个数 choice<len(goods)即选择的选项少于元素个数
p_item=goods[choice]
if p_item[1]<=income:#p_item[0] 输出(iphone ,ipad ,bicycle) #p_item[1] 输出(6000, 5000, 800)
shopping_list.append(p_item)#p_item 输出(‘iphone’,6000),(‘ipad’,5000),(‘bicycle’,800) p_item[0]输出商品名称 p_item[1]输出商品价格
income-=p_item[1]
print(“亲亲,您所购买的”,p_item,“已加入购物车”,“账户余额:”,income)
else:
print(“警告!!!账户余额:”,income,">>>您的余额已不足,请充值后再来,或者选择其他商品进行购买!")
else:
print(“您选择的商品”,choice,“不存在,请重新进行选取!”)
elif choice == ‘q’:
print(’--------------shopping list---------------’)
for product in shopping_list:
print(“商品:”,product)
print(“您的账户余额:”, income)
print(" 购物愉快,欢迎下次再来~ ")
exit()
2016-12-17
最新推荐文章于 2024-11-13 17:26:18 发布