1. 程式人生 > >每日題記10

每日題記10

14. 出錯 nonlocal nbsp 升級 每日 動態傳參 class 字符串

# 2,寫函數,接收n個數字,求這些參數數字的和。(動態傳參)
# def sum(*num):
#     s=0
#     for i in num:
#         s+=i
#     return s
#3,讀代碼,回答:代碼中,打印出來的值a,b,c分別是什麽?為什麽?
# a=10
# b=20
# def test5(a,b):
#     print(a,b)
# c = test5(b,a)
# print(c)        #a=20,b=10,c=None
#4,讀代碼,回答:代碼中,打印出來的值a,b,c分別是什麽?為什麽?
# a=10
# b=20
# def test5(a,b):
#     a=3
#     b=5
#     print(a,b)
# c=test5(a,b)            #a=3,b=5,c=None
# print(c)
#5,寫函數,傳入函數中多個實參(均為可叠代對象如字符串,列表,元祖,集合等),將每個實參的每個元素依次添加到函數的動態參數args裏面.
# 例如 傳入函數兩個參數[1,2,3] (22,33)最終args為(1,2,3,22,33)
# def fn(*args):
#     print(args)
# fn(*[1,2,3],*(22,33))
#6,寫函數,傳入函數中多個實參(實參均為字典),將每個實參的鍵值對依次添加到函數的動態參數kwargs裏面.
# 例如 傳入函數兩個參數{‘name’:’alex’} {‘age’:1000}最終kwargs為{‘name’:’alex’ ,‘age’:1000}
# def fn(**kwargs):
#     print(kwargs)
# fn(**{"name":"alex"} ,**{"age":1000})
#7, 下面代碼成立麽?如果不成立為什麽報錯?怎麽解決?
# 7.1
# a = 2
# def wrapper():
#     print(a)
# wrapper()       #成立,輸出a=2
#
# # 7.2
# a = 2
# def wrapper():
#     global a
#     a += 1
# print(a)
# wrapper()       #不成立,全局作用域變量與局域作用域變量命名重復,執行錯誤
# # 7.3
# def wrapper():
#     a = 1
#     def inner():
#         print(a)
#     inner()
# wrapper()       #成立,輸出a=1
# 7.4
# def wrapper():
#     a = 1
#     def inner():
#         nonlocal a
#         a += 1
#         print(a)
#     inner()
# wrapper()       #不成立,當前的局域變量中與外一層的局部變量a無法讀取
#8,寫函數,接收兩個數字參數,將較小的數字返回.
# def min(a,b):
#     c=a if a<b else b
#     return c
#9,寫函數,接收一個參數(此參數類型必須是可叠代對象),將可叠代對象的每個元素以’_’相連接,形成新的字符串,並返回.
# 例如 傳入的可叠代對象為[1,‘老男孩‘,‘武sir‘]返回的結果為’1_老男孩_武sir’
# def func(*args):
#     s=""
#     for i in args:
#         s=s+"_"+str(i)
#     s1=s[1:]
#     print(s1)
# func(*[1,‘老男孩‘,‘武sir‘])
#10,寫函數,傳入n個數,返回字典{‘max’:最大值,’min’:最小值}
# 例如:min_max(2,5,7,8,4) 返回:{‘max’:8,’min’:2}(此題用到max(),min()內置函數)
# def fn(**kwargs):
#     dic={}
#     dic["min"]=min(args)
#     dic["max"]=max(args)
#     return dic
# print(fn(2,5,7,8,4))
#11,寫函數,傳入一個參數n,返回n的階乘
# 例如:cal(7)  計算7*6*5*4*3*2*1
# def fn(num):
#     cal=1
#     while  num>0:
#         cal=cal*num
#         num-=1
#     print(cal)
# fn(3)
#12寫函數,返回一個撲克牌列表,裏面有52項,每一項是一個元組
# 例如:[(‘紅心’,2),(‘草花’,2), …(‘黑桃’,‘A’)]
# ll=["紅心","方塊","梅花","黑桃"]
# def fn(*args):
#     lst=[]
#     for a in args:
#         for b in ll:
#             if a==1:
#                 lst.append((b,"A"))
#             elif a==11:
#                 lst.append((b, "J"))
#             elif a==12:
#                 lst.append((b,"Q"))
#             elif a==13:
#                 lst.append((b,"K"))
#             else:
#                 lst.append((b,a))
#     print(lst)
# fn(1,2,3,4,5,6,7,8,9,10,11,12,13)


# def func():
#     a = [‘紅心‘, ‘草花‘, ‘方片‘, ‘黑桃‘]
#     b = [2, 3, 4, 5, 6, 7, 8, 9, 10]
#     c = [‘A‘, ‘J‘, ‘Q‘, ‘K‘]
#     li = []
#     for i in b:
#         for p in a:
#             tp = (p, i)
#             li.append(tp)
#     for m in c:
#         for n in a:
#             tp1 = (n, m)
#             li.append(tp1)
#     print(li)
#     print(len(li))
# func()
#13 有如下函數:
# def wrapper():
#     def inner():
#         print(666)
# wrapper()

# 你可以任意添加代碼,用兩種或以上的方法,執行inner函數.
# def wrapper():
#     def inner():
#         print(666)
#     inner()
# wrapper()
# def wrapper():
#     def inner():
#
#         print(666)
#     ret=inner()
#     print(ret)
# wrapper()
#14.
#1.
# 有函數定義如下:
# def calc(a,b,c,d=1,e=2):
# return (a+b)*(c-d)+e
# 請分別寫出下列標號代碼的輸出結果,如果出錯請寫出Error。
# print(calc(1,2,3,4,5))__2___
# print(calc(1,2))___Error_
# print(calc(e=4,c=5,a=2,b=3))___ -7
# print(calc(1,2,3))_____8
# print(calc(1,2,3,e=4))____10
# print(calc(1,2,3,d=5,4))__Error___
#2(此題有坑)下面代碼打印的結果分別是__list1=["a",10]__list2=[123]    list3=["a",10]_____,________,________.
# def extendList(val,list=[]):
#     list.append(val)
#     return list
# list1 = extendList(10)          #list1=[10]
# list2 = extendList(123,[])      #list=[123]
# #
# #
# list3 = extendList(‘a‘)
#
# print(‘list1=%s‘ % list1)
# print(‘list2=%s‘ % list2)
# print(‘list3=%s‘ % list3)
# 3, 寫代碼完成99乘法表.(升級題)
# 1 * 1 = 1
# 2 * 1 = 2 2 * 2 = 4
# 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
# ......
# 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81

# print( ‘\n‘.join([‘ ‘.join([‘%s*%s=%-2s‘ % (y,x,x*y) for y in range(1,x+1)]) for x in range(1,10)]))
# for m in range(1,10):
#     for n in range(1,m+1):
#         print(‘%s×%s=%s‘%(m,n,m*n),end=‘ ‘)
#     print()

每日題記10