what wrong when I defined my class

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

    what wrong when I defined my class

    [code=python]
    class Test:
    def __init__(self):
    self._power = False
    self._v = 5
    self._station = 80.0
    self._presets= [ 72.0, 74.4, 76.6]

    def togglePower(sel f): # when I tried it this did not work
    self._powerOn = not self._PowerOn

    def setPreset(self, ind):
    self._presets[ind]=self_station #when I enter .setPreset(1), it did not work either, global name '_station' is not defined

    [/code]
  • elcron
    New Member
    • Sep 2007
    • 43

    #2
    Originally posted by python101
    [code=python]
    class Test:
    def __init__(self):
    self._power = False
    self._v = 5
    self._station = 80.0
    self._presets= [ 72.0, 74.4, 76.6]

    def togglePower(sel f): # when I tried it this did not work
    self._powerOn = not self._PowerOn

    def setPreset(self, ind):
    self._presets[ind]=self_station #when I enter .setPreset(1), it did not work either, global name '_station' is not defined

    [/code]
    [code=python]
    class Test:
    def __init__(self):
    self._power = False
    self._v = 5
    self._station = 80.0
    self._presets= [ 72.0, 74.4, 76.6]

    def togglePower(sel f): # when I tried it this did not work
    self._powerOn = not self._PowerOn

    def setPreset(self, ind):
    self._presets[ind]=self._station

    [/code]
    you left out the "." after self and before _station.

    Comment

    • python101
      New Member
      • Sep 2007
      • 90

      #3
      [code=python]
      class Test:
      def __init__(self):
      self._power = False
      self._v = 5
      self._station = 80.0
      self._presets= [ 72.0, 74.4, 76.6]

      def togglePower(sel f):
      self._powerOn = not self._powerOn

      [/code]
      Thank you very much.

      Another question, how can it make the following code return "True"
      [code=python]
      def togglePower(sel f):
      self._powerOn = not self._powerOn
      [/code]
      It only return "none" when I try
      [code=python]
      print a.togglePower()
      [/code]

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by python101
        [code=python]
        class Test:
        def __init__(self):
        self._power = False
        self._v = 5
        self._station = 80.0
        self._presets= [ 72.0, 74.4, 76.6]

        def togglePower(sel f):
        self._powerOn = not self._powerOn

        [/code]
        Thank you very much.

        Another question, how can it make the following code return "True"
        [code=python]
        def togglePower(sel f):
        self._powerOn = not self._powerOn
        [/code]
        It only return "none" when I try
        [code=python]
        print a.togglePower()
        [/code]
        [code=python]
        def togglePower(sel f):
        self._power = not self._power
        return self._power
        print a.togglePower()
        [/code]
        Last edited by bartonc; Oct 8 '07, 02:58 AM.

        Comment

        • python101
          New Member
          • Sep 2007
          • 90

          #5
          Thank you.


          Another question related to the one below, can anyone locate the error in the source code below?
          [code=python]
          class Test:
          def __init__(self):
          self._powerOn = False
          self._v = 10
          self._station = 10
          self._presets= [ 5, 6, 7, 8]

          def togglePower(sel f):
          self._powerOn = not self._powerOn

          def setP(self, i):
          self._presets[i]=self._station

          def gotoP(self, i):
          self._station = self._presets[i]

          def increaseV(self) :
          self._v = self._v +2

          def decreaseV(self) :
          self._v = self._v -2

          def increaseStation (self):
          self._station = self._station + 1

          def decreaseStation (self):
          self._station = self._station - 1
          [/code]

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by python101
            Thank you.


            Another question related to the one below, can anyone locate the error in the source code below?
            [code=python]
            class Test:
            def __init__(self):
            self._powerOn = False
            self._v = 10
            self._station = 10
            self._presets= [ 5, 6, 7, 8]

            def togglePower(sel f):
            self._powerOn = not self._powerOn

            def setP(self, i):
            self._presets[i]=self._station

            def gotoP(self, i):
            self._station = self._presets[i]

            def increaseV(self) :
            self._v = self._v +2

            def decreaseV(self) :
            self._v = self._v -2

            def increaseStation (self):
            self._station = self._station + 1

            def decreaseStation (self):
            self._station = self._station - 1
            [/code]
            It will be hard for us to tell without an example of which error you are getting. I do see that[CODE=python]# these two

            def setP(self, i):
            self._presets[i]=self._station

            def gotoP(self, i):
            self._station = self._presets[i][/CODE]will raise an error if you call the funtion with a number greater that 3.

            Comment

            Working...