>>> sum((1, 2, 3, 4, 5, 6, 7))
28
>>> 28/7
4.0
>>> sum((1,2,3,4,5,6,7,8,9,10,11,12,13,14))
105
>>> 105/7
15.0
>>>
我如何使用循环自动化这个和和除法?
编辑:也许我不清楚 - 我想要一个循环继续做总和(7的倍数,例如1-7、1-14、1-21等)直到它达到x(x是用户输入)
好吧,想通了:
def sum_and_div_of_multiples_of_7(x):
y = 7
while (y <= x):
mof7 = range(1,y)
print ('mof7 is', mof7)
total = sum(mof7)
print ('total =', total)
div = total/7
print ('div =', int(div), '\n')
y = y+7 # increase y
x = 70
sum_and_div_of_multiples_of_7(x)