Loop Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OzzyB
    New Member
    • Nov 2009
    • 15

    Loop Question

    I am writing a function that has two parameters F & L and the function returns the sum of the squares of all the intergers between F & L

    e.g if i call def sum(3,5) should output 50 (9 + 16 + 25)

    def sum(first,last) :
    sqaure = 0
    for i in range(first,las t + 1)

    this is my program code at the moment what am I missing?
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Is this for homework?

    Here is code for returning the sum of the numbers between first and last inclusive. Do you understand what it all does?

    Code:
    def sum(first,last):
        sqr=0
        for i in range(first,last+1):
            sqr+=i
        return sqr
    You'll easily be able to adjust it once you understand it. Let us know how it goes.

    Comment

    • OzzyB
      New Member
      • Nov 2009
      • 15

      #3
      Thanks

      Its not homework I am trying to learn python for my new job . This question is actually from the zelle's book. Thanks for your help I am going to use your function as a example.

      I hope you can help me in the future. If you have msn, please PM your email address.

      Thanks

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        I found a good way to learn was to use it solve some of the problems here:
        A website dedicated to the fascinating world of mathematics and programming

        which are designed for clever programming! Enjoy!

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          By the way, what are you planning to use python for?

          Comment

          Working...