Solved – ValueError: could not convert string to float

In this article, you will learn how to solve ValueError: could not convert string to float. Let’s look at code examples that produce the same…

The post Solved – ValueError: could not convert string to float appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli

In this article, you will learn how to solve ValueError: could not convert string to float.

Let’s look at code examples that produce the same error.

float('')
# Traceback (most recent call last):
#   File "example.py", line 2, in <module>
# ValueError: could not convert string to float: ''

float('1,200')
# Traceback (most recent call last):
#   File "example.py", line 6, in <module>
# ValueError: could not convert string to float: '1,200'
float('fd')
# Traceback (most recent call last):
#   File "example.py", line 11, in <module>
# ValueError: could not convert string to float: 'fd'

How to Solve ValueError: could not convert string to float

In order to solve it, you can use the try...except block.

a = 123

try:
    float(a)
    print("Input is a valid number")
except:
	print("Input must be a valid number. Non-special characters, commas and spaces are not accepted.")

The post Solved – ValueError: could not convert string to float appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli


Print Share Comment Cite Upload Translate Updates
APA

Ariessa Norramli | Sciencx (2021-03-07T11:02:37+00:00) Solved – ValueError: could not convert string to float. Retrieved from https://www.scien.cx/2021/03/07/solved-valueerror-could-not-convert-string-to-float/

MLA
" » Solved – ValueError: could not convert string to float." Ariessa Norramli | Sciencx - Sunday March 7, 2021, https://www.scien.cx/2021/03/07/solved-valueerror-could-not-convert-string-to-float/
HARVARD
Ariessa Norramli | Sciencx Sunday March 7, 2021 » Solved – ValueError: could not convert string to float., viewed ,<https://www.scien.cx/2021/03/07/solved-valueerror-could-not-convert-string-to-float/>
VANCOUVER
Ariessa Norramli | Sciencx - » Solved – ValueError: could not convert string to float. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/07/solved-valueerror-could-not-convert-string-to-float/
CHICAGO
" » Solved – ValueError: could not convert string to float." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/03/07/solved-valueerror-could-not-convert-string-to-float/
IEEE
" » Solved – ValueError: could not convert string to float." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/03/07/solved-valueerror-could-not-convert-string-to-float/. [Accessed: ]
rf:citation
» Solved – ValueError: could not convert string to float | Ariessa Norramli | Sciencx | https://www.scien.cx/2021/03/07/solved-valueerror-could-not-convert-string-to-float/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.