Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAldo Rivadeneira
3,241 Pointspython: dice-roller/rpg-roller 2/2 | Error
Im having the following error:
AttributeError: 'list' object has no attribute 'total'
I google it and the problem seems to be that function catch a list an it can't be process in the @property method.
im a little stuck here :/
import random
class Die:
def __init__(self, sides=2):
if sides < 2:
raise ValueError("Can't have fewer than two sides")
self.sides = sides
self.value = random.randint(1, sides)
def __int__(self):
return self.value
def __add__(self, other):
return int(self) + other
def __radd__(self, other):
return self + other
class D20(Die):
def __init__(self, sides=20):
super().__init__(sides)
from dice import D20
class Hand(list):
def __init__(self, size=0, die_class=None, *args, **kwargs):
if not die_class:
raise ValueError("You must define a dice class")
super().__init__()
for _ in range(size):
self.append(die_class())
self.sort()
# Now update Hand in hands.py.
# I'm going to use code similar to Hand.roll(2)
# and I want to get back an instance of Hand with
# two D20s rolled in it.
@classmethod
def roll(cls, dice):
dr = []
for _ in range(dice):
dr.append(cls(size=2, die_class=D20))
return dr
# I should then be able to call .total on the instance
# to get the total of the two dice.
@property
def total(self):
data = self
return sum(data[0])
1 Answer
Steven Parker
231,248 PointsYou don't have to create an __init__
override for this challenge, but if you do you must be careful not to change the method signature.
In particular you should not make any arguments mandatory for creating a new instance, since none were needed before.
Aldo Rivadeneira
3,241 PointsAldo Rivadeneira
3,241 PointsThanks Steven Parker , i swa my mistake and yes i don't need to override the init .
Thanks for your advice and time !