Python實(shí)現(xiàn)購物系統(tǒng)(示例講解)
要求:
用戶入口
1、商品信息存在文件里
2、已購商品,余額記錄。
商家入口
可以添加商品,修改商品價(jià)格
Code:
商家入口:
# Author:P J J
import os
ps = '''
1 >>>>>> 修改商品
2 >>>>>> 添加商品
按q為退出程序
'''
# 打開兩個(gè)文件,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 = []
# 從商品文件中讀取出來的數(shù)據(jù)存放到productslist列表里
for line in file_list:
productname = line.strip().split()
productname, oldprice = line.strip("\n").split()
productslist.append([productname, int(oldprice)])
choose = input("%s請(qǐng)選擇:" %ps)
if choose =='1':
for index, item in enumerate(productslist):
print(index, item)
productindex = input("請(qǐng)輸入要修改價(jià)格的商品序號(hào):")
if productindex.isdigit():
productindex = int(productindex)
while True:
print('要修改商品信息:', productslist[productindex])
price = input("請(qǐng)輸入要修改的價(jià)格:")
if price.isdigit():
price = int(price)
productslist[productindex][1]=price
break
else:
print("請(qǐng)正確的輸入價(jià)格!")
continue
#已經(jīng)修改好的商品列表循環(huán)寫入f_new文件夾
for products in productslist:
insert_data = "%s %s" %(products[0],products[1])
f_new.write(insert_data+'\n')
print("商品價(jià)格已經(jīng)修改!")
# 替換原來的文件
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("請(qǐng)輸入商品名:")
while True:
price = input("請(qǐng)輸入商品價(jià)格:")
if price.isdigit():
f.writelines('%s %s\n' % (pricename, price))
break
else:
print('輸入錯(cuò)誤請(qǐng)重新輸入!')
continue
f.close()
continue
elif choose =='q':
break
else:
print("輸入錯(cuò)誤請(qǐng)重新輸入")
continue
買家入口:
# Author:P J J
productslist = []
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("請(qǐng)輸入你的現(xiàn)金:")
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("請(qǐng)選擇要買什啥>>>:")
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啦,還買個(gè)毛線\033[0m " %salary)
else:
print("\033[41;1m您輸入的商品不存在,請(qǐng)重新輸入!\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("錯(cuò)誤選項(xiàng)")
操作流程:

我的目錄:

1、新建一個(gè)文件,名為 commodit 商品排列格式如下(自己可以更改商品名字或者價(jià)格)
2、運(yùn)行商家入口測(cè)試功能

我們輸入1,首先測(cè)試修改商品:

輸入0,修改第一個(gè)商品價(jià)格為400:

退出后查看 commodit 文件看見商品價(jià)格已經(jīng)修改

--------------------------------------------------
測(cè)試添加商品:

查看 commodit文件

測(cè)試買家入口:

有錢了那就先來一臺(tái)Iphone

再來60包爐石卡包

按q退出結(jié)賬!并且有一個(gè)salary文件記錄余額

此時(shí)目錄會(huì)多一個(gè)salary文件

點(diǎn)開就能看到余額已經(jīng)被記錄

感想:
做完這個(gè)購物車花了2天,其實(shí)也不是整天都在弄,畢竟還要上課、學(xué)習(xí)。這次主要是熟悉文件的操作和一些基礎(chǔ)知識(shí)的回顧,寫完后能跑出功能就很開心了.因?yàn)橹型居龅胶芏嗬щy,解決了一個(gè)又出一個(gè)問題,不過通過上網(wǎng)查找和詢問還是解決了。寫完后感覺很low,畢竟自己敲得太少還是要多加練習(xí),這個(gè)程序挺適合入門或者學(xué)完文件操作的親來練練手。對(duì)了,自己測(cè)試程序的時(shí)候還出現(xiàn)bug,不過影響不是特別大,只是不要多次修改價(jià)格就行,這個(gè)問題我也想過怎么解決,就是把列表清空,這樣數(shù)據(jù)就不會(huì)讀出2遍,但又發(fā)現(xiàn)第二次讀取的數(shù)據(jù)不是更改后的數(shù)據(jù),我就在想,列表有沒有刷新,清空功能。這里先留下這個(gè)問題吧。功能已經(jīng)都實(shí)現(xiàn)了,但寫的真的很low,等以后再掌握了新姿勢(shì),回頭來改改!包括前面做的登錄還有三級(jí)菜單!如果有跟我一樣初學(xué)的可以一起學(xué)習(xí)Alex老師的python課程,如果有大神看到,并且能耐心看完,請(qǐng)大神再多指點(diǎn)指點(diǎn)小弟!
好了,Life is short,use python!
以上這篇Python實(shí)現(xiàn)購物系統(tǒng)(示例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)字典序列ChainMap
容器數(shù)據(jù)類型包括數(shù)組list,字典dict以及元組tuple等。本篇主要介紹了ChainMap字典序列的使用,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
解決Python 爬蟲URL中存在中文或特殊符號(hào)無法請(qǐng)求的問題
今天小編就為大家分享一篇解決Python 爬蟲URL中存在中文或特殊符號(hào)無法請(qǐng)求的問題。這種問題,初學(xué)者應(yīng)該都會(huì)遇到,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python Celery多隊(duì)列配置代碼實(shí)例
這篇文章主要介紹了Python Celery多隊(duì)列配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Django實(shí)現(xiàn)的自定義訪問日志模塊示例
這篇文章主要介紹了Django實(shí)現(xiàn)的自定義訪問日志模塊,結(jié)合具體實(shí)例形式分析了Django針對(duì)日志的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

