• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

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

daniyal007

Token Distribution Expert
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
67
Likes
168
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
python-te-cant-multiply-banner.jpg


If you are facing a Python Type Error that says Python TypeError: can't multiply sequence by non-int of type float. This article can help you understand why this error occurs and how to fix this.

Why does "Python TypeError: can't multiply sequence by non-int of type float" occurs?

The "Python TypeError: can't multiply sequence by non-int of type float" occurs when we are trying to multiply a string with a floating-point number value.

In Python, there are 2 types of numbers which are integer and floating-point numbers. The Integer is a positive or negative whole number including zero while floating-point numbers are numbers that have a decimal point and can also be positive or negative. Multiplying in Python sometimes confusing especially to beginners because in this language, we can multiple stings with the integer that result in a repeating sequence of characters. For example:

  1. value1 =

    "SourceCodester \n

    "
  2. value2 =

    2

  3. print

    (

    value1 * value2)
  4. # The result will output something like
  5. # Sourcecodester
  6. # Sourcecodester

The script above will result in the following image:

Because of that, it is possible for some developers to forget or unintentionally write a code something like the following script:

  1. value1 =

    "20"
  2. value2 =

    2

  3. print

    (

    value1 * value2)

Which results in the following image:

If did not notice this and proceed execution of the code like the following:

  1. value1 =

    "20"
  2. value2 =

    5.5

  3. print

    (

    value1 * value2)

It will raise a Python Type Error that says "can't multiply sequence by non-int of type 'float'" like the image below:

Solution

The TypeError: can't multiply sequence by non-int of type 'float' error can be easily fixed by converting the string into an integer or float. Then, when it was successfully converted the multiplication equation that we want to execute will return the correct product.

Here is an example of the fixed Python script:

  1. value1 =

    "20"
  2. value2 =

    5.5

  3. print

    (

    int

    (

    value1)

    * value2)
  4. #output: 110.0

Or

  1. value1 =

    "101.5"
  2. value2 =

    25.5

  3. print

    (

    float

    (

    value1)

    * value2)
  4. #output: 2588.25

There you go! That is we can fix the "Python TypeError: can't multiply sequence by non-int of type 'float'" and prevent this from occurring in your future projects.

That's it! I hope this article helps you to understand the Python Type Error that says can't multiply sequence by non-int of type 'float' and you'll find this useful for your current and future Python Projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,501

351,357

351,371

Top