我是靠谱客的博主 受伤草莓,最近开发中收集的这篇文章主要介绍Python小作业二(购买物品),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

要求:

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

     2、有物品菜单及价格。

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

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

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


逻辑图:

wKioL1fJKsiyyBzrAAERg0M7oTk589.png-wh_50


代码如下:

#!/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 "33[32;1mWelcome to materials  Supermarket,enjoy your self33[0m"
print ""
while True:
    salary = raw_input("33[33;1mPlease input your salary:33[0m").strip()
    if  not salary.isdigit():continue
    #if not salary:continue
    break
count = 0
form=[]
while True:
    if int(salary) < 50:
        print "33[31;1mYour salary not enough allow you consume,Say Good bay33[0m"
        if form == []:
                print "33[33;1myou get nothing33[0m"
                sys.exit()
        else:
                print "33[33;1mYour buy follows:33[0m"
                print list3
                print "33[32;1mYou remain %s33[0m" %salary
                sys.exit()
    print "33[32;1mFollow is Our Supermarket support goods,please input quence number to select articles33[0m"
    print list
    print "33[34;1mYour Left is %s" %salary
    list2 = raw_input("33[32;1mPlease input your choice list-id:").strip()
    if not list2.isdigit() or int(list2) < 0 or int(list2) > 7:
        print "33[33;1myour input is not legal,please input again(1~7)33[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 "33[33;1mYou got This:%s" %article
        print list3
        while True:
            A = raw_input("33[32;1mWould you want to buy another articles(Y|N):33[0m")
            if A == "Y" or A == "y":
                    break
            elif A == "N" or A == "n":
                    print "33[33;1mSay good bey,thankYou,your buy article is follow list:33[0m"
                    print list3
                    print "33[32;1mYou remain %s33[0m" %salary
                    sys.exit()
            else:
                    print "You input is not incorrect"
                    continue


转载于:https://blog.51cto.com/liyuanchuan8/1845613

最后

以上就是受伤草莓为你收集整理的Python小作业二(购买物品)的全部内容,希望文章能够帮你解决Python小作业二(购买物品)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部