From Lab #1: Can someone explain how you would test if it's an integer or a list? In the example below, It keeps giving errors stating that ("other" is not a list), because it's an integer. class Polynomial: def __init__(self, coefficients): self.coeffs = coefficients def mul(self, other): newcoeffs = [] if len(other) == 1: for n in range(len(self.coeffs)): newcoeffs.append(self.coeffs[i] * other) return newcoeffs
It's pretty easy to check types in python. The built-in command, type() will spit out the type. So if you want to input a list... you could do something like "if type(other) == list", etc.
One thing you could try with your current setup popping the following immediately before your if statement. if type(other) != list: other = [other] Basically, if you're not giving it a list, it'll turn it into a 1-element list and then check your if statement how you might want it to. Might not be your golden ticket, but it's definitely the next step I'd make in debugging.
Join our real-time social learning platform and learn together with your friends!