Python Forum
TypeError: can't multiply sequence by non-int of type 'float'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: can't multiply sequence by non-int of type 'float'
#1
Hi guys I'm having a problem with an assignment in my class we are now on using fullmatrix and sparce and I get this error in my code :

class Vector(object):
    def __init__(self, data):
        self.data = data
        self.rows = len(data)

    def __mul__(self, other):
        assert self.rows == other.rows
        return sum([vi * vj for vi, vj in zip(self.data, other.data)])

    def __rmul__(self, a):
        data = [a * d for d in self.data]
        return Vector(data)
    
    def __add__(self, other):
        assert self.rows == other.rows
        return Vector([i + j for (i, j) in zip(self.data, other.data)])

    def __sub__(self, other):
        assert self.rows == other.rows
        return Vector([i - j for (i, j) in zip(self.data, other.data)])

    def norm(self):
        return math.sqrt(self * self)

    def __str__(self):
        return '{0}'.format(self.data)
Error:
File "C:\Users\illar\Desktop\05.py", line 116, in <listcomp> data = [a * d for d in self.data] TypeError: can't multiply sequence by non-int of type 'float'
Reply
#2
You should provide enough code to reproduce the issue. As-is, things are missing. You'll be more likely to get a fast response if you remove unnecessary details though.
Reply
#3
Your __rmul__ method seems to try to multiply two things together without checking if that's appropriate. If one of the factors is a sequence and the other is float, you'll get this error.

>>> ['a', 'b', 'c'] * 3.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
Either fix your inputs or have your method massage a float into an int if that's more appropriate.
Reply
#4
Change the "3.0" to just "3". Floats are decimal numbers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: 'float' object is not callable #1 isdito2001 1 1,071 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  Write Null values as 0.0 (float) type in csv mg24 3 1,355 Dec-07-2022, 09:04 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,423 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,277 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,835 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,138 May-07-2022, 08:40 AM
Last Post: ibreeden
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 5,088 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  Why can't I explicitly call __bool__() on sequence type? quazirfan 11 4,621 Aug-20-2021, 06:49 AM
Last Post: Gribouillis
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 2,998 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,067 Apr-13-2021, 10:33 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020