Home > Software engineering >  input. check if value is float if not go back to input until a float is written. I fail. "Can n
input. check if value is float if not go back to input until a float is written. I fail. "Can n

Time:12-03

I have a school assignment where im making a budget calcylator. One of the demands are that the program checks if the input is a float, if not go back until a float is written. Im having a super hard time solving this. Ive been doing python one month so my skills are very limitied. Its hard to google on.

x = float(input('nr'))
isinstance(x, float)

CodePudding user response:

You could do something like this:

while True:
    try:
        x = float(input('Enter a number: '))
        break
    except ValueError:
        print('Invalid input. Please try again.')

This code uses a while loop to continuously prompt the user for input until a valid float is entered. The try and except statements are used to handle the potential ValueError that can be raised when trying to convert an invalid input to a float.

CodePudding user response:

Great! This seems to be a really good solution! Perfect! Thank you!

  • Related