求和与除法示例 (Python)

发布于 2024-08-11 01:13:17 字数 618 浏览 5 评论 0原文

>>> 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)
>>> 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
>>>

How do I automate this sum and division using a loop maybe?

Edit: Maybe I wasn't clear - I want a loop to keep doing the sum (of multiples of 7, eg 1-7, 1-14, 1-21 etc..) until it reaches x (x is the user input)

Okay, figured it out:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你是暖光i 2024-08-18 01:13:17

直接答案:

def sum_to_number_divided_by_seven(i):
  return sum(range(i+1)) / 7

更有效的答案:

def sum_to_number_divided_by_seven(i):
  return (i*(i+1))/14

The direct answer:

def sum_to_number_divided_by_seven(i):
  return sum(range(i+1)) / 7

The more efficient answer:

def sum_to_number_divided_by_seven(i):
  return (i*(i+1))/14
被翻牌 2024-08-18 01:13:17
def sumdiv7(limit):
    for i in range(limit):
        result = sum(range(i*7)) / 7
        print "For", i, ", sumdiv = ", result

例子:

>>> sumdiv7(4)
For 0 , sumdiv =  0
For 1 , sumdiv =  3
For 2 , sumdiv =  13
For 3 , sumdiv =  30

技巧很简单,你想对7的倍数求和,

要得到7的第i个倍数,只需 i*7

range 是一个Python函数来获取从 0 到 x 的数字列表

sum 对列表求和。

只需将这些部分组合在一起即可

def sumdiv7(limit):
    for i in range(limit):
        result = sum(range(i*7)) / 7
        print "For", i, ", sumdiv = ", result

Example:

>>> sumdiv7(4)
For 0 , sumdiv =  0
For 1 , sumdiv =  3
For 2 , sumdiv =  13
For 3 , sumdiv =  30

The trick is very simple, you want to sum multiples of 7,

To get the ith multiple of 7, it's just i*7

range is a python function to get a list of numbers from 0 to x

sum sums a list.

Just put these pieces together

仲春光 2024-08-18 01:13:17

我不确定你想要什么,但也许是这样的:

sum(range(x*7+1))/7

I'm not sure what you want, but maybe it is something like:

sum(range(x*7+1))/7
寂寞美少年 2024-08-18 01:13:17

我的版本:

def sum_of_nums_divided_by_7(num):
    return reduce(lambda x, y: x+y, range(num)) / 7

My version:

def sum_of_nums_divided_by_7(num):
    return reduce(lambda x, y: x+y, range(num)) / 7
安静被遗忘 2024-08-18 01:13:17

如果我正确理解你的问题。您希望能够接受用户输入 - x,然后将值 1-7 相加,然后除以 7,如果 qoutient 高于 x 则停止,否则继续将 1-14 相加,除以 7 并检查该商- 并以7的倍数继续?

我的简单解决方案是

x = input('user input - enter your value here')
y = 0
i = 1
while(x > y):
    q = sum(range(1, i*7+1))
    y = q/7
    print y
    i+=1

print "userinput:  %d" % (x)
print "iterations:  %d" %(i)
print "end value: %d" %(y)

if i understand your problem correctly. You want to be able to accept user input - x, and then sum values 1-7 then devide by 7, if the qoutient is higher than x stop there, otherwise continue to sum up 1-14, devide by 7 and check that quotient - and continute in multiples of 7?

My easy sollution is

x = input('user input - enter your value here')
y = 0
i = 1
while(x > y):
    q = sum(range(1, i*7+1))
    y = q/7
    print y
    i+=1

print "userinput:  %d" % (x)
print "iterations:  %d" %(i)
print "end value: %d" %(y)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文