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

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.

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.

» MORE:  Python TypeError: string index out of range Solution

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.

» MORE:  Python Pop: A Step-By-Step Guide

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.

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值