1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
17
Rep Power
0

#### Sumoftwonumbers

Hi guys,
for the question:
Define a function named sum. This function expects two numbers, named low and high, as arguments. The function computes and returns the sum of all of the numbers between low and high, inclusive.

ive done:
def sum(num1, num2):

sumoftwo = 0
index = 0

for num1 < num2:
sumoftwo = sumoftwo + (num1 + index)
index = index + 1

return sumoftwo

i think thats correct, if theres any errors please point it out. also when i tried to run it, it gives me an error of "invalid syntax" while highlighting the "<", not sure what the problem is. Still a noob at python, and any help is appreciated! thanks
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
138
Rep Power
6
Originally Posted by mrhlth

Code:
```def sum(num1, num2):

sumoftwo = 0
index = 0

for num1 < num2:
sumoftwo = sumoftwo + (num1 + index)
index = index + 1

return sumoftwo```
The syntax "for num1 < num2" is not correct, you need to use range():

Code:
`for i in range(num1, num2+1):   # +1 makes it inclusive`
3. Most embarrassing! Your program is not correct.
Originally Posted by mrhlth
i think thats correct,...when i tried to run it, it gives me an error of "invalid syntax"
Test data for you:
Code:
```lo hi sum_of_inclusive_integers
1 5 15
2 5 14
3 5 12
4 5  9```
The handy formula:
(low + high)*(1+high-low)/2
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
17
Rep Power
0
Originally Posted by partoj
The syntax "for num1 < num2" is not correct, you need to use range():

Code:
`for i in range(num1, num2+1):   # +1 makes it inclusive`
Originally Posted by b49P23TIvg
Most embarrassing! Your program is not correct.

Test data for you:
Code:
```lo hi sum_of_inclusive_integers
1 5 15
2 5 14
3 5 12
4 5  9```
The handy formula:
(low + high)*(1+high-low)/2
Sorry for the late reply! and thank you both very much.
5. Though shall not use function names that collide with Python's built-in function names.