Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Advanced Objects Multiplication

NumString challenge broken?

Can't get this challenge to work.

rmul () seems to always return an error, even when correct, based on testing inside and outside workspaces. Unable to see what results are expected, so unable to continue with this particular challenge task.

numstring.py
class NumString:
    def __init__(self, value):
        self.value = str(value)

    def __str__(self):
         return self.value

    def __int__(self):
        return int(self.value)

    def __float__(self):
        return float(self.value)

    def __add__(self, other):
        if '.' in self.value:
            return float(self) + other
        return int(self) + other

    def __radd__(self, other):
        return self + other

    def __iadd__(self, other):
        self.value = self + other
        return self.value

    def __mul__(self, num):
        self.value = int(self) * num
        return self.value

    def __rmul__(self, num):
        self.value = int(self) * num
        return self.value

I've gone so far as to try and cheat it, but it still fails

    def __rmul__(self, other):
        sneaky = type(other).__name__
        print(sneaky)
        if sneaky == 'float':
            self.value = float(self) * other
        elif sneaky == 'long':
            self.value = long(self) * other
        elif sneaky == 'int':
            self.value = int(self) * other
        elif sneaky == 'complex':
            self.value = complex(self) * other
        return self.value

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Look at how __add__ and __radd__ are implemented for addition for task 1. Then for task 2 look at how __iadd__ is implemented. Your solutions for multiplication should be very similar in implementations.

That hint help?

Michael Nichols
Michael Nichols
4,827 Points

Might be wrong but I think it's because you're saving the new number to the variable when you're only supposed to be doing the multiplication. rmul just lets you multiply with the variable on the right side

Hi guys, only saw this now.

Still unable to successfully complete task 1, no matter what I try it always returns: <number> * NumString gave the wrong value

The code definitely works, unless I'm really missing something obvious...