Notes in class:
Reflections on the last question:
as usual when the direct assignment:
def i(x,y):
print(x,y)
i(3,5,9)
This inevitable error, because the two variables corresponding to the number three
and the last to speak in class collection parameters:
def i(*x,y):
print(len(x),y)
i(3,5,9)
At this point it will be error, because the default 359 gave all the x, y no argument this time
so little turtle will be referred to either the i key parameters such as:
def i(*x,y):
print(len(x),y)
i(3,5,y=9)
Or add default parameters
def i(*x,y=4):
print(len(x),y)
i(3,5,9)
Only right
1. function directly as a function of the document and to write notes with a "#" What is the difference?
A: The function to write the document is to let someone else can better understand your function, so it's a good habit:
>>> def Add(x,y):
'add two numbers'
return x+y
***我们看到在函数开头写下的字符串Ta是不会打印出来的,但Ta会作为函数的一部分存储起来,这个我们称之为函数文档字符串,Ta的功能跟注释是一样的。***
函数的文档字符串可以按如下方式访问:
>>> Add.__doc__
'add two numbers'
另外,我们用help()来访问这个函数也可以看到这个文档字符串:
>>> help(Add)
Help on function Add in module __main__:
Add(x, y)
add two numbers
转载自:https://www.jianshu.com/p/38540d75521c
>>>
0. write a function meets the following requirements:
A) is calculated by multiplying print all parameters and base (base = 3) result of
b) If the last parameter (base = 5) parameters, the base is set to 5, base calculated as the sum does not participate.
自己的答案:
def number(x):
for each in x:
a = 0
eng = str(each)
#注意这里要为str找一个赋值变量
if eng.endswith('5'):
a = 1
break
all = 0
#赋值都在for 循环外,避免出错
for i in x:
num = int(i)
all += num
result = 0
if a == 1:
result = all * 5
else:
result = all * 3
return result
print(number((14,16,15,1,14,300)))
Their answer is not very flexible use talked about class content function
best answer:
def Sum(*params,base=3):
#添加了一个默认参数base
#用搜集参数符号*,从而使得params可以容纳多个数据
result = 0
for i in params:
result += i
result *= base
print('结果是:', result)
Sum(1, 2, 3, 4, 5)
Sum(1, 2, 3, 4, 5, base=5)
#人为的当需要改变时,输入关键字参数使得base=5
输出:
结果是: 45
结果是: 75
Reprinted from: https: //www.jianshu.com/p/38540d75521c
1. Looking narcissistic number
questions asked: If a 3-digit numbers equal to the cube and you said to this number is the number of daffodils. 153 = 1, for example, 3 + 5 3 + 3 ^ 3, 153 is thus a number daffodils. Write a program to find all the numbers daffodils.
自己的答案:
def flower():
#括号可以不用定义参数
for x in range(10,1000):
both = 0
temp = x
#这里必须用temp赋值,不然x在while会被改变,从而导致if函数无效
while temp:
a = temp % 10
both += a**3
temp = temp //10
if both == x:
print ('水仙花数为:',x)
else:
continue
flower()
Line of code:
转自:https://www.jianshu.com/p/38540d75521c
a = [i**3+j**3+k**3 for i in range(1, 10) for j in range(0, 10) for k in range(0, 10) if i*100+j*10+k == i**3+j**3+k**3]
2. Write a function findstr (), the function of the statistical number of times a length of 2 substring occurring in another string. For example: Assuming that the input character string is "You can not improve your past, but you can improve your future Once time is wasted, life is wasted..", The sub-string "im", printing is executed after the function "in the letter string sub CCP target string appears three times. "
def findstr():
a='You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted'
i=a.count('im')
print('子字母串在目标字符中总共出现:',i,'次')
findstr()
After the improvements:
def findstr():
print('请输入您想了解的英文字母:',end='')
a=input()
while not a.isalpha():
print('只能输入英文,请重新输入:',end='')
a=input()
print('请输入您要查询的字母:',end='')
b = input()
i=a.count(b)
print('子字母串在目标字符中总共出现:',i,'次')
findstr()
Summary: see above