zoukankan      html  css  js  c++  java
  • python-函数

    # 函数
    #函数 方法 功能
    #说白了,函数就是把一堆代码组合到一起,变成一个整体。
    #函数不调用不会被执行
    #提高代码的复用性
    #全局变量、局部变量
    def hello():
    print('hello')
    #函数不调用不会被执行
    hello()#调用函数

    def hello(file_name,content=''):#形参,形式参数
    #函数里面的变量都是局部变量
    f=open(file_name,'a+',encoding='utf-8')
    if content:
    f.write(content)
    else:
    f.seek(0)
    res=f.read()
    return res
    f.close
    print(hello('a.txt','hh'))#实参,实际参数
    print(hello('b.txt'))

    #直接写的参数叫位置参数,必填参数
    #默认值参数 ,不是必填的---上面的content=''

    #返回值
    #如果想获取到函数的结果,必须用return
    #如果函数没有写return,那么返回值为none,return不是必须写,需要获取到函数的返回结果才写
    #return 立即结束函数
    #递归:
    # 递归 自己调用自己
    count = 0
    def test1():
    # num = int(input('please enter a number:'))
    global count
    count+=1
    num = 8
    if num % 2 == 0: # 判断输入的数字是不是偶数
    pass
    print(count)
    return test1() # 如果不是偶数的话继续调用自己,输入值
    print(test1()) # 调用test
    # 用递归能实现的用循环都能实现,最好别用递归。
    #递归最多能递归999次。



  • 相关阅读:
    十二月31日
    十二月31号
    10,28
    10,27
    十月26
    十月22
    十月21
    十月二十
    十月16
    0227 数据库的知识
  • 原文地址:https://www.cnblogs.com/hoby2017/p/8269587.html
Copyright © 2011-2022 走看看