Python for Everybody - More Conditional Structures

Error in Code Question – Float Conversion from String

I found a small issue in the code:

temp = “5 degrees”
fahr = float(temp)
cel = (fahr - 32.0) * 5.0 / 9.0
print(cel)

The line float(temp) gives a ValueError because “5 degrees” is not a valid float due to the text

A simple fix would be :

temp = “5 degrees”
fahr = float(temp.split()[0])
cel = (fahr - 32.0) * 5.0 / 9.0
print(cel)

the resault should be : -15.0

Challenge Information:

Python for Everybody - More Conditional Structures
https://www.freecodecamp.org/learn/python-for-everybody/python-for-everybody/more-conditional-structures

Please read the question carefully. The question is asking you which lines should be guarded with a try block to catch exactly this problem.