简体   繁体   English

我想找到我所拥有的数字的总和

[英]I want to find the sum of the number which i have

I have some code where I must find the multiples of number 3 and then summarize them I have done the first job, I mean I found all the multiples of number 3 with for loop but I can't summarize all the numbers which I found. 我有一些代码,我必须找到3号的倍数,然后总结它们我已经完成了第一个工作,我的意思是我找到了3号循环的所有倍数,但是我无法总结我找到的所有数字。

I have tried so many times and tried to find the solution on google, but could not find 我已经尝试了很多次,并试图在谷歌上找到解决方案,但找不到

x = 3
for number in range(1000):
    if number%x == 0:
        print(number)

I need now the sum of all the numbers which indicates on this code, when you run this code you can see that there is publishing only the numbers that can divide by 3 now I need the sum of them 我现在需要这个代码上显示的所有数字的总和,当你运行这段代码时,你可以看到只发布了可以除以3的数字,现在我需要它们的总和

It's easier than you think: 它比你想象的容易:

sum(range(0, 1000, 3))

Explanation: 说明:

range is defined as such: range([start], end[, step]) so range(0, 1000, 3) means go from 0 to 1000 in steps of 3 range 定义如下: range([start], end[, step])因此range(0, 1000, 3)表示从0到1000以3为步长

The sum function will sum over any iterable (which includes range) sum函数将对任何iterable(包括范围)求和

You need a variable to hold the sum (If you are in learning stage): 你需要一个变量来保存总和(如果你处于学习阶段):

x = 3
total = 0
for number in range(1000):
    if number % x == 0:
        print(number)
        total += number # equivalent to:  total = total + number
print(total)

Edit: 编辑:
To answer your comment use condition or condition : 要回答您的评论,请使用condition or condition

x = 3
y = 5
total = 0
for number in range(10):
    if number % x == 0 or number % y == 0:
        print(number)
        total += number # equivalent to:  total = total + number
print(total)

You could create a result variable to which you can keep adding: 您可以创建一个可以继续添加的result变量:

result = 0
x = 3
for number in range(1000):
    if number%x == 0:
        result += number
print(result)

The best way is using filter and sum : 最好的方法是使用filtersum

# any iterable variable
iterable_var = range(100)
res = sum(filter(lambda x: x % 3 == 0, iterable_var), 0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在一个数组中找到反转的次数,如果 i &gt; j 那么 2* a[i] &lt; a[j] 在最短的时间内 - I want to find number of inversion in an array which if i > j then 2* a[i] < a[j] in minimum time 我这里有一个代码,我想在某个 csv 文件中找到女性和男性的总数。 - I have a code here, I want to find the total number of females and males in a certain csv file. 我有一个列表,求和相似的数字 - I have an list, sum the similar number 如何打印总和等于数字立方的数字 - How can I print the numbers which have a sum that equals the cube of a number 我想找到数字中数字的平方和,我的代码适用于正数但似乎不适用于负数 - I want to find the sum of squares of digits in a number, my code work fine for positive numbers but doesn't seem to work with negative numbers 如果我有多个最小数字并且想要两个索引,如何在python数组中找到最小数字的索引? - How do I find the Index of the smallest number in an array in python if I have multiple smallest numbers and want both indexes? 我只想获取其中包含 k 个元素的那些子集 - I want to get only those subset which have k elements in it Python,我有一个猜谜游戏,想要添加猜谜数 - Python, I have a guessing game and want to add the number of guesses 我有一个数字列表和一定的和,并且我需要计算产生该和的可能方法 - i have a list of numbers and a certain sum, and i need to calculate the possible number of ways to generate that sum 找到4个数字,其总和为给定数字 - Find 4 numbers which sum is given number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM