函数定义和简单调用
公司新来部分新员工, 定义函数并循环打印欢迎新员工的消息
- employees = {"Chris": "NOC", "David": "PJM", "Brain": "SA", "John": "UX"}
- def introduce_employee(employee, post):
- print("Welcome our new colleague" + username + ", his post is" + job + "!")
- for key, value in employees.items():
- username = key
- job = value
- introduce_employee(username, job)
输出
- Welcome our new colleague Chris, his post is NOC !
- Welcome our new colleague David, his post is PJM !
- Welcome our new colleague Brain, his post is SA !
- Welcome our new colleague John, his post is UX !
- View Code
函数使用默认值
- def print_employees(employee, post="NOC"):
- print("Welcome our new colleague" + employee + ", his post is" + post + ".")
- print_employees("David", "UX")
- print_employees("Chris", "PJM")
- print_employees("Brain")
- print_employees("John")
输出
- Welcome our new colleague David, his post is UX.
- Welcome our new colleague Chris, his post is PJM.
- Welcome our new colleague Brain, his post is NOC.
- Welcome our new colleague John, his post is NOC.
来源: http://www.bubuko.com/infodetail-3331130.html