Why Do I Get Typeerror: Can't Multiply Sequence by Non-Int of Type 'Float'

Why do I get TypeError: can't multiply sequence by non-int of type 'float'?

raw_input returns a string (a sequence of characters). In Python, multiplying a string and a float makes no defined meaning (while multiplying a string and an integer has a meaning: "AB" * 3 is "ABABAB"; how much is "L" * 3.14 ? Please do not reply "LLL|"). You need to parse the string to a numerical value.

You might want to try:

salesAmount = float(raw_input("Insert sale amount here\n"))

Python : TypeError: can't multiply sequence by non-int of type 'float'

Your q0 value is still a string. This is basically what you're doing:

>>> q0 = '3'
>>> q1 = (q0 * 1.2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'

To fix it, convert the string to a number first:

>>> q1 = (float(q0) * 1.2)
>>> q1
3.5999999999999996

You might also want to look into the lxml and BeautifulSoup modules for parsing XML.

can't multiply sequence by non-int of type 'float'

for i in growthRates:  
fund = fund * (1 + 0.01 * growthRates) + depositPerYear

should be:

for i in growthRates:  
fund = fund * (1 + 0.01 * i) + depositPerYear

You are multiplying 0.01 with the growthRates list object. Multiplying a list by an integer is valid (it's overloaded syntactic sugar that allows you to create an extended a list with copies of its element references).

Example:

>>> 2 * [1,2]
[1, 2, 1, 2]

Numpy gives TypeError: can't multiply sequence by non-int of type 'float'

Type Error: can't multiply sequence by non-int of type 'float' means exactly what it says. You cannot multiply a non-number(non-int) data type with a numbered data type.
For example, you cannot multiply strings with a number.

The above code you have submitted actually works. But, since you are getting this error, I assume the above code is just a proxy for your actual code. So, I'll tell how I solved the same error that I had gotten when I was doing something similar.


Let's say there is a csv file that contains the subject marks for a class like the table below

|      | Maths | English |
| Adam | 98 | 78 |
| John | 34 | 89 |

As you can see, there is a row index as well as a column index. If you run

marks = pd.read_csv("marks.csv")

marks will have 3 columns with the firs column containing the names of the students. Pandas read assumed the first column was part of the data. Now, if you multiply this with a Numpy array, you will get an error. Since, numbers can't be multiplied with strings.

To solve this, we need to explicitly tell pandas that the first column in the file is the row index.

marks = pd.read_csv("marks.csv", index_col=0)

The parameter, index_col tells which column in the file to take as the row index.
You can read about this in detail in their documentation, here.

Can't multiply sequence by non-int of type 'float' ERROR when trying to multiply a float number with len(list)

The second argument of random.sample have to be integer type.



Related Topics



Leave a reply



Submit