简体   繁体   English

在 Python 中添加连续整数,稍加改动

[英]Adding Consecutive integers in Python, with a twist

Here is my original code:这是我的原始代码:

x = input("Please input an integer: ")
x = int(x)
i = 1
sum = 0
while x >= i:
    sum = sum + i
    i += 1
print(sum)

Here is what the second part is:这是第二部分的内容:

b) Modify your program by enclosing your loop in another loop so that you can find consecutive sums. b) 通过将循环包含在另一个循环中来修改您的程序,以便您可以找到连续的总和。 For example , if 5 is entered, you will find five sum of consecutive numbers so that:例如,如果输入 5,您将找到五个连续数字的总和,因此:

1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15

I have been stuck on this for 3 days now, and I just can't understand how to do it.我已经坚持了 3 天了,我只是不明白该怎么做。 I have tried this but to no avail.我试过这个,但无济于事。

while x >= i:
    sum_numbers = sum_numbers + i
    past_values = range(i)
    for ints in past_values:
        L = []
        L.append(ints)
        print(L, "+", i, "=", sum_numbers)
    i += 1

Can anyone just help steer my in the correct direction?任何人都可以帮助引导我朝着正确的方向前进吗? BTW.顺便提一句。 it is python 3.3它是python 3.3

You could do this in one loop, by using range to define your numbers, and sum to loop through the numbers for you.您可以在一个循环中完成此操作,方法是使用range定义您的数字,并使用sum为您循环遍历这些数字。

>>> x = input("Please input an integer: ")
Please input an integer: 5
>>> x = int(x)
>>>
>>> for i in range(1, x+1):
...     nums = range(1, i+1)
...     print(' + '.join(map(str, nums)), '=', sum(nums))
...
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15

range(1, x+1) would give me [1, 2, 3, 4, 5] , this acts as the controller for how many times we want to print out a sum. range(1, x+1)会给我[1, 2, 3, 4, 5] ,它充当我们想要打印多少次总和的控制器。 So, this for loop will happen 5 times for your example.因此,对于您的示例,此 for 循环将发生 5 次。

nums = range(1, i+1) notice we are using i instead here, (taken from the range above), which I am using to define which number I am up to in the sequence. nums = range(1, i+1)注意我们在这里使用i代替(取自上面的range ),我用它来定义我在序列中达到的数字。

' + '.join(map(str, nums)) : ' + '.join(map(str, nums)) :

  • map(str, nums) is used to convert all elements of nums into strings using str , since the join method expects an iterable filled with strings. map(str, nums)用于使用strnums所有元素转换为字符串,因为join方法需要一个用字符串填充的迭代。
  • ' + '.join is used to "join" elements together with a common string, in this case, ' + '. ' + '.join用于将元素与公共字符串“连接”在一起,在本例中为 ' + '。 In cases where there is only 1 element, join will just return that element.在只有 1 个元素的情况下, join将只返回该元素。

sum(nums) is giving you the sum of all numbers defined in range(1, i+1) : sum(nums)为您提供range(1, i+1)定义的所有数字的总和:

  • When nums = range(1, 2) , sum(nums) = 1当 nums = range(1, 2)sum(nums) = 1
  • When nums = range(1, 3) , sum(nums) = 3当 nums = range(1, 3)sum(nums) = 3
  • Etc...等等...
reduce(lambda x,y:x+y,range(x+1))

You don't have to use a while loop, 2 for will do the trick nicely and with a more natural feeling.您不必使用 while 循环,2 for 会很好地完成这个技巧,并且感觉更自然。

x = input("Please input an integer : ")
x = int(x)
item = range(1, x + 1)
for i in item:
    sum = 0
    for j in range(1, i + 1):
        sum = sum + j
    print(str(sum))

Using list comprehension in python:在python中使用列表理解:

x = input("Please input an integer: ")
x = int(x)
i = 1

sums = [(((1+y)*y)//2) for y in range(i, x+1)] # creates list of integers
print(sums) # prints list of sums on one line

OR或者

[print(((1+y)*y)//2) for y in range(i, x+1)] # prints sums on separate line

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM