How to fix Typeerror: ‘float’ object is not subscriptable in Python (Causes and Fixes)

Typeerror: ‘float’ object is not subscriptable in Pythonoccurs only when any python statement invokes float as an iterable element in the loop, etc. In this article, we will investigate multiple scenarios where we face this type of error. We will understand the main cause of this error and find the solution to fix different scenarios.

What if Typeerror: ‘float’ object is not subscribtable?

Typeerror: 'float' object is not subscriptable

The ‘float’ object is not subscribable and appears when you try to access a float-type object using index numbers.

Nonsubscriptable objects are those whose items don’t allow using index numbers such as float, int, etc.

Examples of subscribable objects are strings, lists, and dictionaries. We can access items of strings, lists, or dictionaries using index numbers. The floating object is not indexable and you can’t access it using index numbers. 

Let us show you an example. 

# Program for finding area of a circle

radius = int(input("Enter radius of a circle :"))

pi=3.14

area = pi*radius*radius

print("area of the circle :",area[0])

Output

Enter radius of a circle :3

File "area.py", line 5, in <module>

print("area of the circle :",area[0])

TypeError: 'float' object is not subscribable

The “typeerror: ‘float’ object is not subscribable” is normally caused by:

  • Trying to access a specific value from a floating-point number
  • Applying the incorrect syntax to change multiple items in a list

Solutions for the Typeerror:  ‘float’ object is not subscribtable

Scenario #1: Accessing an Item from a Float

 

We’re building a program that verifies whether a raffle ticket holder is a winner. In case a user’s ticket number starts with 1 and ends with 7, they are a winner.

The 1st step is to ask a user to insert a ticket number that should be verified:

ticket_number = float(input("Enter a ticket number: "))

We’ve changed this value to float because it is a number.

And then we use indexing syntax to get back the first and last numbers on a ticket:

first = ticket_number[0]

last = ticket_number[-1]

As you can see, the first item in our ticket number is at the index position 0; the last item is at the index position -1. After that we use an “if” statement to check if a user is a winner:

if first == "1" and last == "7":

print("You are a winner!")

else:

print("You are not a winner.")

If the value of “first” is equal to “1”,  the value of “last” is equal to “7”, our “if” statement will show and inform the user that they are the winners. On the other hand, our “else” statement will show, which will inform the users that they are not winners.

Let’s try our code:

Enter a ticket number: 23

Traceback (most recent call last):

  File "main.py", line 3, in <module>

first = ticket_number[0]

TypeError: 'float' object is not subscriptable

Our code is not working because ticket numbers are collected as a float. We cannot get indexing syntax to retrieve individual values from a float.

To solve this error, you can remove the float() conversion from our first line of code:

ticket_number = input("Enter a ticket number: ")

You are not a winner.

Our code appears to work successfully, you can test our code on a winning ticket: 

Enter a ticket number: 117

You are a winner!

Our code still works even if a ticket is a winner or not. 

Scenario #2: Replacing Multiple Items

Typeerror: 'float' object is not subscriptable

List down slicing which allows you to replace multiple items. Slicing is where you specify a list of items in an iterable that you want to access or change.

Let’s create a program that resets the prices of all products in a list of donuts. We must build this program because a store is doing a “Donut Day” promotion where every donut is a special price. The new price of donuts will be provided by the users:

price = input("Enter the price of the donuts: ")

Next, we determine a list of the current prices of donuts that we must change:

donuts = [2.50, 2.75, 1.80, 1.75, 2.60]

Now we have these values, we’re ready to change our list. We can use indexing:

donuts[0][1][2][3][4] = [float(price)] * 5

print(donuts)

This code resets the values in the “donuts” list at the index positions 0, 1, 2, 3, and 4 to the value of “price”. We’ve changed the value of “price” to a floating point number.

We’ve then enclosed the price in square brackets and multiplied it by five. This creates a list of values that we can assign to our “donuts” list.

Finally, we print the new list of prices to the console. Let’s run our code:

Enter the price of the donuts: 2.50

Traceback (most recent call last):

  File "main.py", line 3, in <module>

donuts[0][1][2][3][4] = [float(price)] * 5

TypeError: 'float' object is not subscribable

When our code tries to change the values in the “donuts” list, an error is returned. Our code tries to retrieve the item at the index position [0][1][2][3][4]. This does not exist in our list because our list only contains floats. To solve this error, use slicing syntax to update our list. 

donuts[0:5] = [float(price)] * 5

This code retrieves all the items in our list from the range of 0 to 5 (exclusive of 5). Then, we assign each value the value of “price”. Let’s try our new code:

Enter the price of the donuts: 2.50

[2.5, 2.5, 2.5, 2.5, 2.5]

Our items have been replaced with the code successfully in the list.

Conclusion

The “typeerror: ‘float’ object is not subscribtable” issue appears will when you try to access items from a floating point number as if the number is indexed.

To solve this issue, please make sure you only use indexing or slicing syntax on a list of iterable objects. If you are trying to convert multiple values in a list, make sure you use slicing to do so instead of specifying a list of index numbers.  

After reading this article you maybe get more knowledge about the ‘float’ object is not subscribable” error. In case you have another thing would like to share, please contact us and share your ideas. Cooperating with you is an honor for ONEX DIGITAL!