• Register
0 votes
217 views

Problem :

While submitting the Python program i am getting below error
python can't multiply sequence by non-int of type 'float'
6 5 3
6,930 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

9 7 4
38,600 points
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

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 16 views
16 views
Problem: While strings can be multiplied by integers to create a repeating sequence, strings cannot be multiplied by floats. Otherwise, Java returns an error.I was going to talk about the &ldquo;typeerror: can t multiply sequence by non-int of type &lsquo;float&rsquo;&rdquo; error and why it is raised. We&rsquo;ll walk through an example scenario with this error present so that how can i solve it.
asked Dec 2, 2020 Mashhoodch 6.1k points
0 votes
1 answer 218 views
218 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
0 votes
1 answer 9 views
9 views
Problem: Hi! I am new to the world of programming. I am trying to multiply two numbers that will be entered by the user. I wrote the following program: number1 = input("Enter first number: ") number2 = input("Enter second number: ") print(number1," * ",number2," = ", ... = ",number1*number2) TypeError: can't multiply sequence by non-int of type 'str' I have no idea what I am doing wrong. Any help?
asked Dec 21, 2020 Code Learner 5.5k points
0 votes
1 answer 8 views
8 views
Problem: Hi there! I have been working in Python for a while. I am trying to run following program: value = 4.0 for val in value: number = float(input("Enter a number: ")) if number == 0: print("Zero") elif number > 0: print("Positive") elif ... understand what is causing this error. I want to take input from user unless val becomes 4. Could anyone from here please help me solve this error?
asked Jan 15 Code Learner 5.5k points
0 votes
1 answer 44 views
44 views
Problem: Hi there! I am new to the world of Python, and I am just trying new things in programming. I learned somewhere about the split() method and tried the following program: number = 43.42 another = number.split() print(another) When I executed this program ... numbers before and after the decimal point. I have no idea what caused this error. Can anyone here help me solve the problem, please?
asked Jan 4 Code Learner 5.5k points
0 votes
1 answer 3 views
3 views
Problem: I am creating a program that reads a file and if the first line of the file is not blank, it reads the next four lines. Calculations are performed on those lines and then the next line is read. If that line is not empty it continues. However, I am getting this error: ... with base 10: ''. It is reading the first line but can't convert it to an integer. What can I do to fix this problem?
asked 2 days ago Mashhoodch 6.1k points
0 votes
1 answer 3 views
3 views
Problem: I have a dataframe created form a JSON output that looks like this: Total Revenue Average Revenue Purchase count Rate Date Monday 1,304.40 CA$ 20.07 CA$ 2,345 1.54 % The value stored are received as string from the JSON. I am trying to: 1) Remove all characters in the ... count columns as int I tried to do the following: df[column] = (df[column].str.split()).apply(lambda x: float(x[0]))
asked 1 day ago Mashhoodch 6.1k points
0 votes
1 answer 8 views
8 views
Problem: Hi there! I am a new programmer, and I have started programming in Python just recently. I am facing a little difficulty in getting used to Python syntax. I am trying to run the following code: pi = "3.14"; floating = float(input("Enter a floating-point ... to subtract a value from the value of pi and print the answer. Could anybody here please point out the mistake I am making? Thanks!
asked Jan 8 Code Learner 5.5k points
0 votes
1 answer 17 views
1 vote
1 answer 20 views
20 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, 2020 Gavin 15.3k points