• Register
0 votes
454 views

Problem :

While submitting the Python program i am getting below error
python can't multiply sequence by non-int of type 'float'
6.9k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

I had faced the same issue while trying to multiple a float and a list of floats. The issue was actually due to the use of * operator on a list.

The solution is to convert the list to numpy array:

coff = np.asarray(coff) * C

Further Readings:

https://github.com/ccxt/ccxt/issues/2419

38.6k points
edited by
0 votes

Solution:

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

You might want to attempt:

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

Maybe this will support others in the future - I had the similat error while attempting to multiple a float and a list of floats. The thing is that everyone here talked about multiplying a float with a string (however here all my element were floats all along) so the problem was indeed employing the * operator on a list.

For example:

import math
import numpy as np
alpha = 0.2 
beta=1-alpha
C = (-math.log(1-beta))/alpha

coff = [0.0,0.01,0.0,0.35,0.98,0.001,0.0]
coff *= C

The error:

  coff *= C 
TypeError: can't multiply sequence by non-int of type 'float'

The resolution - convert the list to numpy array:

coff = np.asarray(coff) * C

The issue is that salesAmount is being set to a string. In case you enter the variable in the python interpreter and stroke enter, you'll glance the value entered encompassed by quotes. For example, in case you entered 56.95 you'd view:

>>> sales_amount = raw_input("[Insert sale amount]: ")
[Insert sale amount]: 56.95
>>> sales_amount
'56.95'

You can't multiply string and float.instead of you attempt as below.it performs fine

totalAmount = salesAmount * float(salesTax)

To get rid of this error you require to do some changes in your q0 value which is still a string. This is originally 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 solve this error you reqyire to convert the string to a number first by employing the below-mentioned way:

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

>>> q1 

3.5999999999999996

 

31.1k points
edited by