1
def num_sum():
    start = int(input("Please specify a starting number: "))
    end = int(input("Please specify a ending number: "))
    end = end + 1
    for x in range(start, end):
num_sum()

Here's an example of what I would like to have happen:

Computer: Please specify a starting number.

User: 1

Computer: Please specify a ending number.

User: 10

Math: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10

Computer: 55

Thanks in advance for the help. It means a lot to me! :)

I've seen a lot of these questions while making this, but a lot of them are very old and messy, I would like to know a new way that is less messy, updated, and less complicated.

For the project I'm doing my code must be non-messy or complicated. (As you can see by my example.)

It should not include any extra modules just plain, simple, beginner python.

Stidgeon
  • 2,673
  • 8
  • 20
  • 28
FakeBlob
  • 60
  • 2
  • 12
  • 2
    Use the `sum()` and `range()` functions. – Barmar May 19 '21 at 22:33
  • 4
    Or just use arithmetic: `(start + end) * (end - start + 1) / 2` – Barmar May 19 '21 at 22:37
  • @Barmar that's exactly what I propose in my answer. FakeBlob, the reason to use arithmetic instead of a loop is to make your program run faster. Obviously there's no discernible difference on something this easy, but as you move on to more complicated projects its something you'll need to keep in mind :) – Jasmine May 19 '21 at 23:13

3 Answers3

1

The Python version of summing here is very simple, since sum will work on any sequence of values (an iterator), and range() is one such. So summing the values from start to end is just:

mySum = sum(range(start, end+1))

Especially for some more complicated process, I would advise putting the calculation into a function with parameters, returning the result(s), although that might be overelaborate in this case:

def sumOver(start, end):
    return sum(range(start, end+1))

which you could then call from another part of the code that is responsible for "talking to the user":

def sumChallenge():
    start = int(input("Please specify a starting number: "))
    end = int(input("Please specify a ending number: "))
    print(sumOver(start, end))
Joffan
  • 1,485
  • 1
  • 13
  • 18
1

If you want to use the for, use a cumulative sum

def num_sum(start ,end ):
    
    total=0
    for x in range(start, end+1):
        total += x
    return total

start = 1
end = 10
print(num_sum(start ,end ))

output

55
DevScheffer
  • 491
  • 4
  • 15
  • I altered the code so it goes with any number with an input statement. Edit the code so it's like this if you like. `def num_sum(): start = int(input("Please specify a starting number: ")) end = int(input("Please sepcify a ending number: ")) total = 0 for x in range(start, end+1): total += x print(total)` @DevScheffer – FakeBlob May 26 '21 at 17:41
1

The sum of all integers from 1 to n is n(n + 1) / 2

if you take the sum of all integers from 1 to end and subtract from that the sum of all integers from 1 to (start - 1) you'll get your answer.

def num_sum():
    start = int(input("Please specify a starting number: "))
    end = int(input("Please specify a ending number: "))
    print(end * (end + 1) // 2 - (start - 1) * start // 2)
num_sum()

Remember your order of operations (PEMDAS) and recall that // is integer division--otherwise you'll get a float as output

Extra simple and no loops needed!

Jasmine
  • 127
  • 9