Python Forum
How to properly catch this exception - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to properly catch this exception (/thread-27102.html)

Pages: 1 2


RE: How to properly catch this exception - Emekadavid - May-28-2020

(May-27-2020, 08:00 AM)buran Wrote: It's better to be specific as to what problem is
while True : 
    score = input('Number between 0 and 1\n')
    try :
        score = float(score)
        if 0 <= score <= 1:
            break
        else:
            print('Number must be between 0 and 1, inclusive. Please try again...') 
    except ValueError:
        print('Not a number. Please try again...')

print(f'You entered: {score}')
Now, if you want to go a step further you can put [part of] this code in a function that takes start and or end argument, ask user for input, validates it and returns the number once correct input.

Thanks. I will implement this suggestion. It's more intuitive.