Greed
Greed

Reputation: 25

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. For example , if 5 is entered, you will find five sum of consecutive numbers so that:

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

Upvotes: 1

Views: 22408

Answers (4)

Pranav
Pranav

Reputation: 135

Using list comprehension in 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

Upvotes: 0

Nitzle
Nitzle

Reputation: 2477

You could do this in one loop, by using range to define your numbers, and sum to loop through the numbers for you.

>>> 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. So, this for loop will happen 5 times for your example.

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.

' + '.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.
  • ' + '.join is used to "join" elements together with a common string, in this case, ' + '. In cases where there is only 1 element, join will just return that element.

sum(nums) is giving you the sum of all numbers defined in range(1, i+1):

  • When nums = range(1, 2), sum(nums) = 1
  • When nums = range(1, 3), sum(nums) = 3
  • Etc...

Upvotes: 3

Ketouem
Ketouem

Reputation: 3857

You don't have to use a while loop, 2 for will do the trick nicely and with a more natural feeling.

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

Upvotes: 0

LtWorf
LtWorf

Reputation: 7600

reduce(lambda x,y:x+y,range(x+1))

Upvotes: 0

Related Questions