国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現購物系統(示例講解)

2020-01-04 16:44:46
字體:
來源:轉載
供稿:網友

要求:

用戶入口

1、商品信息存在文件里
2、已購商品,余額記錄。

商家入口

可以添加商品,修改商品價格

Code:

商家入口:

# Author:P J Jimport osps = '''1 >>>>>> 修改商品2 >>>>>> 添加商品按q為退出程序'''# 打開兩個文件,f文件為原來存取商品文件,f_new文件為修改后的商品文件f = open('commodit', 'r', encoding='utf-8')f_new = open('commodit_update', 'w+', encoding='utf-8')file_list = f.readlines()# 打印商品信息while True: productslist = [] # 從商品文件中讀取出來的數據存放到productslist列表里 for line in file_list:  productname = line.strip().split()  productname, oldprice = line.strip("/n").split()  productslist.append([productname, int(oldprice)]) choose = input("%s請選擇:" %ps) if choose =='1':  for index, item in enumerate(productslist):   print(index, item)  productindex = input("請輸入要修改價格的商品序號:")  if productindex.isdigit():   productindex = int(productindex)  while True:   print('要修改商品信息:', productslist[productindex])   price = input("請輸入要修改的價格:")   if price.isdigit():    price = int(price)    productslist[productindex][1]=price    break   else:    print("請正確的輸入價格!")    continue  #已經修改好的商品列表循環寫入f_new文件夾  for products in productslist:   insert_data = "%s %s" %(products[0],products[1])   f_new.write(insert_data+'/n')  print("商品價格已經修改!")  # 替換原來的文件  f_new = open('commodit_update', 'r', encoding='utf-8')  data = f_new.readlines()  f = open('commodit', 'w+', encoding='utf-8')  for line in data:   f.write(line)  f.close()  f_new.close()  #刪除替換文件  os.remove('commodit_update') elif choose =='2':  # 添加商品  f = open('commodit', 'a+', encoding='utf-8')  pricename = input("請輸入商品名:")  while True:   price = input("請輸入商品價格:")   if price.isdigit():    f.writelines('%s %s/n' % (pricename, price))    break   else:    print('輸入錯誤請重新輸入!')    continue  f.close()  continue elif choose =='q':  break else:  print("輸入錯誤請重新輸入")  continue

買家入口:

# Author:P J Jproductslist = []f = open('commodit','r',encoding='utf-8')for line in f: productname,price = line.strip('/n').split() productslist.append((productname,int(price)))print(productslist)shopping_list = []salary = input("請輸入你的現金:")if salary.isdigit(): salary = int(salary) while True:  # for item in productslist:  #  print(productslist.index(item),item)  for index,item in enumerate(productslist):   print(index,item)  #判斷用戶要輸入  user_choice = input("請選擇要買什啥>>>:")  if user_choice.isdigit():   user_choice = int(user_choice)   if user_choice < len(productslist) and user_choice >= 0:    p_item = productslist[user_choice]    if p_item[1] <= salary: #買得起     shopping_list.append(p_item)     salary -=p_item[1]     print("加入 %s 購物車你的余額是/033[31;1m%s/033[0mRMB" %(p_item,salary))    else:     print("/033[32;1m 你的余額只剩[%s]RMB啦,還買個毛線/033[0m " %salary)   else:    print("/033[41;1m您輸入的商品不存在,請重新輸入!/033[0m")  elif user_choice == 'q':   print("----shopping_list----")   for p in shopping_list:    print(p)   print("你的余額:/033[31;1m%s/033[0mRMB" %salary)   #簡單的余額記錄   f = open('salary','w+',encoding='utf-8')   f.writelines(str(salary))   f.close   exit()  else:   print("錯誤選項")

操作流程:


Python,購物系統

我的目錄:


Python,購物系統

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

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


Python,購物系統

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


Python,購物系統
 

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


Python,購物系統

退出后查看 commodit 文件看見商品價格已經修改


Python,購物系統

--------------------------------------------------

測試添加商品:


Python,購物系統

查看 commodit文件


Python,購物系統
 

測試買家入口:


Python,購物系統

有錢了那就先來一臺Iphone


Python,購物系統

再來60包爐石卡包


Python,購物系統

按q退出結賬!并且有一個salary文件記錄余額


Python,購物系統

此時目錄會多一個salary文件


Python,購物系統

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

Python,購物系統

感想:

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

好了,Life is short,use python!

以上這篇Python實現購物系統(示例講解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 卫辉市| 壤塘县| 都安| 赫章县| 龙岩市| 远安县| 全椒县| 普格县| 平邑县| 仲巴县| 醴陵市| 晋宁县| 隆昌县| 密山市| 广饶县| 成都市| 遂川县| 司法| 南丹县| 湖口县| 洛浦县| 临颍县| 绍兴市| 娱乐| 定西市| 和政县| 石屏县| 南平市| 罗田县| 改则县| 亚东县| 诸城市| 洛川县| 海南省| 博爱县| 诏安县| 航空| 乌兰浩特市| 南丹县| 绩溪县| 舞阳县|