>>>Python函数中的参数利用:(写日志)
def write_log(username,operation):
'''
写日志函数
:param username:用户名
:param operation:用户的操作信息
:return:
'''
w_time = time.strftime('%Y-%m-%d %H:%M:%S')
with open(LOG_FILENAME,'a+') as fw:
log_content = '%s %s %s
'%(w_time,username,operation)
fw.write(log_content)
调用时:
def query_product():
'''
查询商品
:return:
'''
products_dic = read_file(PRODUCT_FILENAME)
p_name = n_input('请输入你要查询的商品名称:')
if p_name in products_dic:
p_id = products_dic[p_name]['id']
p_price = products_dic[p_name]['price']
msg = '商品名称是:【%s】,商品id是【%s】,商品价格是【%s】' % (p_name, p_id, p_price)
print(msg)
write_log(username,msg)
else:
print('你输入的商品不存在!')
>>>Python函数中的return:
函数遇到return立即结束;
调用完函数之后,返回计算的结果;
函数没有返回值的时候,默认返回none
def n_input(msg): # 输入的时候去空
'''
就是把input函数又重新给它封装了一次
:param msg: 提示信息
:return:
'''
res = input(msg).strip()
return res
调用时:
p_name = n_input('请输入你要删除的商品名称:')