1.联系人案例
# 导入的模块
from input_util import *
import re
import csv# 定义一个变量保存文件读取的信息
users = {}# 封装读取文件的函数
def reader_file(path_name: str = './python基础/2024-11-15python基础项目/data/a.csv') -> None:try:with open(path_name, encoding = 'utf-8') as file:csv_file = csv.reader(file)next(csv_file) # 跳过表头for data in csv_file:users[data[0]] = data[1: ]except Exception as e:print(e)# 校验手机号、邮箱是否符合
def phone_email_regex(regex: str, data: str) -> bool:return True if re.match(regex, data) else False# 编写添加功能 (python 中调用一个函数需要这个函数在其上面存在)
def add_user() -> None:name = input('请输入联系人姓名:')if name in users:print('联系人已存在')else:while 1:phone = input('请输入电话:')if phone_email_regex('^(\+86[ \-])?1[3-9][0-9]{9}$', phone):breakwhile 1:email = input('请输入邮箱:')if phone_email_regex('^[a-zA-Z0-9_]{5,13}@[a-zA-Z0-9]{2,}\.com$', email):breakaddress = input('请输入地址:')users[name] = [phone, email, address]print('添加成功')passdef save_user(path_name: str = './python基础/2024-11-15python基础项目/data/a.csv') -> None:try:with open(path_name, encoding = 'utf-8', mode = 'w', newline = '') as file:csv_file = csv.writer(file)csv_file.writerow(['姓名', '手机号', '邮箱',' 地址'])for k, v in users.items():csv_file.writerow([k] + v)except Exception as e:print(e)# 通过函数设计完成增删改查的功能(目前只完成增功能,后面补充)
def menu() -> None:reader_file()msg = ['1.添加联系人', '2.查看联系人', '3.修改联系人', '4.删除联系人', '0.退出', '请选择:']while 1:key = input_int('\n'.join(msg), 0, 4)match key:case 0:save_user()return # return 的权限比 break 大,break 只是结束当前循环,return 是直接结束当前函数case 1:add_user()passcase 2:passcase 3:passcase 4:passmenu()
print(users)
2.奖客富翁
from input_util import *
import random
import csvusers = {}
login_user = Nonedef reader_user(path_name: str = './python基础/2024-11-15python基础项目/data/user.csv') -> None:try:with open(path_name, encoding = 'utf-8') as file:csv_file = csv.reader(file)next(csv_file)for data in csv_file:users[data[0]] = [data[1], int(data[-1])]except Exception as e:print(e)def reg_user() -> None:name = input_str('请输入用户名:', 1)if name in users:print('用户已存在')else:pwd = input_str('请输入密码:', 1)while 1:card = random.randint(1000, 9999)if card not in [i[-1] for i in users.values()]:breakusers[name] = [pwd, card]print(f'{name}注册成功,请登录后参与抽奖')passdef login() -> None:name, pwd = input_str('请输入用户名:', 1), input_str('请输入密码:', 1)global login_user # 声明要用全局变量if name in users and users[name][0] == pwd:print(f'{name}登录成功')login_user = [name, users[name][-1]]returnprint('用户名或密码错误')def save_user(path_name: str = './python基础/2024-11-15python基础项目/data/user.csv') -> None:try:with open(path_name, encoding = 'utf-8', mode = 'w', newline = '') as file:csv_file = csv.writer(file)csv_file.writerow(['用户名', '密码', '卡号'])for k, v in users.items():csv_file.writerow([k] + [str(i) for i in v])except Exception as e:print(e)# 抽奖
def lottery() -> None:if login_user:n = random.randint(0, 9)if n == login_user[-1] % 10:a = '恭喜你中奖了'else:a = '你没有中奖'print(f'{a}\n中奖号码是:{n},您的卡号是:{login_user[-1]}')else:print('请先登录,再来抽奖')def menu() -> None:msg = ['1.注册', '2.登录', '3.抽奖', '0.退出', '请选择:']reader_user()while 1:key = input_int('\n'.join(msg), 0, 3)match key:case 0:save_user()returncase 1:reg_user()passcase 2:login()passcase 3:lottery()passmenu()
3.汽车租赁
from input_util import *
import csvcars = {}def reader_data(path_name: str = './python基础/2024-11-15python基础项目/data/car.csv') -> None:try:with open(path_name, encoding = 'utf-8') as file:csv_file = csv.reader(file)next(csv_file) # 跳过表头for data in csv_file:if data[0] in cars:cars[data[0]].append(data[1: ])else:cars[data[0]] = [data[1: ]]except Exception as e:print(e)def mini_car() -> None:msg = ''index = 1for data in cars['轿车']:msg += f'{index}.' + ' '.join(data) + '\n'index += 1msg += '请选择:'key = input_int(msg, 1, index - 1)days = input_int('请输入租赁的天数:', 1)car = cars['轿车'][key - 1]money = days * int(car[-1])if days > 150:money *= 0.7elif days > 30:money *= 0.8elif days > 7:money *= 0.9print(f'您租赁的车辆信息是:{car[0]}{car[1]}车牌号是:{car[2]}, 每天租金:{car[-1]}, 您租赁的天数是:{days}, 您本次一共消费:{money}RMB')def menu() -> None:reader_data()while 1:key = input_int('1.轿车\n2.客车\n0.退出\n请选择:', 0, 2)match key:case 0:returncase 1:mini_car()passcase 2:passmenu()
4.练习