Python 2週目の宿題--ショッピングカート【未完成】
難点:何度も購入してバグが出たので、後で最適化しましょう
カートプログラム:1、プログラムを起動した後、ユーザー名のパスワードを入力した後、初めて登録し、ユーザーに給料を入力させ、商品リスト2を印刷させ、ユーザーが商品番号に基づいて商品を購入することを許可する3、ユーザーが商品を選択した後、残高が足りないかどうかを検査し、足りない場合は直接金を引く4、いつでも退出することができ、退出する時、購入した商品と残高5を印刷する.ユーザーの使用中、肝心な出力、例えば残高、商品がカートに入ったなどのメッセージは、6、ユーザーが次回ログインした後、ユーザー名のパスワードを入力して、直接前回の状態に戻る必要があります.
shopping.py実行ファイル【同じディレクトリにdataフォルダを作成】
カートプログラム:1、プログラムを起動した後、ユーザー名のパスワードを入力した後、初めて登録し、ユーザーに給料を入力させ、商品リスト2を印刷させ、ユーザーが商品番号に基づいて商品を購入することを許可する3、ユーザーが商品を選択した後、残高が足りないかどうかを検査し、足りない場合は直接金を引く4、いつでも退出することができ、退出する時、購入した商品と残高5を印刷する.ユーザーの使用中、肝心な出力、例えば残高、商品がカートに入ったなどのメッセージは、6、ユーザーが次回ログインした後、ユーザー名のパスワードを入力して、直接前回の状態に戻る必要があります.
shopping.py実行ファイル【同じディレクトリにdataフォルダを作成】
#!/use/bin/env python
# -*- coding:utf-8 -*-
import os
BASE_DIR = os.path.dirname(__file__) #
DATA_DIR = os.path.join(BASE_DIR,'data') #
def login(): #
username = input(" :")
if user_exit(username): #
while True:
password = input(" :")
new_user_file = os.path.join(DATA_DIR, username)
with open(new_user_file,"r",encoding="utf-8") as f:
f1 = f.read()
if f1.split()[1] == password: #
lishi = input(" , y/n:")
if lishi == 'y' or lishi == 'Y':
with open(new_user_file, "r",encoding="utf-8") as f:
for index, i in enumerate(range(3)):
s = f.readline().strip()
if index == 2:
if s[14:-1] =="": #
print("\033[31;1m \033[0m")
print(s[14:-1])
# print("-----",f.split()[5])
money = int(f1.split()[3])
shopping(username,money,password)
break
else:
print(" , !")
else: #
register(username)
return True
def user_exit(name): #
user_info_file = os.path.join(DATA_DIR,name)
if os.path.exists(user_info_file):
# print(" !")
return True
else:
print(" , 。")
return False
def register(name): #
print(" ",name)
while True:
password = input(" -->:")
if len(password.strip()) > 0:
new_user(name,password)
print(" !")
return login()
else:
print(" , , -->:")
def new_user(name,password): # str
user_money = input(" :")
user_list = '''password %s'''.strip()%password\
+"
"+'''balance %s'''.strip()%user_money\
+"
"+'''shopping_log []'''.strip()
new_user_file = os.path.join(DATA_DIR,name)
with open(new_user_file,"w",encoding="utf-8") as f:
f.write(str(user_list))
def shopping(username,money,password): #
shopping = []
goods = [("IPhone8", 6000), ("book", 20), ("Python", 80), ("JAVA", 65),
("Mac", 7000)]
while True:
for index, goods_list in enumerate(goods):
print(index, goods_list)
user_choice = input(" , q :")
if user_choice.isdigit(): #
user_choice = int(user_choice)
if user_choice < len(goods): # goods
goods_list = goods[user_choice]
if goods_list[1] < money: #
shopping.append(goods_list)
money -= goods_list[1]
print("\033[31;1m :%s, :%s\033[0m" % (goods_list, money))
else: #
print(" ! %s " % (money))
else:
print(" :")
elif user_choice == 'q':
print("-------- ---------")
new_user_file = os.path.join(DATA_DIR, username)
with open(new_user_file, "r",encoding="utf-8") as f:
for index, i in enumerate(range(3)):
s = f.readline().strip()
if index == 2:
s=s[14:-1] #
if s =="": # ,
user_shopping = []
else:
user_shopping = [s]
# ( )
for sp in shopping:
print(sp)
user_shopping.append(sp) #
# print("user_shopping-----",user_shopping)
print(" %s" % (money))
user_list = '''password %s'''.strip() % password+ "
" + '''
balance %s'''.strip() % money+ "
" + '''
shopping_log %s'''.strip()%(user_shopping)
new_user_file = os.path.join(DATA_DIR, username)
with open(new_user_file, "w", encoding="utf-8") as f:
f.write(str(user_list))
exit()
else:
print(" :")
login()