Python-Day3 購物系統,python-day3

來源:互聯網
上載者:User

Python-Day3 購物系統,python-day3

要求:
使用者入口
1、商品資訊存在檔案裡
2、已購商品,餘額記錄。

商家入口
可以添加商品,修改商品價格


Code:
商家入口:
 1 # Author:P J J 2  3 import os 4  5 ps = ''' 6 1 >>>>>> 修改商品 7 2 >>>>>> 添加商品 8 按q為退出程式 9 '''10 11 # 開啟兩個檔案,f檔案為原來存取商品檔案,f_new檔案為修改後的商品檔案12 f = open('commodit', 'r', encoding='utf-8')13 f_new = open('commodit_update', 'w+', encoding='utf-8')14 file_list = f.readlines()15 16 # 列印商品資訊17 while True:18     productslist = []19     # 從商品檔案中讀取出來的資料存放到productslist列表裡20     for line in file_list:21         productname = line.strip().split()22         productname, oldprice = line.strip("\n").split()23         productslist.append([productname, int(oldprice)])24     choose = input("%s請選擇:" %ps)25     if choose =='1':26         for index, item in enumerate(productslist):27             print(index, item)28         productindex = input("請輸入要修改價格的商品序號:")29         if productindex.isdigit():30             productindex = int(productindex)31         while True:32             print('要修改商品資訊:', productslist[productindex])33             price = input("請輸入要修改的價格:")34             if price.isdigit():35                 price = int(price)36                 productslist[productindex][1]=price37                 break38             else:39                 print("請正確的輸入價格!")40                 continue41 42         #已經修改好的商品列表迴圈寫入f_new檔案夾43 44         for products in productslist:45             insert_data = "%s %s" %(products[0],products[1])46             f_new.write(insert_data+'\n')47         print("商品價格已經修改!")48         # 替換原來的檔案49         f_new = open('commodit_update', 'r', encoding='utf-8')50         data = f_new.readlines()51         f = open('commodit', 'w+', encoding='utf-8')52         for line in data:53             f.write(line)54         f.close()55         f_new.close()56         #刪除替換檔案57         os.remove('commodit_update')58     elif choose =='2':59         # 添加商品60         f = open('commodit', 'a+', encoding='utf-8')61         pricename = input("請輸入商品名:")62         while True:63             price = input("請輸入商品價格:")64             if price.isdigit():65                 f.writelines('%s %s\n' % (pricename, price))66                 break67             else:68                 print('輸入錯誤請重新輸入!')69                 continue70         f.close()71         continue72     elif choose =='q':73         break74     else:75         print("輸入錯誤請重新輸入")76         continue
買家入口:
 1 # Author:P J J 2  3 productslist = [] 4 f = open('commodit','r',encoding='utf-8') 5 for line in f: 6     productname,price = line.strip('\n').split() 7     productslist.append((productname,int(price))) 8  9 print(productslist)10 shopping_list = []11 12 salary = input("請輸入你的現金:")13 if salary.isdigit():14     salary = int(salary)15     while True:16         # for item in productslist:17         #     print(productslist.index(item),item)18         for index,item in enumerate(productslist):19             print(index,item)20         #判斷使用者要輸入21         user_choice = input("請選擇要買什啥>>>:")22         if user_choice.isdigit():23             user_choice = int(user_choice)24             if user_choice < len(productslist) and user_choice >= 0:25                 p_item = productslist[user_choice]26                 if p_item[1] <= salary: #買得起27                     shopping_list.append(p_item)28                     salary -=p_item[1]29                     print("加入 %s 購物車你的餘額是\033[31;1m%s\033[0mRMB" %(p_item,salary))30                 else:31                     print("\033[32;1m 你的餘額只剩[%s]RMB啦,還買個毛線\033[0m " %salary)32             else:33                 print("\033[41;1m您輸入的商品不存在,請重新輸入!\033[0m")34         elif user_choice == 'q':35             print("----shopping_list----")36             for p in shopping_list:37                 print(p)38             print("你的餘額:\033[31;1m%s\033[0mRMB" %salary)39             #簡單的餘額記錄40             f = open('salary','w+',encoding='utf-8')41             f.writelines(str(salary))42             f.close43             exit()44         else:45             print("錯誤選項")

 

操作流程:

 


我的目錄:

 


1、建立一個檔案,名為 commodit 商品排列格式如下(自己可以更改商品名字或者價格)

2、運行商家入口測試功能

我們輸入1,首先測試修改商品:


輸入0,修改第一個商品價格為400:

退出後查看 commodit 檔案看見商品價格已經修改


--------------------------------------------------
測試添加商品:

查看 commodit檔案


測試買家入口:

有錢了那就先來一台Iphone

再來60包爐石卡包

按q退出結賬!並且有一個salary檔案記錄餘額

此時目錄會多一個salary檔案

點開就能看到餘額已經被記錄

感想:

   做完這個購物車花了2天,其實也不是整天都在弄,畢竟還要上課、學習。這次主要是熟悉檔案的操作和一些基礎知識的回顧,寫完後能跑出功能就很開心了.因為中途遇到很多困難,解決了一個又出一個問題,不過通過上網尋找和詢問還是解決了。寫完後感覺很low,畢竟自己敲得太少還是要多加練習,這個程式挺適合入門或者學完檔案操作的親來練練手。對了,自己測試程式的時候還出現bug,不過影響不是特別大,只是不要多次修改價格就行,這個問題我也想過怎麼解決,就是把列表清空,這樣資料就不會讀出2遍,但又發現第二次讀取的資料不是更改後的資料,我就在想,列表有沒有重新整理,清空功能。這裡先留下這個問題吧。功能已經都實現了,但寫的真的很low,等以後再掌握了新姿勢,回頭來改改!包括前面做的登入還有三級菜單!如果有跟我一樣初學的可以一起學習Alex老師的python課程,如果有大神看到,並且能耐心看完,請大神再多指點指點小弟!

好了,Life is short,use python!



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.