zoukankan      html  css  js  c++  java
  • 函数

    # f = open('a.txt','w',encoding='utf-8')
    # f.write()
    # f.close()

    def smile():
    print('哈哈哈')

    # smile()
    #函数必须调用才会执行
    # price = input('请输入价格:')

    def is_float(s): #形式参数
    s = str(s)
    if s.count('.') == 1:
    left,right = s.split('.') #1.74 ['1','74']
    if left.isdigit() and right.isdigit():
    return True
    elif left.startswith('-') and left[1:].isdigit() and right.isdigit():
    return True
    return False



    #判断输入的数据是否是一个小数

    #1.54
    #1、只有一个小数点
    #2、小数点左右是整数


    #-1.34
    #1、只有一个小数点
    #2、小数点右边是整数,小数点左边只有一个负号
    #负号后面是一个整数





    # is_float(1) #实际参数

    #1.

    #函数的返回值

    def calc(a,b):
    result = a+b
    print(result)
    return result

    # sum = calc(1,1)


    def write_file(file_name,content):
    with open(file_name,'w',encoding='utf-8') as fw:
    fw.write(content)


    def read_file(file_name):
    with open(file_name,encoding='utf-8') as fw:
    result = fw.read()
    return result

    read_file('users')

    #在函数里面定义的变量都是局部变量
    #函数里只要遇到return函数立即结束

    # username = input('username:')
    # res = read_file('users')
    #
    # if username in res:
    # print('用户名存在!')
    # else:
    # print('用户名不存在!')


    def more():
    name = 'xiaohei'
    age = 18
    score = 37.5
    return name,age,score

    result = more()
    print(result)

    mingzi,nianling,chengji = more()
    print(mingzi)
    print(nianling)
    print(chengji)

    #默认值参数

    def register(name,sex='女'):

    print('[%s %s]写入数据库'%(name,sex))

    register('贾金菊','男')
    register('贾金菊')

    #一个函数只实现一个功能,不要写太长的在一个函数里面

    import json
    def op_file(file_name,content=None):
    if content:
    with open(file_name,'w',encoding='utf-8') as fw:
    fw.write(content)
    else:
    with open(file_name,encoding='utf-8') as fr:
    return fr.read()

    def op_file_json(file_name,dic=None):
    if dic:
    with open(file_name,'w',encoding='utf-8') as fw:
    json.dump(dic,fw)
    else:
    with open(file_name,encoding='utf-8') as fr:
    result = json.load(fr)
    return result


  • 相关阅读:
    使用Idhttp.get('') 造成假死(堵塞),请问线程idhttp怎么才能做到不出错?
    mysql 修改字段类型
    Delphi完成的断点续传例子 转
    断点续传的例子
    甲状腺癌怎样早发现 可B超检查
    DELPHI高性能大容量SOCKET并发(九):稳定性问题解决
    百度地图信息提示框的修改 转
    delphi 调用百度地图WEBSERVICE转换GPS坐标 转
    delphi 调用百度地图api
    Gedit
  • 原文地址:https://www.cnblogs.com/Dorami/p/11004564.html
Copyright © 2011-2022 走看看