1. 程式人生 > >User_Login_Register_Shopping+裝飾器 2.0

User_Login_Register_Shopping+裝飾器 2.0

AD lac sid == 記錄 reg 用戶名 %d ebo

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/5/27 0027 14:07
# @Author : Anthony.Waa
# @Site :
# @File : User_Login_Register_Shopping 1.0.py
# @Software: PyCharm

# 購物車
def shopping():
‘‘‘
:return:返回商品清單
‘‘‘

# 原始購物清單
goods = [
{"name": "電腦", "price": 1999},
{"name": "鼠標", "price": 10},
{"name": "遊艇", "price": 20},
{"name": "美女", "price": 998},
]

# 購物車
shopping_car = {}
# 購物車遍歷
shopping_car_list = []

# 商品個數
good_count = 1

# 商品單價
shopping_price = {}

# 輸入不合法是打印
def return_error():
print("\033[1;31m 輸入不合法,請重新輸入 \033[0m")

# 打印購物車商品和剩余金額
def end_shopping():
# 獲取原始列表中的名稱和價格
for list_goods_price in goods:
shopping_price[list_goods_price[‘name‘]] = list_goods_price[‘price‘]
with open(‘shopping_list.txt‘, ‘w‘, encoding=‘utf-8‘) as shoppingf:
# 獲取購物車中的名稱和個數,並從中找到與原始列表中名稱所對應的價格
for list_shopping_name, list_shopping_count in shopping_car.items():
shoppingf.writelines(‘商品名稱:{0},商品個數:{1}個,商品價格:{2}元\n‘.format(list_shopping_name, list_shopping_count,
shopping_price[list_shopping_name]))

