Struggling with program
Dave Angel
davea at davea.name
Sun Feb 17 21:49:32 EST 2013
On 02/17/2013 09:29 PM, maiden129 wrote:
First question: What version of Python are you writing this for?
Version 2.x has slightly different rules than version 3.x
> I'm trying to do this assignment and it not working, I don't understand why...
>
Define "working." Do you mean you get a syntax error when you try to
run it? If so, then post the full traceback, which will point to the
location of the error, (or sometimes the next line or two).
> This is what I have to do:
>
> Write the definition of a class Player containing:
> An instance variable name of type String , initialized to the empty String.
> An instance variable score of type int , initialized to zero.
> A method called set_name that has one parameter, whose value it assigns to the instance variable name .
> A method called set_score that has one parameter, whose value it assigns to the instance variable score .
> A method called get_name that has no parameters and that returns the value of the instance variable name .
> A method called get_score that has no parameters and that returns the value of the instance variable score .
> No constructor need be defined.
>
> Here is my code:
>
> class Player:
If this is Python 2.x, then you want to derive from object, not just
make a standalone class.
>
>
> name = ''
Have you read the Python tutorial page at:
http://docs.python.org/2/tutorial/classes.html
That's assuming you're writing for Python 2.7.
python doesn't have instance variables, they're called instance data
attributes. Have you defined name as an instance attribute, or a class
attribute?
>
> def __init__(self,score = 0)
Without a colon, this is a syntax error.
And without a body, it's another syntax error.
>
> def set_name (self):
Where is the parameter to hold the new name?
> self.name
This references the instance attribute, but doesn't modify it.
>
> def set_score (self):
> self.score
>
> def get_name
>
No parens, no parameters, no colon.
return name
>
> def get_score
ditto
> return score
>
> can someone please help me?
>
--
DaveA
More information about the Python-list
mailing list