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
![](https://www.radiofree.org/wp-content/plugins/print-app/icon.jpg)
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.