购物车小程序

Posted on 2018-03-02 17:10  甜心卜乙  阅读(194)  评论(0)    收藏  举报

  需求:启动程序后提示用户输入工资,打印商品列表,允许用户根据商品编码购买商品,用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒,可随时退出,退出时打印已购买商品和余额

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#author:hanly


salary = int(raw_input('please input your salary:'))

cargo_list = [['IphoneX',6000],['milk',60],['bike',800]]
want_to_buy_list = []
while True:
    print '商品列表'.center(50, '*')
    for index,cargo in enumerate(cargo_list):
        print  index+1, '-->', cargo
    want_to_buy = raw_input('please input the number you want(input q to quit):')
    if want_to_buy=='q':
        print 'you have been quit'
        print '购买信息'.center(50, '*')
        print 'your shopping cart has goods blow'
        for i in want_to_buy_list:
            print i
        print '\033[31;1myour mony now is %s \033[0m'% salary
        exit()
    elif want_to_buy.isdigit():
        want_to_buy = int(want_to_buy)

        if want_to_buy<0 or want_to_buy>len(cargo_list):
            print '输入有误请重新输入'
            continue

        temp_list = cargo_list[want_to_buy-1]

        if salary <temp_list[1]:
            print 'this cargo need more mony,the surplus mony is not enough'

        else:
            salary = salary - temp_list[1]
            want_to_buy_list.append(temp_list)
            print 'your current mony is \033[31;1m%s\033[0m'%salary
    else:
        print '输入有误请重新输入'