python 簡易購物車

簡單的購物車小程序

1、啓動程序後,輸入用戶名密碼後,讓用戶輸入工資,然後打印商品列表

2、允許用戶根據商品編號購買商品

3、用戶選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒

4、可隨時退出,退出時,打印已購買商品和餘額

5、在用戶使用過程中, 關鍵輸出,如餘額,商品已加入購物車等消息,需高亮顯示
#!/usr/bin/env python

# -*- coding: utf-8 -*-
username = 'admin'
password = '123456'
shopping_menu = []
goods = [['電腦', 19999], ['鼠標', 20], ['美女', 9999], ['電視', 1234], ['手機', 2500]]
_username = input("username:")
_password = input("password:")
if _username == username and _password == password:
    print("----Welcome", username, "----")
    pay = int(input("你的工資是:"))
    while True :
        YN = 1;  #判斷是否能買
        print("----商品列表如下----")
        for index, p in enumerate(goods):
            print("%s. %s    %s" % (index, p[0], p[1]))
        choice = input("輸入你想買的商品編號:")
        if choice.isdigit():
            choice = int(choice)
            if choice >= 0 and choice < len(goods):
                if pay <goods[choice][1] :
                    print("對不起你的餘額不足以買下此商品,請選擇其他商品")
                    YN = 0;
                if YN ==1 :
                    shopping_menu.append(goods[choice])
                    pay = pay - goods[choice][1]
                    print("\033[41;36m 您已成功選擇購買 \033[0m", (goods[choice]), "\033[41;36m 剩餘餘額:\033[0m", pay)
            else:
                print("商品不存在,請重新選擇")
        elif choice == 'q':
            if len(shopping_menu) > 0:
                print("------您已將以下商品加入購物車------")
                for index, p in enumerate(shopping_menu):
                    print("%s.  %s    %s" % (index, p[0], p[1]))
                print("\033[41;36m您的餘額爲:\033[0m", pay)
                exit()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章