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
#10
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: How to properly catch this exception - by buran - May-26-2020, 10:42 AM
RE: How to properly catch this exception - by buran - May-27-2020, 08:00 AM

Forum Jump:

User Panel Messages

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