1. 程式人生 > >python實現簡單的循環購物車小功能

python實現簡單的循環購物車小功能

iphone6s opp lose pre 選擇 小功能 alt else ret

python實現簡單的循環購物車小功能

技術分享
# -*- coding: utf-8 -*-
__author__ = hujianli

shopping = [
    ("iphone6s", 5000),
    ("book python", 81),
    ("iwach", 3200),
    ("電視機", 2200)
]

def zero(name):
    if len(name) == 0:
        print("\033[31;1m您的輸入不能為空,清重新輸入~~\033[0m")
        exit()
    else:
        return True

def crre(name): if int(name) not in range(len(shopping)): print("\033[31;1m您輸入了錯誤的序列號,請重新輸入~~\033[0m") exit() else: return True gongzi = input("請輸入您目前的工資:") if not gongzi.isdigit(): crre() elif len(gongzi) == 0: zero() else: gongzi = int(gongzi) print("
=========================================") print("===要購買的列表清單如下: ") for i,j in enumerate(shopping): print(i, j[0], j[1]) shopping_cart = [] while True: choice = input("請輸入你要購買的商品,輸入q或者exit退出選擇回到購物車列表: ") if choice.isdigit(): zero(choice) crre(choice) choice
= int(choice) goods = shopping[choice] if gongzi < int(goods[1]): gongzi -= goods[1] print("===您的工資余額不夠使用了,還差\033[1;31m{}\033[0m請努力掙錢===.".format(abs(gongzi))) else: print("您選購的商品:\033[1;32m %s \033[0m 已經加入到購物車.." % (goods[0])) shopping_cart.append(goods) gongzi -= goods[1] print("您的工資余額還剩下:\033[30;42m %s\033[0m"% (gongzi)) elif (choice == "q") or (choice == "exit"): break print(\033[30;42m你已購商品列表\033[0m.center(42,-)) for i,j in enumerate(shopping_cart): print("\033[1;32m" + str(i+1) + " " + str(j[0]) + " " + str(j[1]) + "\033[0m")
View Code

python實現簡單的循環購物車小功能