[Tutor] Not sure what I'm doing wrong with these 2 python scripts

2013-11-20 Thread Anton Gilb
Not sure what I'm doing wrong, here are the problems and what I have for
answers so far.


1.)Write the definition of a class  Counter containing:
An instance variable  named  counter of type  int .
A constructor that takes one  int argument and assigns its value to  counter
A method named  increment that adds one to  counter . It does not take
parameters or return a value.
A method named  decrement that subtracts one from  counter . It also does
not take parameters or return a value.
A method named  get_value that returns the value of the instance variable
counter .

class Counter(object):
def __init__(self, ct):
self.counter = ct
def increment(self):
self.counter += 1
def decrement(self):
self.counter -= 1
def get_value(self):
return self.counter

2.)Write the definition of a class  WeatherForecast that provides the
following behavior (methods):
A method called  set_skies that has one parameter, a String.
A method called  set_high that has one parameter, an int.
A method called  set_low that has one parameter, an int.
A method called  get_skies that has no parameters and that returns the
value that was last used as an argument in  set_skies .
A method called  get_high that has no parameters and that returns the value
that was last used as an argument in  set_high .
A method called  get_low that has no parameters and that returns the value
that was last used as an argument in  set_low .
 No constructor need be defined. Be sure to define instance variables  as
needed by your "get"/"set" methods.
class WeatherForecast(object):
def __init__ (self):
self.skies = ""
def get_skies():
return self.set_skies
def set_skies(self, value)
self.skies = value
def get_high():
return self.set_high
def set_high(self, value):
self.high = value
def get_low():
return self.set_low
def set_low(self):
self.low = value
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Not sure what I'm doing wrong with these 2 python scripts

2013-11-20 Thread Andreas Perstinger
Anton Gilb  wrote:

>Not sure what I'm doing wrong, here are the problems and what I have
>for answers so far.

You should tell us what's wrong with your solutions.

Do you get an error? Then please show us the complete traceback (error
message).

Does your code something else than you expect? Then show us what you
do, what you get and how/why it differs from the expected solution.

Bye, Andreas
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Not sure what I'm doing wrong with these 2 python scripts

2013-11-20 Thread Steven D'Aprano
On Tue, Nov 19, 2013 at 06:22:32PM -0600, Anton Gilb wrote:
> Not sure what I'm doing wrong, here are the problems and what I have for
> answers so far.

What makes you think you're doing anything wrong? See below for more 
comments.


> 1.)Write the definition of a class  Counter containing:
> An instance variable  named  counter of type  int .
> A constructor that takes one  int argument and assigns its value to  counter
> A method named  increment that adds one to  counter . It does not take
> parameters or return a value.
> A method named  decrement that subtracts one from  counter . It also does
> not take parameters or return a value.
> A method named  get_value that returns the value of the instance variable
> counter .
> 
> class Counter(object):
> def __init__(self, ct):
> self.counter = ct
> def increment(self):
> self.counter += 1
> def decrement(self):
> self.counter -= 1
> def get_value(self):
> return self.counter


This looks fine to me, except for a couple of little nit-picks about 
terminology. (If you care about them, feel free to ask, otherwise I'll 
just bite my tongue.) What errors are you getting? Nothing is obvious.


> 2.)Write the definition of a class  WeatherForecast that provides the
> following behavior (methods):
> A method called  set_skies that has one parameter, a String.
> A method called  set_high that has one parameter, an int.
> A method called  set_low that has one parameter, an int.
> A method called  get_skies that has no parameters and that returns the
> value that was last used as an argument in  set_skies .
> A method called  get_high that has no parameters and that returns the value
> that was last used as an argument in  set_high .
> A method called  get_low that has no parameters and that returns the value
> that was last used as an argument in  set_low .
>  No constructor need be defined. Be sure to define instance variables  as
> needed by your "get"/"set" methods.

Again, what errors are you getting?

In this case, I can guess what some of the errors might be. See below.

> class WeatherForecast(object):
> def __init__ (self):
> self.skies = ""

The instructions say that you don't have to define a constructor, but 
they don't forbid it. In this case, I think I'd prefer not to include 
the constructor.

(I know I said I wouldn't, but here's one of the nit-picks: __init__  
isn't strictly speaking a constructor, it's an initialiser. Not that the 
difference really matters here.)


> def get_skies():
> return self.set_skies

Here, rather than return the skies value, you return the "set_skies" 
method itself. I suggest you look at the first class and consider how 
the get_value method works. You want to return the "skies" value 
instead.


> def set_skies(self, value)
> self.skies = value
> def get_high():
> return self.set_high

Likewise, here rather than return the high value, you return the 
"set_high" method.

> def set_high(self, value):
> self.high = value
> def get_low():
> return self.set_low

And likewise again.

> def set_low(self):
> self.low = value


Hope this helps,


-- 
Steven



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unit testing individual modules

2013-11-20 Thread ALAN GAULD
Forwarding to the python list for visibility/commebnt.

Please always use ReplyAll to include the list.

Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/

http://www.flickr.com/photos/alangauldphotos



>
> From: Patti Scott 
>To: Alan Gauld  
>Sent: Wednesday, 20 November 2013, 17:44
>Subject: Re: [Tutor] Unit testing individual modules
> 
>
>
>Thank you for your comments. 
> 
>I am trying to import the program file named rball.py, then call/run one of 
>the modules 
>within rball.py  [printIntro() to start]  without running the entire main() 
>module of rball.py.   


We need to clarify the terminology.

The "program file" is both a program file (aka script) and a module.
In Python module refers to the file not the functions/classes within it.

printIntro(), and indeed main() itself, are *functions* within the module.

When you run a file directly from the OS prompt, eg:

$ python rball.py

then the interpreter will set the __name__ attribute to __main__.
Thus the if statement at the end will only execute the main() function if
you run the file from the OS prompt as above.

When you import the file, as in

import rball

Then the interpreter sets the __name__ attribute to the name 
of the module, rball in this case. Because the name is not 
__main__ your main function is defined but not executed.
Because main() is never executed the functions defined 
within it are not defined. Hence you get an error when 
you try to use them.

It might work(I have't tried it) if you execute main() 
manually and then try to execute the functions but I 
suspect it won't since the functions are local to main().

>My understanding is that being able to call/run select modules within the  
>main() module is a useful troubleshooting technique.  


No.

It is much more helpful to define your functions outside of main.
You can then access them as functions via the module, thus:

import rball
rball.printIntro()

etc.

What is useful is to define a test() or main() function that 
will exercise all of the other functions when the file is executed 
from the OS level as shown above. I suspect thats the bit that 
has confused you.

Thus when you run

$ python rball.py

You will see all your tests being executed.

But when you do an import in another program(or at the 
Python >>> prompt) you can access the functions in your 
code via the module name. This dual purpose use of files 
is a very handy feature indeed.


>According to my text, adding the conditional execution statement 
>if __name__=='__main__': main()  should result in the program 
> running when invoked directly [F5 from the Idle editor window], 
> but not running when imported [import rball].  

That is correct. 

> When imported, however, I should be able to individually test 
> the functions within rball.py  such as

>>>import rball
>>>rball1.gameOver(0,0)
>False
> 
>This is not working as presented.  

Thats because you put the function definitions inside main()
Move them outside main and it will work as you expect.

HTH,

Alan G.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor