-1

So the task is to make a universal Vector class to perform add method whatever(str or int) the x,y values are. So here is the code that i've tried to execute just to check if try,except somehow works inside a class

class Vector():
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def __valuecheck__(self):
        try:
            self.x + "a"
        except TypeError:
            return str(self.x)
    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)
    def __repr__(self):
        return "Vector({},{})".format(self.x,self.y)

a = Vector(1,"a")
b = Vector("a",2)
c = a.__add__(b)
print(c)

The expected output is

Vector(1a,a2)

I've tried different variants, defining classic function e.g. def valuecheck(), as well tried adding try,except to add and init method, but none seem to work. Need your help guys, any tip is very appreciated! Cheers!

  • what does it output – DontBe3Greedy Feb 17 '20 at 18:52
  • The `__add__` method of a class tells python how to add two instances of that class together (i.e. `Vector(1, "a") + Vector("a", 2)`). You should look into [overloading a class's addition operator](Overloading Addition, Subtraction, and Multiplication Operators). – b_c Feb 17 '20 at 18:53
  • @DontBe3Greedy the expected output is Vector(1a,a2) – Chris Python Feb 17 '20 at 18:54
  • @b_c thx for the answer, but i can't imagine how it solves the case? – Chris Python Feb 17 '20 at 18:55
  • im not asking for the expected output, an asking for what it outputs right now – DontBe3Greedy Feb 17 '20 at 18:58
  • @DontBe3Greedy nothing, can't concatenate int + str – Chris Python Feb 17 '20 at 18:59
  • Do you mean to concatenate if its a number and a string but add if its a number and a number? – DontBe3Greedy Feb 17 '20 at 19:07
  • 1
    This question has [major XY-problems](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). I think before we can start to help in the long run, we actually need to know what you're trying to accomplish here. Your question goes multiple ways without defining a definite question. Is your question about `error-handling`, about `class-structures` or about `variable-types`? Or is the question just "why doesn't my code work"? – Hampus Larsson Feb 17 '20 at 19:22
  • It was more of a comment that you should use `a + b` after implementing the `__add__` method instead of `a.__add__(b)`. I screwed up my link and it's too late to edit the comment so [here's](https://stackoverflow.com/questions/20507745/overloading-addition-subtraction-and-multiplication-operators) what I _meant_ to link. – b_c Feb 18 '20 at 17:09

2 Answers2

0

I think I have found the answer.

class Vector():
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def __valuecheck__(self):
        try:
            self.x + "a"
        except TypeError:
            return str(self.x)
    def __repr__(self):
        return "Vector({},{})".format(self.x,self.y)
    def __add__(self, other):
        mvbh = str(self.x), str(self.y) # My Vector Before Hand
        myVector = ''.join(mvbh)

        ovbh = str(other.x), str(other.y) # Other Vector Before Hand
        otherVector = ''.join(ovbh)

        final = "Vector({}, {})".format(myVector, otherVector) # Change this to create a new vector

        print(final)


a = Vector(1,"a")
b = Vector("a",2)
a.__add__(b)
0
class Vector():
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def __valuecheck__(self):
        try:
            self.x + "a"
        except TypeError:
            return str(self.x)
    def __add__(self, other):
        return Vector(str(self.x) + str(other.x), str(self.y) + str(other.y))
    def __repr__(self):
        return "Vector({},{})".format(self.x,self.y)

a = Vector(1,"a")
b = Vector("a",2)
c = a.__add__(b)
print(c)
DontBe3Greedy
  • 564
  • 5
  • 12
  • Thanks for your answer, but this is not right because i don't perform value check at all, and if i have 2 figures they end up being strings – Chris Python Feb 17 '20 at 19:04
  • 2
    @ChrisPython your question is very ambigious then. Re-edit you question. what are you trying to do – DontBe3Greedy Feb 17 '20 at 19:06
  • @ChrisPython What does that even mean? If you're not checking the value, and always expects it to append new values to the end of your `xy`pair, then why not just create them as strings inside of the `__init__` method? – Hampus Larsson Feb 17 '20 at 19:08
  • @HampusLarsson yeah his question is very confusing – DontBe3Greedy Feb 17 '20 at 19:08