I am trying to create a class MarblesBoard also include switch and rotate function.
My code is below:
class MarblesBoard():
def __init__(self, balls):
self.balls = balls
def __repr__(self):
return " ".join(str(i) for i in self.balls)
def switch(self):
lst=list(self.balls)
lst[0], lst[1] = lst[1], lst[0]
return lst
def rotate(self):
lst=list(self.balls)
lst = lst[1:]+lst[:1]
return self.balls
The out put should be like:
>>> board = MarblesBoard((3,6,7,4,1,0,8,2,5))
>>> board
3 6 7 4 1 0 8 2 5
>>> board.switch()
>>> board
6 3 7 4 1 0 8 2 5
>>> board.rotate()
>>> board
3 7 4 1 0 8 2 5 6
>>> board.switch()
>>> board
7 3 4 1 0 8 2 5 6
However, when I use switch or rotate, it allays call the original ball list. Not sure how to solve this.
You aren't actually modifying self.balls, just returning a modified list.
If you want to keep your methods basically the same, and continue to work with tuples, you could change the definition of switch() to actually write the changes to self.balls by doing something like:
def switch(self):
lst=list(self.balls)
lst[0], lst[1] = lst[1], lst[0]
self.balls = tuple(lst)
Likewise, you can change rotate() to something like this:
def rotate(self):
lst=list(self.balls)
lst = lst[1:]+lst[:1]
self.balls=tuple(lst)
Your methods are returning lists. If you want to modify the object, you have to change self.balls instead of returning. Like this:
class MarblesBoard:
def __init__(self, balls):
self.balls = balls
def __repr__(self):
return " ".join(str(i) for i in self.balls)
def switch(self):
self.balls[0], self.balls[1] = self.balls[1], self.balls[0]
def rotate(self):
self.balls = self.balls[1:] + self.balls[:1]
Related
I am trying to create a "CumulativeMovingAverage" class. That's what I did:
class CumulativeMovingAverage():
cma = None
n = 0
def add(self, *args):
if self.cma is None:
self.cma = args[0]
else:
self.cma = (args[0] + self.n*self.cma) / (self.n+1)
self.n += 1
return None
def __call__(self):
return self.cma
It works like this:
a = CumulativeMovingAverage()
a.add(2)
a.add(4)
a.cma ==> 3
a() ==> 3
I would like to overwrite a dunder method such that
a ==> 3
and also
b = a + 100
b ==> 103
That is, without having to call a with parenthesis. Is it possible? What dunder should I overwrite?
How should I implement func to correctly return the corresponding value of the Direction enum?
from enum import Enum
class Direction(Enum):
right = 0
down = 1
left = 2
up = 3
def func(self, n):
# When n = 0 return Direction.right
# When n = 1 return Direction.down
# When n = 2 return Direction.left
# When n = 3 return Direction.up
A function is not needed, it can simply be done like this:
>>> Direction(1)
<Direction.down: 1>
>>> Direction(3)
<Direction.up: 3>
Source: https://docs.python.org/3.4/library/enum.html
Hello I have these two classes
class BaseCounter(object):
def __init__(self):
print ("BaseCounter init = ")
self._counter = 0
def increment(self, count=1):
self._counter += count
def items(self):
return self._counter
class DictCounter(object):
def __init__(self, dict_class):
self._counter = defaultdict(lambda: dict_class)
def increment(self, key, value, *args, **kwargs):
print (key, value, args, kwargs)
self._counter[key].increment(value, *args, **kwargs)
def items(self):
result = []
for key, counter in self._counter.items():
result.append((key, counter.items()))
return result
and I am trying to create something like this:
y = DictCounter(DictCounter(DictCounter(BaseCounter())))
y.increment(10,1,2,3)
y.increment(10,1,2,3)
y.increment(10,1,3,3)
y.increment(10,2,2,3)
which leads to
10 1 2 12
10 1 3 12
10 2 2 12
10 2 3 12
but I was expecting
10 1 2 6
10 1 3 3
10 2 2 3
it should simulate, which is working correctly
defaultdict(defaultdict(defaultdict(int))) "with counter at the end"
but I am confused with the behavior (I think there will be problem with shallow copy or something with references)
Any idea?
As Martijn Pieters said. The problem was referencing to the same object (dict_class) for every new key. So instead of this:
class DictCounter(object):
def __init__(self, dict_class):
self._counter = defaultdict(lambda: dict_class)
....
DictCounter(DictCounter(DictCounter(BaseCounter())))
do this:
class DictCounter(object):
def __init__(self, dict_class):
self._counter = defaultdict(dict_class)
....
DictCounter(lambda: DictCounter(lambda: DictCounter(lambda: BaseCounter())))
I was trying to describe it a little bit more at my blog.
This is my code that I wanted to create,
class RegularPolygon :
def __init__(self):
self.__n =3
self.__side = 1
self.__x = 0
self.__y = 0
def get_n(self):
return self.__n
def get_side(self):
return self.__side
def getX(self):
return self.__x
def getY(self):
return self.__y
So If I run the below code to check,
polygon1 = RegularPolygon()
print(polygon1.get_n(), polygon1.get_side(), polygon1.getX(), polygon1.getY() )
It gives me 3 0 1 1 as result.
but I'd like to make a change that when I put order like
polygon2 = RegularPolygon(6)
print(polygon2.get_n(), polygon2.get_side(), polygon2.getX(), polygon2.getY() )
I want to have 6 0 1 1 for my result.
So my question is, how can I still get 3 0 1 1 when I don't put any argument in running RegularPolygon() but If put any integer such as 6, it gives me 6?
as user2896976 said just change
def __init__(self):
self.__n =3
to
def __init__(self, n=3):
self.n = n
This will mean n by default is set to 3 when you do not specify it, but when u specifiy it, like when u enter 6, it also gives that output too.
I am unsure how to make a health system for a class player to get killed after getting hit three times. Can someone please help? I am using Python 2.7 to code.
I currently have
if pygame.sprite.spritecollideany(player, opponents):
player.kill()
and i was considering using a variable
player.HP = 3
but it won't die!
if pygame.sprite.spritecollideany(player, opponents):
player.HP -1
if player.HP == 0:
player.kill
but, like i said, it won't die but the code works. It was killing until i added the new system, and now it won't. Can somebody help? Thanks.
As #juanpa.arrivillaga said in a comment, you need to assign the decremented value of player.HP or else, you are doing subtraction and throwing away the value. You need to change:
player.HP -1
to
player.HP -= 1
Here's an example:
>>> class A:
... def __init__(self, val):
... self.val = val
... def decrement(self):
... self.val - 1 # <- Does not reassign decremented value
... print self.val
...
>>> a = A(10)
>>> a.decrement()
10
>>> a.decrement()
10
>>> a.decrement()
10
>>> a.decrement()
10
>>> class A:
... def __init__(self, val):
... self.val = val
... def decrement(self):
... self.val -= 1 # <- Does reassign value
... print self.val
...
>>> a = A(10)
>>> a.decrement()
9
>>> a.decrement()
8
>>> a.decrement()
7
>>> a.decrement()
6