day09作业—函数进阶

# 2.写函数,接收n个数字,求这些参数数字的和。(动态传参)
def func1(*args):
     sum = 0
    for i in args:
        sum += i
    print(sum)
func1(1,2,3,4,5)
View Code
# sum = 0
# def func1(*args):
#     for i in args:
#         global sum
#         sum += i
#     print('答案等于',sum)
# # func1(1,2,3,4,5)
# while 1:
#     shuzi = int(input('输入数字:'))
#     func1(shuzi)
View Code
3.读代码,回答:代码中,打印出来的值a,b,c分别是什么?为什么?
 a=10
 b=20
 def test5(a,b):
         print(a,b)
 c = test5(b,a)
 print(c)
a = 10
b = 20
def test5(a, b):
    print(a, b)
c = test5(b, a) #b =20 a =10
print(c)'''
#20 10 none
View Code
'''
4.读代码,回答:代码中,打印出来的值a,b,c分别是什么?为什么?
 a=10
 b=20
 def test5(a,b):
  a=3
  b=5
      print(a,b)
 c = test5(b,a)
 print(c)
a=10
b=20
def test5(a,b):
    a=3
    b=5
    print(a,b)
c = test5(b,a)  # b =20 a =10
print(c)'''
#3 5 none
View Code
'''
5.传入函数中多个列表和字典,如何将每个列表的每个元素
依次添加到函数的动态参数args里面?
如何将每个字典的所有键值对依次添加到kwargs里面?
'''
l1 = [1,2,3]
l2 = ['a','b','c']
l3 = [(1,2),1,{1:3}]
def func5(*args):
    print(*args,end=' ')
func5(*l1)
def func6(*args):
    print(*args)
    for l in args:
        func5(*l)
func6(l1,l2,l3)

d1={1:2,3:4}
d2={'a':'b','c':'d'}
func6(d1,d2)
View Code
6.下面代码成立么?如果不成立为什么报错?怎么解决?
6.1
 a = 2
 def wrapper():
         print(a)
 wrapper()

    6.2
 a = 2
 def wrapper():
            a += 1
     print(a)
 wrapper()
6.3
def wrapper():
     a = 1
     def inner():
         print(a)
     inner()
 wrapper()
6.4
def wrapper():
     a = 1
     def inner():
         a += 1
         print(a)
     inner()
 wrapper()'''
View Code
'''7.写函数,接收两个数字参数,将较小的数字返回.'''
def func7(can1 , can2):
    if can1 > can2:
        return can2
    else:
        return can1
# func7(1,2)
print(func7(1,2))
View Code
'''8.写函数,接收一个参数(此参数类型必须是可迭代对象),
将可迭代对象的每个元素以’_’相连接,形成新的字符串,并返回.
例如 传入的可迭代对象为[1,'老男孩','武sir']返回的结果为’1_老男孩_武sir’'''
a = [1,'老男孩','武sir']
def func8(*args):
    for i in args:
        # print(args)
        print(i,end='_')
func8(*a)
View Code
#从最里层返回任意一个字符串,在最外层打印
def fua(args):
    def fub(args):
        def fuc(args):
            def fud(args):
                a = 8
                return a
            a = fud(0)
            return a
        a = fuc(0)
        return a
    a = fub(0)
    return a
print(fua(0))
View Code

 

posted @ 2019-03-14 20:39  learnacode  阅读(195)  评论(0编辑  收藏  举报