当前位置: 首页 > article >正文

python基础项目

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):
                break

        while 1:
            email = input('请输入邮箱:')
            if phone_email_regex('^[a-zA-Z0-9_]{5,13}@[a-zA-Z0-9]{2,}\.com$', email):
                break

        address = input('请输入地址:')
        users[name] = [phone, email, address]
        print('添加成功')
    pass

def 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()
                pass
            case 2:
                pass
            case 3:
                pass
            case 4:
                pass

menu()
print(users)

2.奖客富翁

from input_util import *
import random
import csv

users = {}
login_user = None

def 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()]:
                break
        users[name] = [pwd, card]
        print(f'{name}注册成功,请登录后参与抽奖')
    pass

def 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]]
        return
    print('用户名或密码错误')

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()
                return
            case 1:
                reg_user()
                pass
            case 2:
                login()
                pass
            case 3:
                lottery()
                pass

menu()

3.汽车租赁

from input_util import *
import csv

cars = {}

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 = 1
    for data in cars['轿车']:
        msg += f'{index}.' + ' '.join(data) + '\n'
        index += 1
    msg += '请选择:'

    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.7
    elif days > 30:
        money *= 0.8
    elif days > 7:
        money *= 0.9

    print(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:
                return
            case 1:
                mini_car()
                pass
            case 2:
                pass

menu()

4.练习


http://www.kler.cn/a/455277.html

相关文章:

  • Wend看源码-Java-集合学习(List)
  • Vue3入门(9)
  • .NET能做什么?全面解析.NET的应用领域
  • 【速成51单片机】1.已经学过stm32如何快速入门51单片机——软件下载与安装
  • 【再谈设计模式】享元模式~对象共享的优化妙手
  • 解决 vue3 中 echarts图表在el-dialog中显示问题
  • 5G/4G工业边缘网关 边缘计算 硬核配置强算力
  • 使用three.js 实现vr全景图展示,复制即可用
  • C语言的语法
  • 集成 jacoco 插件,查看单元测试覆盖率
  • 【Agent】AutoGen Studio2.0开源框架-UI层环境安装+详细操作教程(从0到1带跑通智能体AutoGen Studio)
  • httpclient POST 工具方法
  • python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
  • <代码随想录> 算法训练营-2024.12.27
  • Linux 硬盘扩容 分区 挂载
  • 蓝牙BLE开发——解决iOS设备获取MAC方式
  • FreePBX修改IP地址和端口以及添加SSL证书开启HTTPS访问
  • Ajax总结
  • HTMLCSS:超级酷炫的3D照片墙
  • 项目三:信号源的FPGA实现
  • 蓝牙链路控制(Link Control)命令概览
  • 【音视频工具系列】streamEye 工具分析 H264 码流详细教程
  • Scala的统计
  • 转运机器人推动制造业智能化转型升级
  • Vue3响应式数据: 深入分析Ref与Reactive
  • 系统压力测试助手——stress-ng