Solved – valueerror: could not convert string to float:

In this article, you will learn how to solve valueerror: could not convert string to float: error in Python. Let’s look at a code example that…

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 Deven

In this article, you will learn how to solve valueerror: could not convert string to float: error in Python.

Let’s look at a code example that produces the same error.

Bitcoin_value = float(input("Enter the Bitcoin you want to convert to USD: "))
exchange_rate = 51000
Converted_value = Bitcoin_value * exchange_rate
print("{} Bitcoin is equal to {} USD".format(Bitcoin_value, Converted_value))

Output

Enter the Bitcoin you want to convert to USD: 3,99
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    Bitcoin_value = float(input("Enter the Bitcoin you want to convert to USD: "))
ValueError: could not convert string to float: '3,99'


Our code runs without any error but if we try to insert a number into our code that is not formatted correctly we get the above error.

To fix valueerror: could not convert string to float: error in Python we can use a try…except block. As shown below:

Bitcoin_value = float(input("Enter the Bitcoin you want to convert to USD: "))
exchange_rate = 51000
Converted_value = Bitcoin_value * exchange_rate
print("{} Bitcoin is equal to {} USD".format(Bitcoin_value, Converted_value))
except:
	print("Please insert a valid number. Don't include any commas, spaces, or characters.")

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 Deven


Print Share Comment Cite Upload Translate Updates
APA

Deven | Sciencx (2021-03-10T17:27:25+00:00) Solved – valueerror: could not convert string to float:. Retrieved from https://www.scien.cx/2021/03/10/solved-valueerror-could-not-convert-string-to-float-2/

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

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.