sum = 0
x = 0
while x<100:
if x%2!=0:
sum+=x
x=x+1
print sum
x = 0
while x<100:
if x%2!=0:
sum+=x
x=x+1
print sum
2018-08-07
def average(*args):
if len(args)==0:
return 0.0
aver = 0
for i in args:
aver=aver+i
return (aver+0.0)/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
if len(args)==0:
return 0.0
aver = 0
for i in args:
aver=aver+i
return (aver+0.0)/len(args)
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
2018-08-06
def greet(a="world."):
if(a):
print 'hello,',a,'.'
return
print 'hello,wrold.'
greet()
greet('Bart')
if(a):
print 'hello,',a,'.'
return
print 'hello,wrold.'
greet()
greet('Bart')
2018-08-06
import math
def quadratic_equation(a, b, c):
x= (-b+math.sqrt(b*b-4*a*c))/(2*a)
y= (-b-math.sqrt(b*b-4*a*c))/(2*a)
return x,y
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
x= (-b+math.sqrt(b*b-4*a*c))/(2*a)
y= (-b-math.sqrt(b*b-4*a*c))/(2*a)
return x,y
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2018-08-06
就两行代码,这是我现在能想到的最简单的方法了
L = ['Adam', 'Lisa', 'Bart']
L.append(L.pop(0)) #删掉第一个并添加到最后
L.insert(0,L.pop(1)) #删掉第二个并添加到最前面
print L
L = ['Adam', 'Lisa', 'Bart']
L.append(L.pop(0)) #删掉第一个并添加到最后
L.insert(0,L.pop(1)) #删掉第二个并添加到最前面
print L
2018-08-06
def move(n, a, b, c):
if n==1:
print a,'-->',c
return 0
elif n==2:
print a,'-->',b
print a,'-->',c
print b,'-->',c
else:
move(n-1,a,b,c)
print a,'-->',c
move(n-1,b,a,c)
move(6, 'A', 'B', 'C')
自己写的,想了半天才弄懂
if n==1:
print a,'-->',c
return 0
elif n==2:
print a,'-->',b
print a,'-->',c
print b,'-->',c
else:
move(n-1,a,b,c)
print a,'-->',c
move(n-1,b,a,c)
move(6, 'A', 'B', 'C')
自己写的,想了半天才弄懂
2018-08-06
print [int(str(m)+str(n)+str(m)) for m in range(1,10) for n in range(0,10)]
两次循环就可以了,但是不足的地方是我转换成了字符串相加后最后才转换成数字
两次循环就可以了,但是不足的地方是我转换成了字符串相加后最后才转换成数字
2018-08-05
print [a*100+b*10+c for a in range(1,10) for b in range(0,10) for c in range(1,10) if a==c]
2018-08-05
sum = 0
x = 1
n = 1
while True:
if n>20:
break
sum+=x
#x=2**n
x=x*2
n=n+1
print sum
x = 1
n = 1
while True:
if n>20:
break
sum+=x
#x=2**n
x=x*2
n=n+1
print sum
2018-08-05