#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 1.列舉你常見的內置函數。
"""
強制轉換:int() / str() / list() / dict() / tuple() / set() / bool()
數學相關:sum() / max() / min() / divmod() / float() / abs()
輸入輸出:input() / print()
其餘:len() / open() / type() / id() / range()
"""
# 2.列舉你常見的內置模塊?
'''
json / getpass / os / sys / random / hashlib / copy / shutil / time
'''
# 3.json序列化時,如何保留中文?
'''
import json
a = '你好'
s = json.dumps(a,ensure_ascii=False)
print(s)
'''
# 4.程序設計:用戶管理系統# 功能:# 1.用戶註冊,提示用戶輸入用戶名和密碼,而後獲取當前註冊時間,最後將用戶名、密碼、註冊時間寫入到文件。# 2.用戶登陸,只有三次錯誤機會,一旦錯誤則凍結帳戶(下次啓動也沒法登陸,提示:用戶已經凍結)。"""from datetime import datetimeimport osUSER = {}def user(): ''' 註冊用戶 :return: ''' username = input('註冊用戶名:') with open('4用戶註冊.txt', 'r', encoding='utf-8') as f: for line in f: if line.split('_')[0].strip() == username: return '賬戶已存在!' pwd = input('密碼:') sj = datetime.now().strftime('%Y-%m-%d %H:%M:%S') msg = '_'.join([username, pwd, sj, True]) with open('4用戶註冊.txt', 'a', encoding='utf-8') as f: f.write(msg + '\n') return '註冊成功!'def login(): ''' 登錄 :return: ''' while 1: username = input('用戶名:') with open('4用戶註冊.txt', 'r', encoding='utf-8') as f2: status = False for line in f2: if line.split('_')[0] == username: if line.split('_')[-1].strip() == 'False': return '賬戶已鎖定!' status = True break if not status: return '用戶不存在!' pwd = input('密碼:') with open('4用戶註冊.txt', 'r', encoding='utf-8') as f: for line in f: if line.split('_')[0] == username and line.split('_')[1] == pwd: USER[username] = 0 return '登錄成功' if USER.get(username) == None: USER[username] = 0 if USER[username] < 3: USER[username] += 1 if not USER[username] < 3: with open('4用戶註冊.txt', 'r', encoding='utf-8') as f1,open('4用戶註冊(改).txt', 'w', encoding='utf-8') as f2: for line in f1: if line.split('_')[0] == username: new_line = line.replace('True', 'False') f2.write(new_line) else: f2.write(line) os.remove('4用戶註冊.txt') os.rename('4用戶註冊(改).txt', '4用戶註冊.txt') return '輸入錯誤超過3次,鎖定帳號!' print('登陸失敗請重試!')print(user())print(login())"""# 5.有以下文件,請經過分頁的形式將數據展現出來。【文件很是小】# 商品|價格# 飛機|1000# 大炮|2000# 迫擊炮|1000# 手槍|123# ...'''def func(): f = open('5-6商品列表.txt', 'r', encoding='utf-8') a = f.read() # 所有讀到內存 lst = a.split('\n') max_page, mowei = divmod(len(lst), 3) # 最大頁,最後一頁條數(總條數,每頁條數) if mowei > 0: max_page += 1 while 1: user = input('要查看第幾頁(N/n退出):') if user.upper() == 'N': return if not user.isnumeric() or int(user) not in range(1, max_page+1): print('輸入有誤!請從新輸入!') continue start = (int(user)-1)*3 end = (int(user))*3 data = lst[start+1:end+1] print(lst[0]) for i in data: print(i.strip()) if not (int(user)>max_page or int(user)<1): print('當前第%s頁,共%s頁.' % (user,max_page))func()'''# 6.有以下文件,請經過分頁的形式將數據展現出來。【文件很是大】# 商品|價格# 飛機|1000# 大炮|2000# 迫擊炮|1000# 手槍|123"""def func(): f = open('5-6商品列表.txt', 'r', encoding='utf-8') a = f.readlines() # 全部行組成一個列表 print(a) max_page, mowei = divmod(len(a), 3) # 最大頁,最後一頁條數(總條數,每頁條數) if mowei > 0: max_page += 1 while 1: user = input('要查看第幾頁(N/n退出):') if user.upper() == 'N': return if not user.isnumeric() or int(user) not in range(1, max_page+1): print('輸入有誤!請從新輸入!') continue start = (int(user)-1)*3 end = (int(user))*3 data = a[start+1:end+1] print(a[0].strip()) for i in data: print(i.strip()) if not (int(user)>max_page or int(user)<1): print('當前第%s頁,共%s頁.' % (user,max_page))func()"""# 7.程序設計:購物車# 有以下商品列表 GOODS_LIST,用戶能夠選擇進行購買商品並加入到購物車 SHOPPING_CAR 中且能夠選擇要購買數量,購買完成以後將購買的所# 有商品寫入到文件中【文件格式爲:年_月_日.txt】。# 注意:重複購買同一件商品時,只更改購物車中的數量。"""SHOPPING_CAR = {} # 購物車GOODS_LIST = [ {'id': 1, 'title': '飛機', 'price': 1000}, {'id': 3, 'title': '大炮', 'price': 1000}, {'id': 8, 'title': '迫擊炮', 'price': 1000}, {'id': 9, 'title': '手槍', 'price': 1000},] # 商品列表from datetime import datetimedef shopping(): while 1: for i in GOODS_LIST: print('序號:' + str(i['id']) + ' 商品:' + i['title'] + ' 價格:' + str(i['price'])) user = input('請輸入序號加入到購物車(N/n退出):') if user.upper() == 'N': return '退出' pand = False for j in GOODS_LIST: if int(user) == j['id']: pand = True if SHOPPING_CAR.get(j['title']) == None: SHOPPING_CAR[j['title']] = 0 break if not pand: print('輸入錯誤!') continue while 1: count = input('請選擇數量:') if not count.isdigit(): print('請輸入阿拉伯數字!') continue SHOPPING_CAR[j['title']] += int(count) date = datetime.now().strftime('%Y_%m_%d') with open("%s.txt" % date, 'w', encoding='utf-8') as f: f.write(str(SHOPPING_CAR)) break print('已添加進購物車!') print(SHOPPING_CAR)a = shopping()print(a)"""