您的位置:首页 > 编程语言 > Python开发

[ Python -1 ] 简易购物车程序

2017-06-08 11:14 323 查看
练习:

  1. 要求用户输入总资产,例如:2000

  2. 显示商品列表,让用户根据序号选择商品,加入购物车

  3. 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。

# -*- coding: utf-8 -*-
goods_list = [
['computer', 5000],
['apple', 500],
['pen', 50],
]
salary = float(input('enter your salary:'))
while True:
for index, item in enumerate(goods_list, 1):
print(index, item)
choice = input('enter your choice:')
if choice == 'q':
break
if choice.isdigit() == False:
print('\033[31;1m输入编号错误,请重新输入\033[1m')
elif int(choice) > len(goods_list) or int(choice) < 1:
print('\033[31;1m编号不在商品列表中\033[1m')
else:
choice_buy = int(choice) -1
if salary >= goods_list[choice_buy][1]:
print('\033[32;1m购买成功.\033[1m')
salary = salary - goods_list[choice_buy][1]
else:
print('\033[31;1m购买失败\033[1m')
break


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: