天数:第六天
时间:2023年12月20日
内容:
- 2. 调用函数
- 参数值和返回
- 参数:函数执行时,需要从外面调用的数据
- 形参 实参
- 形参和实参个数必须一致
- (普通用法)返回值:执行后返回的结果
- 用法:执行后返回的结果
- def shuchu(x):
- print x
- 这段代码我们直接使用的时候没有问题,比如shuchu(3). 那么也会打印出来3,但是如果我们需要把我们定义的函数输出的结果与一个变量关联的时候,例如
- d=shuchu(3)
- print(d)
- 我们会发现d的值是None
- ---------------------------------------------------------------------------------------------
- def shuchu(x):
- return x
- d=shuchu(3)
- print(d)
- 我们会发现d的值已经是3了, 没有None
- 用法:执行后返回的结果
- (终止嵌套循环) 用法:(当里层的条件满足时,直接跳出所有的for循环层)
- 因为break只能终止当前的for层,但他还会继续外层的for。
- 形参 实参
- 参数:函数执行时,需要从外面调用的数据
- 变量作用域
- 全局变量:能在整个文件中被访问(能在整个文件中被访问)
- 局部变量:定义在函数或类的内部
- 用法:
- 当有一个全局变量a = 10时,局部变量也有一个a = 10,此时在局部的方法里,会有先调用局部的。当此方法结束时,还是以全局变量的 a 为主
- 局部变量变成全局变量,使用global内置函数 . global 对象
- 用法:
第1天到目前天数的综合练习:(下面是我根据练习自己打的代码)
# 大宝的超市开业了,为了更好的管理商品信息,准备开发一个商品管理系统。 # 系统需要用户先登录,再进行操作,其中包含以下功能菜单: # # 1.显示商品列表 # 2.增加商品信息 # 3.删除商品 # 4.设置商品折扣 # 5.修改商品信息 # 6.退出 # # a. 使用列表嵌套字典的方式保存用户数据(包含用户名,密码,姓名); # b. 使用列表嵌套字典的方式保存商品数据(包含编号,名称,价格,折扣); # c. 编写用户登录的函数,返回登录结果; # d. 循环提示菜单,业务完毕时返回主菜单,退出时回到登陆页面; # e. 将功能菜单中的业务功能各自编写到函数中; # f. 用户选择不同业务编号时,调用已经写好的各种函数。
person1 = {'姓名': '张三', '用户名': 1001, '密码': 123}
person2 = {'姓名': '李四', '用户名': 1002, '密码': 123}
person3 = {'姓名': '王五', '用户名': 1003, '密码': 123}
person4 = {'姓名': '赵六', '用户名': 1004, '密码': 123}
person5 = {'姓名': '孙三', '用户名': 1005, '密码': 123}
person = [person1, person2, person3, person4, person5]
item1 = {'编号': '001', '名称': '苹果', '价格': 5, '折扣': 10}
item2 = {'编号': '002', '名称': '香蕉', '价格': 8, '折扣': 10}
item3 = {'编号': '003', '名称': '橘子', '价格': 7, '折扣': 10}
item4 = {'编号': '004', '名称': '柚子', '价格': 12, '折扣': 10}
item5 = {'编号': '005', '名称': '菠萝', '价格': 14, '折扣': 10}
item6 = {'编号': '006', '名称': '西瓜', '价格': 25, '折扣': 10}
item7 = {'编号': '007', '名称': '哈密瓜', '价格': 20, '折扣': 10}
item = [item1, item2, item3, item4, item5, item6, item7]
def login():
while 1 == 1:
account = int(input("输入用户名"))
pwd = int(input("输入密码"))
for s in range(len(person)):
if person[s]['用户名'] == account and person[s]['密码'] == pwd:
print("成功登录,您的用户: ", person[s]['姓名'])
meauFunc()
return
print("您输入的用户名或密码错误,请重新输入")
def meau():
print("1.显示商品列表")
print("2.增加商品信息")
print("3.删除商品")
print("4.设置商品折扣")
print("5.修改商品信息")
print("6.退出")
def meauFunc():
while 1 == 1:
meau()
selection = int(input("输入选定的菜单业务号码"))
if selection == 1:
for s in range(len(item)):
print(item[s], '\t')
print('\n')
nextStep = int(input("返回上一级按: 1 "))
if nextStep == 1:
break
elif selection == 2:
while 2 ==2 :
print("添加货物的格式为: {'编号':'xxx','名称':'xxx','价格':xx,'折扣':xx} ")
addItem = input("增加商品信息")
item.append(addItem)
for s in range(len(item)):
print(item[s])
nextStep = int(input("返回上一级或继续添加货物: 1 or 0: "))
if nextStep == 1:
break
else:
continue
elif selection == 3:
while 2 ==2:
print("以下是现在的货物item详细: ")
for s in range(len(item)):
print(item[s])
item.pop(int(input("请输入删除的货物编号: 如:{'编号':'001'} 即 1")) - 1)
for s in range(len(item)):
print(item[s])
nextStep = int(input("返回上一级或继续删除货物: 1 or 0: "))
if nextStep == 1:
break
else:
continue
elif selection == 4:
while 2==2:
discount = int(input("设置商品折扣"))
for s in range(len(item)):
item[s]['折扣'] = discount
print("商品折扣已设好: 下面是所有折扣展示 ")
for s in range(len(item)):
print(item[s])
nextStep = int(input("返回上一级或继续修改折扣: 1 or 0: "))
if nextStep == 1:
break
else:
continue
# item6 = {'编号': '006', '名称': '西瓜', '价格': 25, '折扣': 10}
elif selection == 5:
while 2 ==2:
print("请分别输入您将修改商品编号,即可进入具体数据")
modifyId = int(input("第几个商品号")) - 1
modifyName = int(input("项目:比如'1.名称','2.价格','3.折扣' (请输入编号1/2/3)"))
if modifyName == 1:
modifyVal = input("修改值")
item[modifyId]['名称'] = modifyVal
elif modifyName == 2:
modifyVal = int(input("修改值"))
item[modifyId]['价格'] = modifyVal
else:
modifyVal = int(input("修改值"))
item[modifyId]['折扣'] = modifyVal
print("修改成功!以下是目前货物信息: ")
for s in range(len(item)):
print(item[s])
nextStep = int(input("是否返回上一级或继续修改商品信息: 1 or 0: "))
if nextStep == 1:
break
else:
continue
else:
print("您已经成功退出!!")
return
login()