Skip to content
Advertisement

Python ValueError: could not convert string to float: ‘exit’

The below code works for integers, negative and decimal numbers. The issue is that Id like it to jump to the else statement below if someone types exit or if someone types any string to either exit the program or give an I dont understand message. Appreciate the help.

JavaScript

Advertisement

Answer

Try this. It converts the input to string, then tries to convert it to float (both integers and floats can be converted to floats, hence it checks if it is numeric).
If it can’t be converted to float, then it is a string and stays that way for the if-else statements.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement