float() argument must be a string or a real number, not X

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# Table of Contents

  1. float() argument must be a string or a real number, not list
  2. float() argument must be string or real number, not NoneType
  3. float() argument must be string or real number, not method

# float() argument must be a string or a real number, not list

The Python "TypeError: float() argument must be a string or a real number, not 'list'" occurs when we pass a list to the float() class.

To solve the error, access a specific item in the list and pass the item to the float() class, e.g. float(my_list[0]).

float argument must be a string or real number not list

Here is an example of how the error occurs.

main.py
my_list = ['1.1', '2.2', '3.3'] # โ›”๏ธ TypeError: float() argument must be a string or a real number, not 'list' result = float(my_list)

We passed an entire list to the float() class which caused the error.

# Access the list at a specific index to solve the error

One way to solve the error is to access the list at a specific index and pass the item to the float() class.

main.py
my_list = ['1.1', '2.2', '3.3'] result = float(my_list[0]) print(result) # ๐Ÿ‘‰๏ธ 1.1

access list at specific index

We accessed the list at index 0 and used the float() class to convert the value to a floating-point number.

Indices are 0 based, so the first item in the list has an index of 0, and the last has an index of -1.

# Converting all values in a list to floating-point numbers

If you meant to convert all items in the list to floating-point numbers, use a list comprehension.

main.py
my_list = ['1.1', '2.2', '3.3'] new_list = [float(x) for x in my_list] print(new_list) # ๐Ÿ‘‰๏ธ [1.1, 2.2, 3.3]

converting all values in list to floats

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

We pass each string in the list to the float() class to convert each item to an integer.

Alternatively, you can use the map() function.

main.py
my_list = ['1.1', '2.2', '3.3'] new_list = list(map(float, my_list)) print(new_list) # ๐Ÿ‘‰๏ธ [1.1, 2.2, 3.3]

using the map function instead

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

We passed the float() class to map(), so the class gets passed each value in the list as an argument and converts it to a floating-point number.

The last step is to convert the map object to a list.

  1. float() argument must be string or real number, not NoneType
  2. float() argument must be string or real number, not method

# float() argument must be string or real number, not NoneType

The Python "TypeError: float() argument must be a string or a real number, not 'NoneType'" occurs when we pass a None value to the float() class.

To solve the error, correct the assignment or provide a fallback value.

float argument must be a string or real number not nonetype

Here is an example of how the error occurs.

main.py
example = None # โ›”๏ธ TypeError: float() argument must be a string or a real number, not 'NoneType' result = float(example)

We are passing a None value to the float() class which causes the error.

# Common sources of None in Python

The most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

# Providing a default value if the variable is None

One way to solve the error is to provide a fallback value, e.g. 0 if the variable stores None.

main.py
example = None result = float(example or 0) print(result) # ๐Ÿ‘‰๏ธ 0.0

providing default value if variable is none

The expression checks if the example variable stores a falsy value, in which case 0 is returned.

# Functions that don't return a value return None

Functions that don't explicitly return a value return None.

main.py
# ๐Ÿ‘‡๏ธ this function returns None def get_str(): print('3.14') # โ›”๏ธ TypeError: float() argument must be a string or a real number, not 'NoneType' result = float(get_str())

You can use a return statement to return a value from a function.

main.py
def get_str(): return '3.14' result = float(get_str()) print(result) # ๐Ÿ‘‰๏ธ 3.14

# Checking whether a variable doesn't store a None value

Use an if statement if you need to check whether a variable doesn't store a None value before passing it to the float() class.

main.py
example = None if example is not None: result = float(example) print(result) else: # ๐Ÿ‘‡๏ธ this runs print('variable stores a None value')

Alternatively, you can reassign the variable to a fallback value.

main.py
example = None if example is None: example = 0 result = float(example) print(result) # ๐Ÿ‘‰๏ธ 0.0

If the variable is equal to None, the if block runs and sets it to 0.

# A function that only returns a value if a condition is met

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
def get_num(a): if a > 15: return a my_num = get_num(10.5) print(my_num) # ๐Ÿ‘‰๏ธ None

The if block in the get_num function is only run if the passed-in number is greater than 15.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To solve the error, you either have to check if the function didn't return None or return a default value if the condition is not met.

main.py
def get_num(a): if a > 15: return a return 0 # ๐Ÿ‘ˆ๏ธ Return a fallback if condition not met my_num = get_num(10.5) print(my_num) # ๐Ÿ‘‰๏ธ 0

Now the function is guaranteed to return a value regardless of whether the condition is met.

# float() argument must be string or real number, not method

The Python "TypeError: float() argument must be a string or a real number, not 'method'" occurs when we pass a method to the float() class.

To solve the error, make sure to call the method with parentheses, e.g. my_method().

float argument must be a string or real number not method

Here is an example of how the error occurs.

main.py
class MyClass(): def get_str(self): return '3.14' m = MyClass() # โ›”๏ธ TypeError: float() argument must be a string or a real number, not 'method' result = float(m.get_str) # ๐Ÿ‘ˆ๏ธ Forgot to call the method
We forgot to call the method with parentheses, e.g. m.get_str(), so our code actually tries to convert a method to a floating-point number.

# Call the method to solve the error

To solve the error, make sure to call the method.

main.py
class MyClass(): def get_str(self): return '3.14' e = MyClass() # โœ… Call the method() with parentheses result = float(e.get_str()) print(result) # ๐Ÿ‘‰๏ธ 3.14

We used parentheses to invoke the method, so now our code converts the return value of the method to a float.

If your method takes arguments, make sure to provide them when calling it, e.g. my_obj.my_method(10, 20).

# Checking what type the variable stores

If you aren't sure what type of object a variable stores, use the type() class.

main.py
class MyClass(): def get_str(self): return '3.14' e = MyClass() print(type(e.get_str)) # ๐Ÿ‘‰๏ธ <class 'method'> print(callable(e.get_str)) # ๐Ÿ‘‰๏ธ True my_float = 3.14 print(type(my_float)) # ๐Ÿ‘‰๏ธ <class 'float'> print(isinstance(my_float, float)) # ๐Ÿ‘‰๏ธ True

The type class returns the type of an object.

The isinstance() function returns True if the passed-in object is an instance or a subclass of the passed-in class.

The callable function takes an object as an argument and returns True if the object appears callable, otherwise, False is returned.

If the callable() function returns True, it is still possible that calling the object fails, however, if it returns False, calling the object will never succeed.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev