您好,登錄后才能下訂單哦!
#author:yangxiao
'''
購物車需求
用戶入口:
1.商品信息存在文件里
2.已購商品,余額記錄。第一次啟動程序時需要記錄工資,第二次啟動程序時談出上次余額
3.允許用戶根據商品編號購買商品
4.用戶選擇商品后,檢測是否夠,夠就直接扣款,不夠就提醒
5.可隨時退出,退出時,打印移購買商品和余額
商家入口:
1.可以添加商品,修改商品價格
'''
import os,sys
def user_shopping():
while True:
user_salary = input('請輸入你的充值金額:')
if user_salary.isdigit():
user_salary = int(user_salary)
user_shop_file.write(login_name + '\t')
user_shop_file.write(str(user_salary) + '\n')
for item in product_lists:
print(product_lists.index(item),item)
p_index = product_lists.index(item)
while True:
user_choice = input('請輸入你要選擇的商品:')
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice <= p_index or user_choice >= 0:
p_item = product_lists[user_choice]
p_item[1] = int(p_item[1])
if user_salary >= p_item[1]:
user_salary -= p_item[1]
global shop_lists
shop_lists.append(p_item)
user_shop_file.seek(0)
user_shop_file.write(login_name + '\t')
user_shop_file.write(str(user_salary) + '\n')
print('商品%s已加入購物車,您還剩%s錢'%(p_item[0],user_salary))
else:
print('您的金額不錯,請去充值')
print('您已購得'.center(30,'*'))
print(shop_lists)
sys.exit()
else:
print('請輸入范圍內的商品:')
elif user_choice == 'q':
sys.exit('退出程序')
else:('其輸入正確的商品編號')
else:
print('請輸入正確的格式')
def user_top():
while True:
user_salary = input('請輸入你要充值的金額:')
if user_salary.isdigit():
user_salary = int(user_salary)
user_salary += int(shop[1])
user_shop_file.seek(0)
user_shop_file.write(login_name + '\t')
user_shop_file.write(str(user_salary) + '\n')
for item in product_lists:
print(product_lists.index(item),item)
p_index = product_lists.index(item)
while True:
user_choice = input('請輸入你要購買的商品:')
user_choice = int(user_choice)
if user_choice <= p_index or user_choice >= p_index:
p_item = product_lists[user_choice]
p_item[1] = int(p_item[1])
if p_item[1] <= user_salary:
user_salary -= p_item[1]
global shop_lists
shop_lists.append(p_item)
user_shop_file.seek(0)
user_shop_file.write(login_name + '\t')
user_shop_file.write(str(user_salary) + '\n')
print('商品%s已加入購物車,您還剩%s錢'%(p_item[0],user_salary))
else:
print('您的金額不錯,請去充值')
print('您已購得'.center(30,'*'))
print(shop_lists)
sys.exit()
else:
print('請輸入正確的商品范圍')
else:
print('請輸入爭取的格式的格式')
user_name = ''
shop_lists = []
product_lists = []
def product_shop():
user_product_list_file = open('product_file.txt','r+',encoding ='UTF-8')
product_list_item = user_product_list_file.readlines()
for i in product_list_item:
i = eval(i)
product_lists.append(i)
product_shop()
print(product_lists)
user_shop_file =open('user_shop_file.txt','r+')
user_shop_list =user_shop_file.readlines()
for i in user_shop_list:
shop = i.split()
if len(user_shop_list) == 0:
login_name = input('請輸入用戶名:')
user_info_file = open('user_info_file.txt','r+')
user_info_list = user_info_file.readlines()
for a in user_info_list:
a = a.split()
if login_name in a:
while True:
login_pwd = input('請輸入密碼')
if login_pwd == a[1]:
print('歡迎進入購物商城')
user_shopping()
else:
print('你輸入的密碼不爭取')
else:
sys.exit('用戶名不存在')
else:
login_name = input('請輸入用戶名:')
user_info_file = open('user_info_file.txt','r+')
user_info_list = user_info_file.readlines()
for a in user_info_list:
a = a.split()
if login_name in a:
while True:
login_pwd = input('請輸入密碼')
if login_pwd == a[1]:
print('歡迎進入購物商城')
user_top()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。