Python Forum
How to properly catch this exception
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to properly catch this exception
#11
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020