函数

函数:体现了一种封装的思想,把一个功能封装在一个函数中,使用的时候直接调用这个函数,不用写重复的代码

python函数格式  def 函数名(形式参数):
                               代码块

 Python函数:先定义在使用

用函数写的一个小dome:计算器

#计算器
def add(a,b):
    """
    加法运算
    :param a: 参数1
    :param b: 参数2
    :return: 返回
    """
    return a + b
def sub(a,b):
    return a - b
def div(a,b):
    return a / b
def Chen(a,b):
    return a * b
#说明文档
help(add)
#help + 函数名
print("-----------------------------<<计算器>>-------------------------");
while 1:
    print("1,加法")
    print("2,减法")
    print("3,除法")
    print("4,乘法")
    print("5,退出")
    z = int(input("请选择你要执行的项目:"))
    x = int(input("请输入数字:"))
    y = int(input("请输入数字:"))

    if z == 1:
        temp = add(x,y)
        print("和:%d"%temp)
    elif z == 2:
        temp = sub(x,y)
        print("差:%d"%temp)
    elif z == 3:
        temp = div(x,y)
        print(temp)
    elif z == 4:
        temp = Chen(x,y)
        print(temp)
    else:
        break

 

                       

posted @ 2020-07-19 21:47  小白yuan  阅读(177)  评论(0)    收藏  举报