for list_name in shopping_car:
print(‘\033[1;32m 商品名稱:{0} 商品個數:{1} \033[0m‘.format(list_name, shopping_car[list_name]))
print("\033[1;32m 剩余金額為:{0}\033[0m".format(all_money))

while True:
# 用戶輸入總資產
all_money = input("\033[1;32m 請輸入總資產: \033[0m").strip()
if all_money.isdigit():
all_money = int(all_money)
# 顯示購物商品列表
print(‘\033[1;32m ========== 商 品 清 單 ========== \033[0m‘.center(20))
for good_index, good in enumerate(goods, 1):
print(‘\033[1;32m {0} {1} {2} \033[0m‘.format(good_index, good[‘name‘],
good[‘price‘]).center(20))
print(‘\033[1;32m 選擇"q"退出 \033[0m‘.center(20))
while True:
# 選擇商品序號,或選擇退出購物
choice_count = input("\033[1;32m 請輸入商品序號: \033[0m").strip()
if choice_count.isdigit():
choice_count = int(choice_count)
# 商品序號小於商品列表長度
if choice_count > 0 and choice_count <= len(goods):
good_price = goods[choice_count - 1][‘price‘]
# 用戶資產大於商品價格,否則提示充值
if all_money > good_price:
good_name = goods[choice_count - 1][‘name‘]
all_money -= good_price
# 商品是否存在購物車中
if good_name not in shopping_car:
shopping_car[good_name] = good_count
else:
for shopping_index, shopping_name in enumerate(shopping_car):
if shopping_name == good_name:
shopping_car[shopping_name] += 1

end_shopping()


else:
print(‘\033[1;31m 余額不足,請充值: \033[0m‘.center(17))
add_money = input("請輸入總資產:").strip()
if add_money.isdigit():
add_money = int(add_money)
all_money += add_money
print(‘\033[1;32m 充值成功,剩余金額為: \033[0m‘.center(17), all_money)
continue
elif choice_count.lower() == ‘q‘:
print("\033[1;32m 購物結束,歡迎下次光臨 \033[0m")
end_shopping()
break
return_error()


def user_login():
count = 0
while True:
print(‘-‘ * 55)
username = input(‘請輸入用戶名:‘).strip()
password = input(‘請輸入密碼:‘).strip()
with open(‘register.txt‘, ‘r‘, encoding=‘utf-8‘) as f1:
for line in f1:
line_list = line.strip().replace(‘,‘, ‘,‘).split(‘,‘)
if username in line_list[0] and password in line_list[1]:
print(‘登陸成功‘)
status_dict[‘username‘] = username
status_dict[‘status‘] = True
return True
else:
print(‘輸入錯誤‘)
count += 1
if count == 3:
print(‘用戶名或密碼輸入錯誤超過3次,已鎖定.‘)
exit()


def user_regiter(*args, **kwargs):
while True:
print(‘-‘ * 55)
print(‘提示:用戶名任意填寫,不能超過8位,密碼為數字,字母,下劃線,不能超過16位‘)
username = input(‘請輸入用戶名:‘).strip()
password = input(‘請輸入密碼:‘).strip()
if len(username) > 8 and len(password) > 16:
print(‘用戶名或密碼長度不符合規定,請重新輸入...‘)
continue
else:
with open(‘register.txt‘, ‘r‘, encoding=‘utf-8‘) as f1:
for line in f1:
line_list = line.strip().replace(‘,‘, ‘,‘).split(‘,‘)
if username in line_list[0] and password in line_list[1]:
print(‘該用戶已存在,請重新輸入‘)
break
else:
with open(‘register.txt‘, ‘a+‘, encoding=‘utf-8‘) as f2:
f2.writelines(‘{0},{1}{2}‘.format(username, password, ‘\n‘))
print(‘註冊成功..‘)
print(‘自動登錄中...‘)
status_dict[‘username‘] = username
status_dict[‘status‘] = True
print(‘登錄成功,請選擇...‘)
return True


def article():
print(‘-‘ * 55)
print(‘歡迎進入文章視圖‘)


def notebook():
print(‘-‘ * 55)
print(‘歡迎進入日記視圖‘)


def comment():
print(‘-‘ * 55)
print(‘歡迎進入評論視圖‘)


def collection():
print(‘-‘ * 55)
print(‘歡迎進入收藏視圖‘)


def cancellation():
print(‘-‘ * 55)
print(‘用戶已註銷‘)
status_dict[‘status‘] = False
return False


def sign_out():
print(‘-‘ * 55)
print(‘程序已退出‘)
exit()


choice_menus = {
1: user_regiter,
2: user_login,
3: article,
4: notebook,
5: comment,
6: collection,
7: shopping,
8: cancellation,
9: sign_out,
}
print(‘-‘ * 55)

show_menu = {
1: ‘用戶註冊‘,
2: ‘用戶登錄‘,
3: ‘文章頁面‘,
4: ‘日記頁面‘,
5: ‘評論頁面‘,
6: ‘收藏頁面‘,
7: ‘商城‘,
8: ‘註銷‘,
9: ‘退出程序‘,
}

# 全局用戶登錄狀態
status_dict = {
‘username‘: None,
‘status‘: False,
}

# 打印主菜單
print(‘ 歡迎登陸博客園‘)
for menu_index, menu in enumerate(show_menu, 1):
print(‘ {0}、{1}‘.format(menu_index, show_menu[menu]))
print(‘-‘ * 55)
print(‘提示:請登錄後再嘗試其他選項...‘)

import time

# 用戶訪問內容
visit_website = []


# 打印用戶選項日誌
def outside(log):
def into_log(*args, **kwargs):
with open(‘loging.log‘, ‘a+‘, encoding=‘utf-8‘) as log1:
start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
log(*args, **kwargs)
stop_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
log1.writelines(
‘\n用戶:{0},登錄時間:{1},退出時間:{2},使用函數:{3},歡迎{4}用戶,訪問{5}‘.format(status_dict[‘username‘], start_time,stop_time, choice_menus[choice_menu],
status_dict[‘username‘], visit_website[-1]))
return into_log



# 運行主體
while True:
choice_menu = input(‘請輸入以上任一選項:‘).strip()
if choice_menu.isdigit():
choice_menu = int(choice_menu)
# 添加用戶的訪問內容至visit_website列表記錄
if choice_menus[choice_menu] not in visit_website:
visit_website.append(show_menu[choice_menu])


@outside
def print_main():
if choice_menu == 9:
choice_menus[9]()
else:
if 0 < choice_menu <= len(choice_menus):
# 判斷用戶若沒有登錄,只能輸入選項為1、2
if status_dict[‘status‘] == False:
if 0 < choice_menu <= 2:
choice_menus[choice_menu]()
# 登錄成功後,選擇其他選項
else:
choice_menus[choice_menu]()
else:
print(‘選項不存在,請重新輸入...‘)


print_main()
else:
print(‘輸入錯誤,請重新輸入...‘)

User_Login_Register_Shopping+裝飾器 2.0