Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

I am newbie programmer trying to make an irc bot that parse xml and paste its content on a channel. Usually i find my answer on google, but this time i can't find my answer.

q0tag = dom.getElementsByTagName('hit')[0].toxml()
q0 = q0tag.replace('<hit>','').replace('</hit>','')

q1 = (q0 * 1.2)

when i'm trying to multiply q0 it always showing

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

Im trying to make q0 int or float but it just make another error

AttributeError: 'NoneType' object has no attribute 'replace'

q0 value is a round number without decimal.

like image 534
user1695285 Avatar asked Sep 24 '12 18:09

user1695285


People also ask

How do you multiply a sequence by a non-int of type float?

An error called "TypeError can't multiply sequence by non-int of type 'float'" will be raised. The easiest way to resolve this is by converting the string into a float or integer and then multiplying it. Due to which while doing the multiplication between string and float variable it raised an error.

How do you multiply a float in Python?

Use the multiplication operator to multiply an integer and a float in Python, e.g. my_int * my_float . The multiplication result will always be of type float .

What is non-int of type float?

This means that we're trying to perform an operation on a value whose data type does not support that operation. For instance, if you try to concatenate an integer and a string, a type error is raised. The error is telling us that we're multiplying a sequence, also known as a string , by a floating-point number .

Can you multiply float and int?

The result of the multiplication of a float and an int is a float . Besides that, it will get promoted to double when passing to printf . You need a %a , %e , %f or %g format. The %d format is used to print int types.


1 Answers

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.

like image 88
jterrace Avatar answered Oct 01 '22 06:10

jterrace