# 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