TypeError: can not multiply sequence by non-int of type 'str'

0

I am developing a small exercise of the implementation of a class oriented to handling fractions with their respective methods, then I must use it to make a small calculator, the module with the class works perfect and I have tested each of the methods separately . In another file, I import the module and use it to make the program, in summary mode, I copy the code:

a=Fraccion(raw_input("n/n?"))
b=Fraccion(raw_input("n/n?"))
print(str(a+b))

The functions created in the Fraction class, which are occupied in this small extract are:

#entrega la suma de dos fracciones
def __add__(self,x):
    num=self.__numerador * x.__denominador + self.__denominador * x.__numerador
    den=self.__denominador * x.__denominador
    return Fraccion(num,den).simple()

#entrega una fraccion representada en string
def __str__(self):
    return str(self.__numerador) + '/' + str(self.__denominador)

The exact error is:

File "/Users/xxxxxxxxx/PycharmProjects/untitled/clase16.py", line 43,     in __add__
num=self.__numerador * x.__denominador + self.__denominador * x.__numerador
TypeError: can't multiply sequence by non-int of type 'str'

Thank you very much for the help:)

[Update]:

class Fraccion:
    # ...
    def __init__(self, x=0, y=1): 
        if type(x)==str: 
            i=x.find("/") 
            self.__numerador=x[0:i] 
            self.__denominador=x[i+1:] 
        else: 
            self.__numerador=x 
            self.__denominador=y 
            assert self.__denominador!=0
    # ...
    
asked by Rudy Garcia 21.11.2016 в 03:43
source

1 answer

1

It seems that objeto.__numerador u objeto.__denominador are sequences and strings. That is, you are mixing types and a sequence can not be multiplied with a string. If you show the initializer or the complete code of class Fracción we could know exactly where the error occurs.

On the other hand, you use the tag

answered by 21.11.2016 в 09:55