I simply want to continue asking for input until it becomes valid.
def IsValidInput(UserInput): # We hate string return False if it is string.
if type(UserInput) == str: return False; return True
temp = 0 # By default, the value is 0.
while(True):
temp = float(input("Enter a valid input between 1 and 100: ")) # Float and Integer are accepted.
if IsValidInput(temp) and 1 <= temp <= 100:
break # break the loop on correct input.
else:
print("Not a valid input, please try again.") # Continue with the loop.
....
But I keep having an error of ValueError: could not convert string to float:
everytime I pressed enter on my keyboard(no input) or inputting a string.
Even with this I always get the same error.
def IsValidInput(UserInput):
try:
float(UserInput)
except ValueError:
return False
else:
return True
How can I check for a valid input then? I'm confused.
I'm using Python3