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
Reading a Text File and Splitting It into Single Words in Python
Check If Item Is in an Array/List
Pip Cannot Uninstall <Package>: "It Is a Distutils Installed Project"
Pandas Groupby and Select Rows with the Minimum Value in a Specific Column
Update a Dataframe in Pandas While Iterating Row by Row
Retrieve List of Tasks in a Queue in Celery
When and How to Use the Builtin Function Property() in Python
Tensorflow Read Images with Labels
Launch a Completely Independent Process
Seeing Escape Characters When Pressing the Arrow Keys in Python Shell
How to Add an Integer to Each Element in a List
Only Extracting Text from This Element, Not Its Children
How to Compare Multiple Variables to the Same Value
How to Convert a Time.Struct_Time Object into a Datetime Object
Python Assigning Multiple Variables to Same Value? List Behavior
C and Python - Different Behaviour of the Modulo (%) Operation