Python實現(xiàn)購物系統(tǒng)(示例講解)
要求:
用戶入口
1、商品信息存在文件里
2、已購商品,余額記錄。
商家入口
可以添加商品,修改商品價格
Code:
商家入口:
# Author:P J J
import os
ps = '''
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 = []
# 從商品文件中讀取出來的數(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請選擇:" %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
#已經(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("商品價格已經(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("請輸入商品名:")
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 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("請輸入你的現(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("請選擇要買什啥>>>:")
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("錯誤選項")
操作流程:

我的目錄:

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

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

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

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

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

查看 commodit文件

測試買家入口:

有錢了那就先來一臺Iphone

再來60包爐石卡包

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

此時目錄會多一個salary文件

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

感想:
做完這個購物車花了2天,其實也不是整天都在弄,畢竟還要上課、學習。這次主要是熟悉文件的操作和一些基礎知識的回顧,寫完后能跑出功能就很開心了.因為中途遇到很多困難,解決了一個又出一個問題,不過通過上網(wǎng)查找和詢問還是解決了。寫完后感覺很low,畢竟自己敲得太少還是要多加練習,這個程序挺適合入門或者學完文件操作的親來練練手。對了,自己測試程序的時候還出現(xiàn)bug,不過影響不是特別大,只是不要多次修改價格就行,這個問題我也想過怎么解決,就是把列表清空,這樣數(shù)據(jù)就不會讀出2遍,但又發(fā)現(xiàn)第二次讀取的數(shù)據(jù)不是更改后的數(shù)據(jù),我就在想,列表有沒有刷新,清空功能。這里先留下這個問題吧。功能已經(jīng)都實現(xiàn)了,但寫的真的很low,等以后再掌握了新姿勢,回頭來改改!包括前面做的登錄還有三級菜單!如果有跟我一樣初學的可以一起學習Alex老師的python課程,如果有大神看到,并且能耐心看完,請大神再多指點指點小弟!
好了,Life is short,use python!
以上這篇Python實現(xiàn)購物系統(tǒng)(示例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決Python 爬蟲URL中存在中文或特殊符號無法請求的問題
今天小編就為大家分享一篇解決Python 爬蟲URL中存在中文或特殊符號無法請求的問題。這種問題,初學者應該都會遇到,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05

