|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
测试题:
0.
don't repeat yourself,不要重复你自己,意在让程序员不要重复写同一用处的代码,把重复代码放在一起集中声明.
1.
1)减少了代码量,便于阅读
2)便于识别和复用
3)可以用别人写好的轮子,节约时间,提升效率
2.
可以,不过不建议过多参数
3.
def,要有小括号和冒号,函数名最好不要和系统函数方法重叠
4.
两个
5.
Hello World,因为return后就不会执行下面的代码了,毕竟return代表'返回'
动动手:
0.
def power(x,y):
if (not isinstance(x,int)) or (not isinstance(y,int)):
print('x,y必须是整数')
return
else:
calc = x
if x == 0:
print('x不能等于0!')
return
elif y == 0:
return 1
elif y > 0 :
if y == 1:
return x
else:
while y > 1:
calc = calc * x
y -= 1
return calc
else:
if y == -1:
return 1/x
else:
while y < -1:
calc = 1/(calc * x)
y += 1
return calc
1.
def gcd(x,y):
if x == y:
return x
else:
if x > y:
big = x
small = y
else:
big = y
small = x
dif = big - small
while dif != small:
if dif > small:
big = dif
small = small
else:
big = small
small = dif
dif = big - small
return small
2.
def shi2er(x):
if not isinstance(x,int):
print('x必须是整数!')
else:
L = str()
if x < 0 :
x = -x
flag = -1
else:
flag = 1
shang = x
yu = x % 2
if x == 0:
return str(0)
else:
while shang != 0:
L = str(yu) + L
shang = shang // 2
yu = shang % 2
if flag == -1:
return ('-'+L)
else:
return L
课堂总结:
1. 函数定义是def xxx():
2. while循环中,在循环开始之前,参数会有初始值,要保证装结果的值和初始值在一个节点上.
3. 声明flag= 0 or 1来界定两种状态 |
|