How to Fix TypeError: can’t multiply sequence by non-int of type ‘float’

Diagram of How to Fix TypeError: can't multiply sequence by non-int of type 'float'

Diagram

TypeError: can’t multiply sequence by non-int of type ‘float’ error occurs when “you use the input() function, which takes user input”.  The input() function returns user input as a “string” by default.

To fix the TypeError: can’t multiply sequence by non-int of type ‘float’ error, “convert the string into a floating-point number using float() function before multiplying it with a float”.

When you try multiplying a float and a value, not an integer or string, you will encounter a TypeError can’t multiply sequence by non-int of type ‘float’ error.

Reproducing the error

user_input = input("Please enter your age: ")

print(user_input * 0.5)

Output

Please enter your age: 18

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

How to fix the error

You can use the “float()” function to convert the string data type and multiply it with a float value.

import math

# Get user input and convert it to a float
user_input = float(input("Please enter your age: "))

print(user_input * 0.9)

Output

Please enter your age: 34

30.6

In this code, we converted user input into a float and multiplied it by the float value. This way, you avoid the TypeError: can’t multiply sequence by non-int of type float error when working with user input.

That’s it!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.