Solve the Can't Multiply Sequence by Non-Int of Type 'float' Error in Python

Solve the Can't Multiply Sequence by Non-Int of Type 'float' Error in Python

  1. Use int() to Solve the TypeError: can't multiply sequence by non-int of type 'float' Error in Python
  2. Use float() to Solve the TypeError: can't multiply sequence by non-int of type 'float' Error in Python

Strings are a sequence of characters, and numbers can be seen as individual characters. Therefore, we can store numbers (int, float) within strings, which can cause problems.

When we use Python to receive input from the user, the characters from letters to numbers are processed as strings. With this comes certain behaviors and errors, especially TypeError, that can be annoying, especially among new programmers and Python developers.

When receiving numbers (integers, floats) input from users, understanding how to deal with such to allow for smooth and error-free operation is important.

In this article, we will discuss how to solve TypeError that involves operations between strings and numbers.

Use int() to Solve the TypeError: can't multiply sequence by non-int of type 'float' Error in Python

Among the many errors that can happen when working with strings and numbers, the can't multiply sequence by non-int of type 'float' is a popular one that many people face. This happens when you want to multiply a string with a float number.

Let’s take this piece of code that takes a user input, the sale amount, and multiplies the input by the sales tax (salesTax) to give the total amount.

salesAmout = input("Insert sale amount: ")
salesTax = 0.08
totalAmount = salesAmout * salesTax
print(totalAmount)

The above code will give the below error:

Traceback (most recent call last):
  File "C:\Users\akinl\Documents\html\python\type.py", line 3, in <module>
    totalAmount = salesAmout * salesTax
TypeError: can't multiply sequence by non-int of type 'float'

Let’s change the code by updating the salesTax variable to an integer 8.

salesAmout = input("Insert sale amount: ")
salesTax = 8
totalAmount = salesAmout * salesTax
print(totalAmount)

This code will give the below output when we input the value 3:

Insert sale amount: 3
33333333

The reason for the above output is the way string behaves with integers, but it also gives us an understanding of the TypeError message we got initially, can't multiply sequence by non-int of type 'float'. It means Python cannot multiply the float value within the salesTax variable to the user’s input, salesAmount, which is a string.

Aside from that, the code’s behavior when we change the salesTax is not what we want; therefore, we must find an optimal solution. That is where int() comes in.

The int() method converts a string to an integer as long as the string contains only integer values. A simple example is given below.

store = "123"
intStore = int(store)

print(store, intStore)
print(type(store))
print(type(intStore))

The output of the above code:

123 123
<class 'str'>
<class 'int'>

The code converts the value within the store variable to an integer, and we can check that using the built-in type function, type(). The store variable outputs str meaning string, and int meaning integer.

However, if we try to convert a string value that holds more than numbers, it will give an error. The below code snippet shows that:

holder = "12e"
print(int(holder))

The error it will give is below:

Traceback (most recent call last):
  File "c:\Users\akinl\Documents\HTML\python\convert.py", line 2, in <module>
    print(int(holder))
ValueError: invalid literal for int() with base 10: '12e'

The same error message will be outputted if you pass a float value stored within a string, as shown below.

holder = "12.3"
print(int(holder))

Therefore, ensure that the value you pass to the int() is a string with only numeric characters and, to be specific, integers.

Now that we are clear on the behavior and outputs of int(), let us use it to solve our can't multiply sequence by non-int of type 'float' problem.

Suppose you are sure that the sales amount will only be integers. In that case, we can convert the salesAmount to an integer using the int(), and Python will now have a multiplication expression of integer and float, which it has no problem executing.

salesAmout = int(input("Insert sale amount: "))
salesTax = 0.08
totalAmount = salesAmout * salesTax
print(totalAmount)

The output of the above code:

Insert sale amount: 12
0.96

We no longer have the can't multiply sequence by non-int of type 'float' problem.

Use float() to Solve the TypeError: can't multiply sequence by non-int of type 'float' Error in Python

Similar to int(), we have an alternative solution called the float() function that can be seen as more comprehensive. With the float() function, we can convert a string to decimal or floating-point numbers; therefore, we do not have to worry about whether the user passes an integer or floating-point number value.

Let’s try out the same sale amount using the same code with the float() modification.

salesAmout = float(input("Insert sale amount: "))
salesTax = 0.08
totalAmount = salesAmout * salesTax
print(totalAmount)

The output of the code:

Insert sale amount: 12
0.96

Now, let us input a float value (23.4):

Insert sale amount: 23.4
1.8719999999999999

Therefore, we are safer using the float() function. However, the int() function is more appropriate if we need only integer values.

Olorunfemi Akinlua avatar Olorunfemi Akinlua avatar

Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.

LinkedIn

Related Article - Python Error

  • Python PermissionError: [WinError 5] Access Is Denied
  • Python TypeError: 'DataFrame' Object Is Not Callable
  • Python TypeError: Can't Convert 'List' Object to STR
  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10