1. 程式人生 > >一個簡單的購物車程序

一個簡單的購物車程序

while 有感 計算器 git art user img lex bre

  前幾天跟著視頻做了一個簡單的購物車程序,但是沒有做出來,之後又跟著後面的視頻,做了一個,視頻裏面老師也講了,只有不斷的嘗試寫,才能慢慢有感覺,越不寫,越不會。下面是我前幾天寫的購物車和跟著視頻做的購物車的對比:

  自己寫的:

 1 __Author__ = "Zhang Peng"
 2 
 3 salary = input("What is your monthly salary?\n")
 4 somethings={"IPhone":5888,"杯子":8,"眼鏡盒":15,"計算器":38,"鼠標":128,"電腦":7889,"零食":120,}
 5 Shopping_Cart=[]
6 7 while True: 8 dongxi = input("What do you want to buy? \n") 9 10 if dongxi == quit: 11 break 12 13 elif somethings[dongxi] == KeyError: 14 print("Sorry!The product you want is not found.") 15 break 16 17 elif salary - somethings[dongxi] >= 0:
18 Shopping_Cart.append([dongxi,somethings[dongxi]],) 19 salary = salary - somethings[dongxi] 20 21 22 elif salary - somethings[dongxi] < 0: 23 print("The balance you have left is not enough to buy it. Try something else, please.") 24 print("Enter ‘quit‘ to end the program.
") 25 26 27 28 29 print(f你還有{salary}塊錢!) 30 print(fYour shopping list:\n{Shopping_Cart})


  老師寫的:

product_list = [
    (Iphone,5800),
    (Mac Pro,9800),
    (Bike,800),
    (Watch,10600),
    (Coffee,31),
    (Alex Python,120),
]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list):
            #print(product_list.index(item),item)
            print(index,item)
        user_choice = input("選擇要買嘛?>>>:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice >=0:
                p_item = product_list[user_choice]
                if p_item[1] <= salary: #買的起
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) )
                else:
                    print("\033[41;1m你的余額只剩[%s]啦,還買個毛線\033[0m" % salary)
            else:
                print("product code [%s] is not exist!"% user_choice)
        elif user_choice == q:
            print("--------shopping list------")
            for p in shopping_list:
                print(p)
            print("Your current balance:",salary)
            exit()
        else:
            print("invalid option")

?  後面的視頻布置的作業是把購物車進行優化,優化的內容是,分別添加用戶和商家入口,用戶可以顯示自己以前的購物記錄,已經錢包余額;商家可以添加商品和修改商品價格,下面代碼是我自己研究研究寫出來的,後面的保存和調用的方法還是不熟練,寫的比較亂,試了好多遍,還是不行,大家可以看看:

技術分享
product_list=[
    (Iphone,5888),
    (筆記本,12888),
    (手表,10600),
    (電視,3888),
    (,58),
]

id_list=["商家","用戶","shangjia","yonghu"]
id_shopping=input("Please input your ID:\n")

exit_flag=False
        
if id_shopping=="shangjia":
    choice=input("你是否想添加\修改商品?添加商品輸入‘1‘,修改商品輸入‘2‘,退出輸入‘q‘:\n")
    if choice=="1":
        while not exit_flag:
            name=input("請輸入您想添加的商品名稱:\n")
            name2=input("請輸入您添加商品的價格:\n")
            if name=="q" or name2=="q":
                exit_flag=True
            else:
                product_list.append((name,name2),)

    elif choice=="2":
        while not exit_flag:
            name3=input("請輸入您想修改價格的商品名稱:\n")
            name4=input("請輸入您想要修改的價格:\n")
                
            if name3=="q" or name4=="q":
                exit_flag=True  
            else:
                a=product_list.index(name3)
                product_list.remove(name3)
                product_list.index((name3,name4))

    else:
        if choice=="q":
            exit_flag=True
    print(product_list)


elif id_shopping=="yonghu":
    shopping_list=[]
    with open("shopping_list.txt") as f:
        print("你的購物車裏已經有:\n")
        f.read()
    with open("salary.txt") as f:
        salary=f.read()
        print(salary)
    if salary.isdigit():    #isdigit 是判斷輸入的是不是一個數字
        salary=int(salary)
        while True:
            print("我們有一下商品:\n")
            for index,item in enumerate(product_list):
                print(index,item)
            user_choice=input("請問你還想買什麽?:\n")
            if user_choice.isdigit():
                user_choice=int(user_choice)
                if user_choice<len(product_list) and user_choice>0:
                    p_item=product_list[user_choice]
                    if p_item[1]<salary:  #買不起
                        shopping_list.append(p_item)
                        salary -=p_item[1]
                        print("您買的商品已經加入購物車")
                    else:
                        print("沒錢別嘚瑟了")
                else:
                    print("沒有您要的商品")
            elif user_choice == q:
                print("----------shopping list----------")
                for p in shopping_list:
                    print(p)
                    with open("shopping_list.txt","w") as f:
                        f.write("shopping_list")
                print("你還剩余",salary)
                salary.save("salary.txt")
                

            else:
                print("輸入錯誤")

else:
    shopping_list=[]
    salary=input("請輸入你的月薪:\n")
    if salary.isdigit():    #isdigit 是判斷輸入的是不是一個數字
        salary=int(salary)
        while True:
            for index,item in enumerate(product_list):
                print(index,item)
            user_choice=input("請問你想買什麽?:\n")
            if user_choice.isdigit():
                user_choice=int(user_choice)
                if user_choice<len(product_list) and user_choice>0:
                    p_item=product_list[user_choice]
                    if p_item[1]<salary:  #買不起
                        shopping_list.append(p_item)
                        salary -=p_item[1]
                        print("您買的商品已經加入購物車")
                    else:
                        print("沒錢別嘚瑟了")
                else:
                    print("沒有您要的商品")
            elif user_choice == q:
                print("----------shopping list----------")
                for p in shopping_list:
                    print(p)
                shopping_list.save("shopping_list.txt")
                print("你還剩余",salary)
                with open("salary.txt","w") as f:
                    f.write("salary")
                    exit()

            else:
                print("輸入錯誤")
View Code

  有興趣的可以看一下,歡迎踴躍參與。給我寫建議,謝謝!

一個簡單的購物車程序