• Register
0 votes

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

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:


36.1k points
edited by
0 votes


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 
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

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 



15.3k points
edited by

Related questions

0 votes
1 answer 185 views
Problem : I'm new to python and I've been struggling with this problem for a while. I've search google and youtube but no luck so far. I've written this in IDLE 2.6.5. If someone could tell me why I'm getting the can't multiply sequence by non-int of type float error.
asked Oct 21, 2019 peterlaw 6.9k points
1 vote
1 answer 13 views
Problem: Hello mate I am in deep trouble. I want to generate a random float between 0 and 1 in a java program. What is the procedure to generate a random float number? Please share your thoughts on this. Thanks in advance.
asked Apr 6 Gavin 15.2k points
1 vote
1 answer 64 views
Problem: For the following code import re import string import pandas as pd import openpyxl import glob import csv import os import xlsxwriter import numpy as np keywords = {"oracles","mining","ico"} df = pd.read_csv(r"C:MYfile.csv", error_bad_lines=False) sep="," , ... j in df.columns] 21 TypeError: argument of type 'float' is not iterable Can anyone sortout the problem and help me to solve this?
asked Apr 26 samhaz 5k points
0 votes
2 answers 556 views
Problem : I am facing following type error while trying to execute the Python program deprecationwarning: object of type <class 'float'> cannot be safely interpreted as an integer.
asked Nov 14, 2019 peterlaw 6.9k points
1 vote
2 answers 32 views
Problem: Well, I am getting an error from my python program that says, argument of type 'int' is not iterable. Could anybody save my day by fixing the error? Find my sample code snippet below def duplicates(n): my_list = n[0] for i in range(1,len(n)): if n[i] in my_list: print "duplicate " + str(n[i]) else: out.append(n[i]) return my_list duplicates([4,5,5,4]) Thanks.
asked Apr 4 Gavin 15.2k points