def square_of_sum(L):
L= [elem * elem for elem in L]
return sum(L)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
L= [elem * elem for elem in L]
return sum(L)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-01-23
sum = 0
x = 0
y = 0
while True:
x = x + 1
if x > 100:
break
if x == y:
continue
sum = sum + x
y = y + 2
print sum
x = 0
y = 0
while True:
x = x + 1
if x > 100:
break
if x == y:
continue
sum = sum + x
y = y + 2
print sum
2015-01-21
当然也可以先判断x是否为奇数,这样计算也不会错误。
但是在编程时,要确定哪些需要优先判断的,形成一个好习惯。
- 嗯,我是这么想的。
但是在编程时,要确定哪些需要优先判断的,形成一个好习惯。
- 嗯,我是这么想的。
2015-01-20
#coding=utf-8
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break #判断x是否大于100,如果大于100就终止循环
if x % 2 == 0:
continue #优先判断x是否大于100,接着判断x是否是奇数,如果是奇数则计算sum
sum = sum + x
print sum
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break #判断x是否大于100,如果大于100就终止循环
if x % 2 == 0:
continue #优先判断x是否大于100,接着判断x是否是奇数,如果是奇数则计算sum
sum = sum + x
print sum
2015-01-20