Python 3は簡単なショッピングカートのプログラムを実現します

4776 ワード

# Python3           
Goods = [["1.iphone X",7399],["2.vivo X21",3598],["3.  Python      ",72],["4.      ", 179],["5.        ", 1999]]
Havebuys = [] #       
salarytem = input("           :")
if not salarytem.isdigit():
    exit()
salary = int(salarytem)
yuer = salary #  
for i in Goods:
    print(i)

buyindex = ""
while True:
    buy = input("  'q'  ,          :")

    if buy.isdigit():
        buyindex = int(buy)
        if buyindex < 1 or buyindex > len(Goods):
            print("        ,     ...")
            continue
    elif buy == "q":
        if len(Havebuys) < 1:
            print("         ")
            exit()
        print("----------------      -------------------")
        for j in Havebuys:
            print(j)
        print("           。     :%f " % yuer)
        exit()
    price = Goods[buyindex-1][1]
    if (price > yuer):
        print("\033      ,  :%f\033,       ..." % yuer)
    elif (price <= yuer):
        yuer = yuer - price
        Havebuys.append(Goods[buyindex-1])
        print("    ,     :
"
+ Goods[buyindex-1][0])

出力結果:
           :10000
['1.iphone X', 7399]
['2.vivo X21', 3598]
['3.  Python      ', 72]
['4.      ', 179]
['5.        ', 1999]
  'q'  ,          :1
    ,     :
1.iphone X
  'q'  ,          :3
    ,     :
3.  Python      
  'q'  ,          :4
    ,     :
4.      
  'q'  ,          :2
      ,  :2350.000000...
  'q'  ,          :q
----------------      -------------------
['1.iphone X', 7399]
['3.  Python      ', 72]
['4.      ', 179]
           。     :2350.000000