Struggling with program

M

maiden129

I'm trying to do this assignment and it not working, I don't understand why...

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:


name = ''

def __init__(self,score = 0)

def set_name (self):
self.name

def set_score (self):
self.score

def get_name
return name

def get_score
return score

can someone please help me?
 
D

Dave Angel

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
 
M

Michael Torrie

I'm trying to do this assignment and it not working, I don't
understand why...

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. can someone please
help me?

While no one here is willing to do homework for someone else we do like
to help out however we can.

As Dave alluded to, python "instance variables" are called attributes
and typically are initialized in the the __init__(self, ...) method of
the class. The "self" name, however, can be anything you want. For
consistency, most python programmers use "self."

So your assignment is a bit misleading when it says a constructor is not
required, because if you want to initialize some instance attributes, it
has to be done in a method, usually the constructor, which in Python is
__init__.

"Instance variables" always need to be referred to with an explicit self
parameter. This is always the first parameter of any method
declaration. Thus you would define things kind of like so:

---------------------
from __future__ import print_function #in case python2

class MyClass(object):
def __init__(self, var2):
# create instance attributes and assign them values
self.instance_variable = 5
self.instance_variable2 = var2

def print_values(self):
print("instance_variable is {0}, and instance_variable2 is
{1}".format(self.instance_variable, self.instance_variable2))

if __name__ == "__main__":
c = MyClass(10); # this passes 10 to the __init__ method
c.print_values() # note there is no self parameter passed;
# python passes "c" for you implicitly.
 
C

Chris Angelico

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.

Is this actually a Python assignment? This sounds decidedly
un-Pythonic. A much more idiomatic way to write this would simply be
(with the explicit subclassing of object optional in Python 3):

class Player(object):
pass

You can then create a player thus:

fred = Player()

And set his name and score:

fred.name = "Fred"
fred.score = 1024

And get them just as easily:

print("Name: "+fred.name)
print("Score: "+str(fred.score))

Note how much code I wrote to make all this possible: None. In Java
and C++, the convention is to hide internal members, on the off-chance
that you might need to change a trivial getter/setter pair into
something more complicated (though in my experience, this almost never
can be done so simply even if you _have_ the getter and setter - the
only situation that that model serves is logging/breakpoints); in
Python, you get the same benefit without them, thanks to some spiffy
features that you probably don't need to worry about the details of at
the moment.

The only thing that I might want to change in the above code is to
create an __init__ member that initializes all the "normal" members -
possibly to its arguments, letting you do fred=Player("Fred",1024), or
possibly to some sort of default value. This serves as documentation
and also saves checking for AttributeError when reading from the
object. But otherwise, there's nothing in there that Python hasn't
already done for you.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top