面向过程编程

面向过程编程

是一种编程思想,核心是过程,即解决问题的步骤

优点:将复杂的问题流程化,进而简单化

缺点:扩展性差(若修改当前程序的某一部分,会导致其他部分同时需要修改)

案例: 注册功能

1.先让用户输入用户名密码,检验合法性,得到合法的用户名密码

2.设计字符串拼接,得到拼接的用户名密码字符串

3.保存用户数据,写入文件,每个用户数据单独存入以用户名为命名的文件

# 创建用户名密码,校验合法性
def get_usn_pwd():
    #验证输入的用户名是否为字母,中文
    while True:
        username = input('create your username:').strip()
        if username.isalpha():
            break
        else:
            print('username is illegal')
    #验证两次输入的密码是否相同
    while True:
        password = input('create your password:').strip()
        r_password =input('create your password:').strip()
        if password == r_password:
            break
        else:
            print('two passwords are different')

    return username , password


# 拼接用户名密码,得到拼接的字符串
def cut_usn_pwd(user,pwd):
    u_p = f'{user}|{pwd}
'
    return u_p


# 保存用户数据,写入文件,每个数据都保存到一个以用户名命名的新文件
def save_data(u_p,user_name):
    with open(f'{user_name}.txt','w',encoding='utf-8')as f:
        f.write(u_p)


def register():
    user,pwd = get_usn_pwd()
    u_p= cut_usn_pwd(user,pwd)
    save_data(u_p,user)


register()
原文地址:https://www.cnblogs.com/littleb/p/11858833.html