首页 > 代码库 > Python小作业二(购买物品)

Python小作业二(购买物品)

要求:

    1、输入工资后,提供多种物品供选择。

     2、有物品菜单及价格。

     3、选择商品计算用户是否可以支付。

     4、打印用户所购买的物品。

     5、输出用户剩余的钱,问用户是否购物,结束。


逻辑图:

技术分享


代码如下:

#!/usr/bin/env python
from tabulate import tabulate
import time
import sys
goods = [["1","car",20000],["2","iphone",5000],["3","computer",3000],["4","watch",1000],["5","T-short",500],["6","shoes",200],["7","hamburg",50]]
header = ["list","goods","price"]
list = tabulate(goods,header,tablefmt="grid")
print ""
print "\033[32;1mWelcome to materials  Supermarket,enjoy your self\033[0m"
print ""
while True:
    salary = raw_input("\033[33;1mPlease input your salary:\033[0m").strip()
    if  not salary.isdigit():continue
    #if not salary:continue
    break
count = 0
form=[]
while True:
    if int(salary) < 50:
        print "\033[31;1mYour salary not enough allow you consume,Say Good bay\033[0m"
        if form == []:
                print "\033[33;1myou get nothing\033[0m"
                sys.exit()
        else:
                print "\033[33;1mYour buy follows:\033[0m"
                print list3
                print "\033[32;1mYou remain %s\033[0m" %salary
                sys.exit()
    print "\033[32;1mFollow is Our Supermarket support goods,please input quence number to select articles\033[0m"
    print list
    print "\033[34;1mYour Left is %s" %salary
    list2 = raw_input("\033[32;1mPlease input your choice list-id:").strip()
    if not list2.isdigit() or int(list2) < 0 or int(list2) > 7:
        print "\033[33;1myour input is not legal,please input again(1~7)\033[0m"
        continue
    price = goods[int(list2)-1][2]
    article = goods[int(list2)-1][1]
    if salary >= price:
        salary = int(salary) - int(price)
        count += 1
        time2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        price = str(price)
        count = str(count)
        element = [count,time2,article,price]
        count = int(count)
        form.append(element)
        header2 = ["list","Time","Article","Price"]
        list3 = tabulate(form,header2,tablefmt="grid")
        print "\033[33;1mYou got This:%s" %article
        print list3
        while True:
            A = raw_input("\033[32;1mWould you want to buy another articles(Y|N):\033[0m")
            if A == "Y" or A == "y":
                    break
            elif A == "N" or A == "n":
                    print "\033[33;1mSay good bey,thankYou,your buy article is follow list:\033[0m"
                    print list3
                    print "\033[32;1mYou remain %s\033[0m" %salary
                    sys.exit()
            else:
                    print "You input is not incorrect"
                    continue


本文出自 “linux世界” 博客,请务必保留此出处http://liyuanchuan8.blog.51cto.com/6060290/1845613

Python小作业二(购买物品)