要求:
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