While strings can be multiplied by integers to create a repeating sequence, strings cannot be multiplied by floats. Otherwise, Python returns an error.

In this article, we’re going to talk about the “typeerror: can t multiply sequence by non-int of type ‘float’” error and why it is raised. We’ll walk through an example scenario with this error present so that we can solve it.

## typeerror: can’t multiply sequence by non-int of type ‘float’

Let’s take a look at our error message and see what it tells us:

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

Our error is a TypeError. 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.

- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses

The error is telling us that we’re multiplying a sequence, also known as a string, by a floating-point number. This is not supported in Python.

There are two types of numbers in Python: integers and floating-point numbers. Integers are whole numbers whereas floating-point numbers are decimals.

Strings can be multiplied by integers. Consider this example:

scone = "Scone" print(scone * 3)

Our code returns: SconeSconeScone. When you multiply a string by an integer, it creates a repeating sequence of that string.

Strings cannot be multiplied by floating point numbers. If you tried to multiply our “scone” string by 3.3, what would Python do? You can’t have .3 of a string. Hence, an error is returned.

## An Example Scenario

This error is commonly found when working with input() statements. Let’s take a look at a program that calculates a 5% discount on a purchase made at a store.

value = input("How much has the customer spent? ") discount = 0.05 final_cost = value - (value * discount) print(round(final_cost, 2))

We’ve declared a variable called value which stores how much the customer has spent on a purchase. This value is collected from the user using the input() method.

Next, we have declared a variable called discount. This stores the 5% discount that we are going to apply to purchases as a decimal number. We then calculate the percentage discount by multiplying “value” and “discount” together. We then subtract this number from the total cost of the product.

We use the round() method to round the value of “final_cost” to two decimal places. Then, we print this value to the console.

Let’s try to run our code:

How much has the customer spent? 12.99 Traceback (most recent call last): File "main.py", line 4, in <module> final_cost = value - (value * discount) TypeError: can't multiply sequence by non-int of type 'float'

Oh no. An error has been returned. Let’s fix this error.

## The Solution

The error “typeerror: can’t multiply sequence by non-int of type ‘float’” is caused by multiplying a string and a floating-point number together.

This error occurred in our earlier program because input() returns a string. This means that even if we insert a number into our program it will be stored as a string.

To solve this problem, we can convert the value the user inserts into the program to a floating-point number. We can do this using the float() method:

value = float(input("How much has the customer spent? ")) discount = 0.05 final_cost = value - (value * discount) print(round(final_cost, 2))

The float() method is surrounded by the input() method. The float() method converts the string value returned by input() into a floating-point number. This allows us to multiply “value” and “discount” because they are both numbers.

Let’s try to run our code again:

How much has the customer spent? 12.99 12.34

Our code works! Our program tells us that a 5% discount on the value of a $12.99 purchase makes the cost of the final product $12.34.

## Conclusion

Strings cannot be multiplied by floating-point numbers. This is because multiplying strings by integer numbers creates a repetitive sequence of the string. This is not possible using a floating-point because it would result in multiplying a string by decimal values.

To solve the “typeerror: can’t multiply sequence by non-int of type ‘float’” error, make sure that all string values are converted to a floating-point number if they are being used as part of a calculation.

Now you’re ready to solve this error like a Python expert!

**About us: **Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.