码迷,mamicode.com
首页 > 编程语言 > 详细

python实现简单的循环购物车小功能

时间:2017-07-05 00:35:21      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:pen   div   orm   book   bre   end   opened   python实现   []   

python实现简单的循环购物车小功能

技术分享
# -*- coding: utf-8 -*-
__author__ = hujianli

shopping = [
    ("iphone6s", 5000),
    ("book python", 81),
    ("iwach", 3200),
    ("电视机", 2200)
]

def zero(name):
    if len(name) == 0:
        print("\033[31;1m您的输入不能为空,清重新输入~~\033[0m")
        exit()
    else:
        return True

def crre(name):
    if int(name) not in range(len(shopping)):
        print("\033[31;1m您输入了错误的序列号,请重新输入~~\033[0m")
        exit()
    else:
        return True



gongzi = input("请输入您目前的工资:")
if not gongzi.isdigit():
    crre()
elif len(gongzi) == 0:
    zero()
else:
    gongzi = int(gongzi)
    print("=========================================")
    print("===要购买的列表清单如下: ")
    for i,j in enumerate(shopping):
        print(i, j[0], j[1])

shopping_cart = []
while True:
    choice = input("请输入你要购买的商品,输入q或者exit退出选择回到购物车列表: ")
    if choice.isdigit():
        zero(choice)
        crre(choice)
        choice = int(choice)
        goods = shopping[choice]
        if gongzi < int(goods[1]):
            gongzi -= goods[1]
            print("===您的工资余额不够使用了,还差\033[1;31m{}\033[0m请努力挣钱===.".format(abs(gongzi)))
        else:
            print("您选购的商品:\033[1;32m %s \033[0m 已经加入到购物车.." % (goods[0]))
            shopping_cart.append(goods)
            gongzi -= goods[1]
            print("您的工资余额还剩下:\033[30;42m %s\033[0m"% (gongzi))
    elif (choice == "q") or (choice == "exit"):
        break

print(\033[30;42m你已购商品列表\033[0m.center(42,-))
for i,j in enumerate(shopping_cart):
    print("\033[1;32m" + str(i+1) + " " + str(j[0]) + " " + str(j[1]) + "\033[0m")
View Code

 

python实现简单的循环购物车小功能

标签:pen   div   orm   book   bre   end   opened   python实现   []   

原文地址:http://www.cnblogs.com/hujianli/p/7118957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!