Tianhao Cui

It is about code, it is not about code

  • Home
  • Code
  • Apple
  • Others
All Blogs My Verse

Tianhao Cui

It is about code, it is not about code

  • Home
  • Code
  • Apple
  • Others

Day3-Python-Functions

2020-05-22

Python之路(3)— 函数

依旧是Python基础篇,还是基于上篇文章提到的教程。链接在下

The link of tutorials: Python

在数学中我们总是提到函数,简单的函数,复杂的函数,而我们的程序中函数又是什么呢?函数的作用是什么呢?
个人认为,函数的使用让我们的程序更简洁,更有调理。教程中着重提到了三点函数的特性:

  1. 减少代码的重复性
  2. 代码的可扩展性
  3. 使程序已维护

在编程中,函数就是把一些运算方法包装到一起,当我们需要使用时可以随意调用。

参数

讲到函数,就不能不提参数。在绝大多数情况下,我们对一个函数的调用都需要给这个函数定义参数来达到我们的目的。既然提到了参数,我们又得引出下一个重要的概念。实参和形参

  • 实参:用户给函数的赋予值
    1
    2
    3
    4
    #x和y是形参
    def sum(x,y):
    s=x+y
    return s
  • 形参:定义函数时所添加的变量
    1
    2
    3
    #1和2是实参
    f=sum(1,2)
    print(f)
    引用教程内的一段函数的概念

    函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计。函数就是面向过程的程序设计的基本单元

    Code

    员工信息管理程序

    要求:
  1. 展现员工信息表
  2. 可添加员工
  3. 管理员工信息
  4. 搜索员工信息

应用:

  1. 创建文本文件.txt对数据存储,调用,和更新(id,name,phone,age)
  2. 创建菜单栏实现不同需求
  3. 创建不同函数实现对员工信息的不同加工
  4. 程序可扩展,实现更多需求

总结:

  1. 函数定义,传参,调用得到了练习
  2. 当编写管理员工函数时,实现修改文本制定行

    主程序代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    import sys
    from utility import Add_Employees,Search_By_Age,Manage_Emplpoyees
    from printinfo import Employees_info

    def main():
    while True:
    print("\t\tMenu")
    print("\t********************")
    print("\t1.Add more employees")
    print("\t2.Employees List")
    print("\t3.Search Employees by age")
    print("\t4.Manage Employees")
    choose=input("Enter(press q to quit):").strip()
    if choose=="1":
    Add_Employees()
    if choose=="2":
    Employees_info()
    if choose=="3":
    Search_By_Age()
    if choose=="4":
    Manage_Emplpoyees()
    elif choose=="q":
    exit(0)

    if __name__ == "__main__":
    main()

    添加员工

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    def Add_Employees():
    print("Adding employees...")
    with open('id.txt','a') as a:
    num=len(employee_id)+1
    employee_id.append(num)
    a.write(str(num)+"\n")
    with open('name.txt','a') as a:
    new_employee_name=input("Name:")
    a.write(new_employee_name+"\n")
    employee_name.append(new_employee_name)
    with open('age.txt','a') as a:
    new_employee_age=int(input("Age:"))
    a.write(str(new_employee_age)+"\n")
    employee_age.append(new_employee_age)
    with open('phone.txt','a') as a:
    new_phone_number=input("Phone number:")
    a.write(str(new_phone_number)+"\n")
    phone_number.append(new_phone_number)
    s=input("keep adding more(y/n)")
    if s=="y":
    return Add_Employees()
    else:
    print("done!\nBack to Menu")

    打印信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    def Employees_info():
    with open("id.txt") as f:
    employee_id=f.read().splitlines()
    with open("name.txt") as f:
    employee_name=f.read().splitlines()
    with open("age.txt") as f:
    employee_age=f.read().splitlines()
    with open("phone.txt") as f:
    phone_number=f.read().splitlines()
    print("Employees Info")
    print("ID Name Age Phone")
    for n in range(len(employee_id)):
    print(employee_id[n],
    employee_name[n],
    employee_age[n],
    phone_number[n],sep=' ')

    搜索员工

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    def Search_By_Age():
    int_employee_age=[int(m) for m in employee_age]
    print(int_employee_age)
    print("Serach employees who are older than the input age")
    print(type(int_employee_age[0]))
    age=int(input("Input age:"))
    print("ID***Name")
    for n in range(len(employee_age)):
    if age<int_employee_age[n]:
    print(employee_id[n],employee_name[n],sep=' ')
    else:continue
    print("done!")

    管理员工

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    def Manage_Emplpoyees():
    print("Employees information")
    print("ID***Name***Age***Phone number")
    for i in range(len(employee_id)):
    print(employee_id[i],employee_name[i],employee_age[i],phone_number[i],sep=' ')

    target=input("Please enter the employee's ID to edit: ")
    if(target in employee_id):
    print("found it")
    t=int(target)-1
    print(employee_id[t],employee_name[t],employee_age[t],phone_number[t],sep=' ')
    f=open('age.txt','r')
    a=f.readlines()
    print(a)
    m_age=input("New age: ")
    f=open('age.txt','w')
    for i in a:
    print(i,"***",a)
    f.write(i.replace(employee_age[t],m_age))
    #employee_age[t]=m_age
    f.close()
    employee_age[t]=m_age
    print()
    print("Updated Employees information")
    print("ID***Name***Age***Phone number")
    for i in range(len(employee_id)):
    print(employee_id[i],employee_name[i],employee_age[i],phone_number[i],sep=' ')
    else:
    print("Input Error!")
  • Code
  • Python

扫一扫,分享到微信

微信分享二维码
Python-Basics-Summary
Day2-Python-Basics
© 2021 Tianhao Cui
Hexo Theme Yilia by Litten
  • All Blogs
  • My Verse

tag:

  • Code
  • Blockchain
  • Python
  • Others
  • Interview
  • Apple
  • MacOS
  • Algorithm
  • iPhone

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

And now these three remain:faith,hope,love
But the greatest of these is love