another class difining question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    another class difining question

    I would like to write a program for calculating the distance of an from initial distance x0 and to a distance vot
    distance = x0 + v0t

    Here is my "intended" code
    [code=python]
    class distance:
    def __init__(self, iniX, iniV):
    self._x0= iniX
    self._v0= iniV

    def setTime(self,t) :
    self._t0=t

    def result(self):
    return self._x0 + self._v0*t

    #the result I would like to have
    d = distance(5,10)
    d.setTime(5)
    #d = 5 + 10*5 = 55
    >>>55
    d.setTime(5)
    #d=d(5) [the first distance set by t=5] + vo*5[the new distance set by another t=5]=105
    >>>105
    [/code]
    How can I write the right code?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by python101
    I would like to write a program for calculating the distance of an from initial distance x0 and to a distance vot
    distance = x0 + v0t

    Here is my "intended" code
    [code=python]
    class distance:
    def __init__(self, iniX, iniV):
    self._x0= iniX
    self._v0= iniV

    def setTime(self,t) :
    self._t0=t

    def result(self):
    return self._x0 + self._v0*t

    #the result I would like to have
    d = distance(5,10)
    d.setTime(5)
    #d = 5 + 10*5 = 20
    >>>20
    d.setTime(5)
    #d=d(5) [the first distance set by t=5] + d(5)[the new distance set by another t=5]=40
    >>>40
    [/code]
    How can I write the right code?
    Here's where you initially when wrong: self._v0*t t exists in the set function only. You save a copy for other functions to use (self._t0), so use that.[CODE=python]
    class distance:
    def __init__(self, iniX, iniV):
    self._x0= iniX
    self._v0= iniV
    self._t0 = 1

    def __str__(self):
    return "%f" %self.result()

    def setTime(self,t) :
    self._t0 = t

    def result(self):
    return self._x0 + (self._v0 * self._t0)


    d = distance(5, 10)
    print d
    d.setTime(5)
    print d[/CODE]
    Last edited by bartonc; Oct 7 '07, 05:08 AM.

    Comment

    • python101
      New Member
      • Sep 2007
      • 90

      #3
      Originally posted by bartonc
      Since 5 + (10 * 5) will never equal 20, I can't be sure about your math, but here's where you initially when wrong: self._v0*t t exists in the set function only. You save a copy for other functions to use (self._t0), so use that.[CODE=python]
      class distance:
      def __init__(self, iniX, iniV):
      self._x0= iniX
      self._v0= iniV
      self._t0 = 1

      def __str__(self):
      return "%f" %self.result()

      def setTime(self,t) :
      self._t0 = t

      def result(self):
      return self._x0 + (self._v0 * self._t0)


      d = distance(5, 10)
      print d
      d.setTime(5)
      print d[/CODE]
      Thank you for helping me.

      In your code,

      [code=python]
      def __str__(self):
      return "%f" %self.result()[/code]
      the result(), we haven't defined yet. when I try , the distance can't be updated; still 55, if enter setTime(5) several times.

      It should be 105 if I enter setTime(5) 2 times, 155 if enter setTime(5) 3 times. In other words, it continues add v0*t into the previous distance

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by python101
        Thank you for helping me.
        [code=python]
        class distance:
        def __init__(self, iniX, iniV):
        self._x0= iniX
        self._v0= iniV

        def __str__(self):
        return "%f" %self.result()

        def setTime(self, t):
        self._x0 += self._v0 * t

        def result(self):
        return self._x0


        d = distance(5, 10)
        print d
        d.setTime(5)
        print d
        d.setTime(5)
        print d[/CODE]5.000000
        55.000000
        105.000000

        If you use better (more descriptive) names for your variable, then it will be easier for those of us that are reading your code to be able to help. Remember, all this is your creation and none of us as been thinking along these same lines.
        Last edited by bartonc; Oct 7 '07, 05:11 AM.

        Comment

        Working...