首页 > 技术文章 > 函数知识

wangguoyuan-09 2017-06-13 10:40 原文

1.函数的定义,用def 函数名():

2.必填参数

3.可变参数

4.关键字参数

 

5.必填参数,可变参数,关键字参数,默认参数一起用

6.关键字调用

7.函数的返回值

8.全局变量和局部变量

9.下面是一个详细的案例

# 写一个商品系统,要求如下:
# #1、先登录,登录的用户名和密码都是写在文件里面的
# #1、读文件、字符串分割
# #2、登录成功的话,再去操作,添加、查询商品,添加商品也是写在文件里面
# #1、读写文件,
# # 字典添加元素,取元素,
# # str()强制类型转换,
# # eval()#用它把字符串转成字典
# '''
# {
# 'mac':{
# 'id':1,
# 'price':2222.22
# },
# 'iphone':{
# 'id':2,
# 'price':22222
# }
# }
#
# {
# 'mac':{
# 'id':1,
# 'price':2222.22
# },
# 'iphone':{
# 'id':2,
# 'price':22222
# },
# 'shubao':{
# }
# }
#
# '''
import time
USER_FILENAME='users'
LOG_FILENAME='file_log'
PRODUCT_FILENAME='products'
def read_file(filename):
with open(filename,'a+') as fr:
fr.seek(0)
content=fr.read()
if len(content):
return eval(content)
return {}

def write_file(filename,content):
with open(filename,'a+') as fw:
fw.seek(0)
fw.truncate()
fw.write(str(content))

def write_log(username,operation):
w_time=time.strftime('%Y-%m-%d %H%M%S')
with open(LOG_FILENAME,'a+') as fw:
log_content='%s%s%s\n'%(w_time,username,operation)
fw.write(log_content)

def is_price(s):
s=str(s)
if s.count('.')==1:
s1=s.split('.')
left=s1[0]
right=s1[1]
if left.startswith('-') and left.count('-')==1 and right.isdigit():
lleft=left.split('-')[1]
if lleft.isdigit():
return False
elif left.isdigit() and right.isdigit():
return True
elif s.isdigit():
s=int(s)
if s!=0:
return True
return False

def login():
print('欢迎登录商品系统'.center(50,'*'))
username=input('请输入用户名:').strip()
password=input('请输入密码: ').strip()
user_dic=read_file(USER_FILENAME)
if username==''or password=='':
pass
else:
if username in user_dic:
if user_dic[username][password]==password:
write_log(username,'登录成功!')
return username
else:
print('密码不正确')
else:
print('用户不存在')

def add_product():
product_dic=read_file(PRODUCT_FILENAME)
p_name=input('请输入商品名称').strip()
p_id=input('请输入商品id').strip()
p_price=input('请输入商品价格').strip()
if p_name!='' and p_id!='' and p_price !='':
if p_name in product_dic:
print("商品也存在")
elif not is_price(p_price):
print('商品价格不合法')
else:
product_dic[p_name]={'id':p_id,'price':p_price}
write_file(PRODUCT_FILENAME,product_dic)
write_log(username,'添加了商品信息,商品名称【%s】,商品id【%s】,商品价格【%s】'%(p_name,p_id,p_price))
print('商品添加成功')
else:
print('商品名称、商品id、商品价格不能为空')

def n_input(msg):
res=input(msg).strip()
return res

def delete_product():
product_dic=read_file(PRODUCT_FILENAME)
p_name=n_input("请输入你要删除的商品名称:")
if p_name!='':
if p_name in product_dic:
product_dic.pop(p_name)
write_file(PRODUCT_FILENAME,product_dic)
print("删除成功")
write_log(username,'删除的【%s】'%(p_name))

else:
print('商品名称不存在')
else:
print("商品名称不能为空")

def check_product():
product_dic=read_file(PRODUCT_FILENAME)
p_name=n_input("请输入要查询的商品名称")
if p_name in product_dic:
p_id=product_dic[p_name]['id']
p_price=product_dic[p_name]['price']
msg='商品名称是【%s】,商品id是【%s】,商品价格是【%s】'%(p_name,p_id,p_price)
print(msg)
write_log(username,msg)
else:
print("你输入的商品名称不存在")


def n_exit():
exit('程序退出')

def add_user():
users_dic = read_file(USER_FILENAME)#获取用户信息
username = input('用户名:').strip()
passwd = input('用户密码:').strip()
blance = input('用户的钱:').strip()
if username != '' and passwd != '' and blance != '':
# if和elif都是条件为真的时候才走的
if username in users_dic:
print('用户名已存在!')
elif not is_price(blance):
# not True是flase,指定走不到这里
# not Flase,就是true,就走这了
print('钱不合法!')
else:
users_dic[username] = {'password': passwd, 'price': blance}
# products是存最新所有商品,给这个字典添加商品
write_file(USER_FILENAME,users_dic)
#调用写文件的函数,把商品信息写入到文件中
write_log(username,'添加了用户信息 用户名【%s】 钱是【%s】'
%(username,blance))
print('用户添加成功')


def delete_user():
users_dic = read_file(USER_FILENAME) # 获取用户信息
username=n_input("请输入要删除的用户名:")
if username !='':
if username in users_dic:
if username !='admin':
users_dic.pop(username)
write_file(USER_FILENAME,users_dic)
print("删除成功")
write_log(username,'删除了用户%s'%username)
else:
print("admin账号不能被删除")
else:
print("用户名不存在")
else:
print("用户名不能为空")

def modify_user():
users_dic = read_file(USER_FILENAME) # 获取用户信息
username=n_input("请输入用户名:")
password = n_input("请输入密码:")
money1 = n_input("请输入用户余额:")
if username !='' and (password !='' or money1 !=''):
if username in users_dic:
if money1 !='':
users_dic[username]['money']=money1
elif password != '':
users_dic[username]['passwd']=password
else:
users_dic[username]['money']=money1
users_dic[username]['passwd']=password
else:
print("用户名不存在")
else:
print('用户名不能为空,密码和金额至少有一个不能为空')

def maneger_user():
choice=n_input('1.添加用户、2.删除用户、3.修改用户,0、退出')
if choice in maneger_user_menu:
maneger_user_menu[choice]()
else:
print('请输入0-3选项')

maneger_user_menu={
"1":add_user,
"2":delete_user,
"3":modify_user,
"0":n_exit

}

product_manger = {
"1":add_product,
"2":delete_product,
"3":check_product,
"0":n_exit,
}

admin_menu = {"4":maneger_user}
admin_menu.update(product_manger)
def welcome():
global username
username=login()
if username:
if username=='admin':
choice=input('1、添加商品,2、删除商品,3、查询商品、4、用户管理、0、退出').strip()
if choice in admin_menu:
admin_menu[choice]()
else:
print('请输入0-4选项')
else:
choice=input('1、添加商品,2、删除商品,3、查询商品、0、退出').strip()
if choice in product_manger:
product_manger[choice]()
else:
print('请输入0-3选项')

welcome()

 

推荐阅读