Thread: Sumoftwonumbers

    #1
  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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by mrhlth
    Use code tags to make your code readable in the forum.

    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
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,893
    Rep Power
    481
    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
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. 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.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    Though shall not use function names that collide with Python's built-in function names.

    Comments on this post

    • Matt1776 agrees
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo