整體思路: 1.用戶功能:購買、顯示餘額、列表清單、輸入 2.商家功能:修改和添加商品 創建兩個介面: 用戶: 商家: ...
整體思路:
1.用戶功能:購買、顯示餘額、列表清單、輸入
2.商家功能:修改和添加商品
創建兩個介面:
用戶:
#Author: Gordon
#讀取文檔,生成goods
f = open('goods.txt',mode='r+',encoding='utf-8')
line = f.readline()
goods = eval(line)
user_goods ={}
print("商品:價格:",str(goods))
#輸入工資,購買商品
salary = int(input("請輸入你的工資: "))
while True:
wants = input("請輸入你要購買的商品: ")
if wants in goods:
user_goods[wants] = 1
salary = salary - goods[wants]
print("你的餘額還有: %d" % salary)
elif wants == 'q':
break
else:
print("對不起,我們沒有這件商品")
#顯示已經購買信息和餘額
print("你的購物車:",str(user_goods))
print("餘額:%s"% salary)
商家:
#Author: Gordon
#step1:讀取文件,將商品信息保存到goods字典里
f = open('goods.txt',mode='r+',encoding='utf-8')
lines = f.readline()
print(lines)
goods = eval(lines)
print("商品數量為:%d" % len(goods))
print("商品為:" ,goods)
#step2:詢問店主是要上貨,還是修改商品價格,執行相應操作
info = '''
#a:添加商品
#u:修改商品價格
#q:退出
'''
print(info)
while True:
print("請問你是要添加商品或者修改商品:")
order = input("請輸入命令:a or u: ")
if order == 'a':
add_goods_name = input("請輸入要添加的商品名稱:")
add_goods_prise = int(input("請輸入添加商品的價格:"))
goods[add_goods_name] = add_goods_prise
print("添加成功")
elif order == 'u':
update_goods_name = input("請輸入要修改的商品名稱:")
if update_goods_name in goods :
update_goods_prise = input("請輸入商品新的價格:")
goods[update_goods_name] = update_goods_prise
print("更新成功")
else:
print("對不起,你沒有這件商品!")
elif order == 'q':
break
else:
print("命令錯誤!")
#保存進文件里
goods = str(goods)
f.seek(0,0)
f.write(goods)
f.close()
最後商城介面:
#Author: Gordon
rule = input("請輸入你的角色:")
if rule == 's':
import shop
shop
elif rule == 'c':
import costomers
costomers
else:
pass