471,353 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

what wrong when I defined my class

Expand|Select|Wrap|Line Numbers
  1. class Test:
  2.     def __init__(self):
  3.         self._power = False
  4.         self._v = 5
  5.         self._station = 80.0
  6.         self._presets= [ 72.0, 74.4, 76.6]
  7.  
  8.     def togglePower(self):  # when I tried it this did not work
  9.         self._powerOn = not self._PowerOn
  10.  
  11.     def setPreset(self, ind): 
  12.             self._presets[ind]=self_station #when I enter .setPreset(1), it did not work either, global name '_station' is not defined
  13.  
  14.  
Oct 8 '07 #1
5 1092
elcron
43
Expand|Select|Wrap|Line Numbers
  1. class Test:
  2.     def __init__(self):
  3.         self._power = False
  4.         self._v = 5
  5.         self._station = 80.0
  6.         self._presets= [ 72.0, 74.4, 76.6]
  7.  
  8.     def togglePower(self):  # when I tried it this did not work
  9.         self._powerOn = not self._PowerOn
  10.  
  11.     def setPreset(self, ind): 
  12.             self._presets[ind]=self_station #when I enter .setPreset(1), it did not work either, global name '_station' is not defined
  13.  
  14.  
Expand|Select|Wrap|Line Numbers
  1. class Test:
  2.     def __init__(self):
  3.         self._power = False
  4.         self._v = 5
  5.         self._station = 80.0
  6.         self._presets= [ 72.0, 74.4, 76.6]
  7.  
  8.     def togglePower(self):  # when I tried it this did not work
  9.         self._powerOn = not self._PowerOn
  10.  
  11.     def setPreset(self, ind): 
  12.             self._presets[ind]=self._station
  13.  
  14.  
you left out the "." after self and before _station.
Oct 8 '07 #2
Expand|Select|Wrap|Line Numbers
  1. class Test:
  2.     def __init__(self):
  3.         self._power = False
  4.         self._v = 5
  5.         self._station = 80.0
  6.         self._presets= [ 72.0, 74.4, 76.6]
  7.  
  8.     def togglePower(self):  
  9.         self._powerOn = not self._powerOn
  10.  
  11.  
Thank you very much.

Another question, how can it make the following code return "True"
Expand|Select|Wrap|Line Numbers
  1.     def togglePower(self):  
  2.         self._powerOn = not self._powerOn
  3.  
It only return "none" when I try
Expand|Select|Wrap|Line Numbers
  1. print a.togglePower()
  2.  
Oct 8 '07 #3
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. class Test:
  2.     def __init__(self):
  3.         self._power = False
  4.         self._v = 5
  5.         self._station = 80.0
  6.         self._presets= [ 72.0, 74.4, 76.6]
  7.  
  8.     def togglePower(self):  
  9.         self._powerOn = not self._powerOn
  10.  
  11.  
Thank you very much.

Another question, how can it make the following code return "True"
Expand|Select|Wrap|Line Numbers
  1.     def togglePower(self):  
  2.         self._powerOn = not self._powerOn
  3.  
It only return "none" when I try
Expand|Select|Wrap|Line Numbers
  1. print a.togglePower()
  2.  
Expand|Select|Wrap|Line Numbers
  1.     def togglePower(self):  
  2.         self._power = not self._power
  3.         return self._power
  4. print a.togglePower()
  5.  
Oct 8 '07 #4
Thank you.


Another question related to the one below, can anyone locate the error in the source code below?
Expand|Select|Wrap|Line Numbers
  1. class Test:
  2.     def __init__(self):
  3.         self._powerOn = False
  4.         self._v = 10
  5.         self._station = 10
  6.         self._presets= [ 5, 6, 7, 8]
  7.  
  8.     def togglePower(self):
  9.         self._powerOn = not self._powerOn
  10.  
  11.     def setP(self, i):
  12.             self._presets[i]=self._station
  13.  
  14.     def gotoP(self, i):
  15.         self._station = self._presets[i]
  16.  
  17.     def increaseV(self):   
  18.         self._v = self._v +2
  19.  
  20.     def decreaseV(self):   
  21.         self._v = self._v -2
  22.  
  23.     def increaseStation(self):   
  24.         self._station = self._station + 1
  25.  
  26.     def decreaseStation(self):   
  27.         self._station = self._station - 1
  28.  
Oct 8 '07 #5
bartonc
6,596 Expert 4TB
Thank you.


Another question related to the one below, can anyone locate the error in the source code below?
Expand|Select|Wrap|Line Numbers
  1. class Test:
  2.     def __init__(self):
  3.         self._powerOn = False
  4.         self._v = 10
  5.         self._station = 10
  6.         self._presets= [ 5, 6, 7, 8]
  7.  
  8.     def togglePower(self):
  9.         self._powerOn = not self._powerOn
  10.  
  11.     def setP(self, i):
  12.             self._presets[i]=self._station
  13.  
  14.     def gotoP(self, i):
  15.         self._station = self._presets[i]
  16.  
  17.     def increaseV(self):   
  18.         self._v = self._v +2
  19.  
  20.     def decreaseV(self):   
  21.         self._v = self._v -2
  22.  
  23.     def increaseStation(self):   
  24.         self._station = self._station + 1
  25.  
  26.     def decreaseStation(self):   
  27.         self._station = self._station - 1
  28.  
It will be hard for us to tell without an example of which error you are getting. I do see that
Expand|Select|Wrap|Line Numbers
  1. # these two
  2.  
  3.     def setP(self, i):
  4.             self._presets[i]=self._station
  5.  
  6.     def gotoP(self, i):
  7.         self._station = self._presets[i]
will raise an error if you call the funtion with a number greater that 3.
Oct 8 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

220 posts views Thread by Brandon J. Van Every | last post: by
72 posts views Thread by E. Robert Tisdale | last post: by
13 posts views Thread by Jason Huang | last post: by
8 posts views Thread by Midnight Java Junkie | last post: by
669 posts views Thread by Xah Lee | last post: by
5 posts views Thread by Vols | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.