-2

I have written the following code for an exercise I was given, nonetheless, when I try to submit it, I get an incorrect answer for my code.

So this is my code:

def problem(n):
    my_sum = 0
    while my_sum < n: 
        my_sum = n 
        my_sum = my_sum + n 
    print (my_sum)

What they have asked me to code is the following: Write a function problem(n): that adds up the numbers 1 through n and prints out the result. You should use either a 'while' loop or a 'for' loop. Be sure that you check your answer on several numbers n.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • this is wrong because you aren't adding all the numbers. you shouldnt be iterating over my_sum but over 1..n – Nullman Jun 04 '17 at 21:11

2 Answers2

3

You can try this:

def problem(n):

   print sum(xrange(1, n+1))

If you are not allowed to use the built in sum function, you can try this:

def problem(n):

   counter = 0
   for i in range(1, n+1):
       counter += i

   print counter

The top example uses a for loop in what is called list comprehension, which is a shorter way of utilizing the regular for loop.

Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • it has to be a while or for loop though – Nullman Jun 04 '17 at 21:11
  • 2
    Get rid of the braces, you do not need them. In fact, it makes it slower (constructing a possible huge list instead of using a generator to get one value at a time). – MaxNoe Jun 04 '17 at 21:13
  • BTW, you can do that 1st version more efficiently: `sum(range(1,n+1))` – PM 2Ring Jun 04 '17 at 21:14
  • If this is py2 then surely `sum(xrange(1, n+1))` - no? – AChampion Jun 04 '17 at 21:16
  • 1
    @AChampion I just did some `timeit` tests and you're correct: `xrange` is at least 10% faster, even for small `n`. – PM 2Ring Jun 04 '17 at 21:39
  • @AChampion: FWIW, here's [my `timeit` code](https://gist.github.com/PM2Ring/ea746db64ecd17c2fb5d6907342f9911) – PM 2Ring Jun 04 '17 at 21:48
  • Why not `sum(xrange(n+1))`? – Jared Goguen Jun 04 '17 at 21:49
  • One extra calculation, and not strictly the same as original ask `sum 1 to n`. – AChampion Jun 04 '17 at 21:53
  • Please see my recent edit. – Ajax1234 Jun 04 '17 at 21:54
  • 1
    @JaredGoguen Interestingly, `sum(xrange(n+1))` might be slightly faster since it doesn't need to pass the `1` arg. But it's hard to tell with my `timeit` code, since the difference is smaller than the variation in times between runs. – PM 2Ring Jun 04 '17 at 21:57
  • 1
    @MaxNoe There are surprisingly many cases in which constructing an intermediate list comprehension is quicker than using a generator (since most Python implementations heavily optimize list comprehensions). Don't know whether that's true here, just a cool tidbit. – Jared Goguen Jun 04 '17 at 22:18
0

Your solution will iterate through the while loop only once and when my_sum becomes >= to n it will stop.

What you need is:

for x in range(1, n+1):                      
    my_sum += x

This will do the job. n+1 is done because the right boundary of range() is non-inclusive.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
tigrank
  • 31
  • 2