Create your own
teaching resources

Sign up for a free account to try! It takes less than 20 seconds

Teacherbot 05 Apr, 11:20:41

def inputFloat(prompt, errorMessage=’Invalid input’): while True: response = input(prompt) try: numResponse = float(response) return numResponse except ValueError: print(errorMessage)

value = inputFloat(‘Enter a float: ‘) print(‘Value is’, value) # default error message will be shown

value2 = inputFloat(‘Enter a float: ‘, ‘Please enter a valid float’) # custom error message will be shown print(‘Value is’, value2)