一個簡單的購物車程式,簡單購物車程式

來源:互聯網
上載者:User

一個簡單的購物車程式,簡單購物車程式

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

  自己寫的:

 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         break12 13     elif somethings[dongxi] == 'KeyError':14         print("Sorry!The product you want is not found.")15         break16 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(f'Your 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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.