美文网首页
Python简易购物车实现

Python简易购物车实现

作者: 南少cc | 来源:发表于2017-04-17 17:41 被阅读0次

    <pre>shopping_list = []
    product_list = [('iphone',5800),
    ('mac pro',9800),
    ('bike',800),
    ('watch',10600),
    ('coffee',31),
    ('python',120)]
    salary = input('请输入工资:')
    if salary.isdigit():
    salary = int(salary)
    while True:
    for index,item in enumerate(product_list):
    print(index,item)
    user_choice = input('选择要买嘛》》》》?:')
    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('添加:%s到购物车,余额为:\033[31;1m%s\033[0m' % (p_item,salary))
    else:
    print('\033[41;1m你的余额%s已不足\033[0m' % salary)
    else:
    print('商品不存在!')
    elif user_choice == 'q':
    print('--------- shopping list -------')
    for p in shopping_list:
    print(p)
    print('你的余额%s' % salary)
    exit()
    else:
    print('输入有误!')</pre>

    相关文章

      网友评论

          本文标题:Python简易购物车实现

          本文链接:https://www.haomeiwen.com/subject/aaotzttx.html