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


How to properly catch this exception - Emekadavid - May-26-2020

I wanted to write a little code that takes in a number. If the number is within a specified range, 0.0 to 1.0, no exception is thrown. But if it is outside the range, the program should prompt the user to input another number. I don't seem to be getting the prompting right when the exception happens. Instead of prompting the user for a new number, the code just fails. Please can anyone tell me what is wrong? The code is below.
Score = input('enter number between 0 and 1\n')
try : 
    Score = float(Score)
    if Score >= 0.0 and Score <=1.0 :
        print('correct number')
except:
    print('enter correct number')



RE: How to properly catch this exception - pyzyx3qwerty - May-26-2020

The way you use except is wrong - The except block lets you handle the error. So for example
try:
  print(x)
except NameError:
  print("Variable x is not defined")
except:
  print("Something else went wrong")
Here, the variable x is not defined, and therefore, a Nameerror will come like
Error:
Variable x not defined
like that
here, even if the input is greater than 1, an error will not come.
You will have to use a while loop like
while Score > 1:
    Score = input('enter number between 0 and 1\n')
Then print your rest of the code. The while loop will work as long as your score is greater than 1. When it becomes less than 1, it will automatically break


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

Thanks. Am going back to the code to implement your suggestion.


RE: How to properly catch this exception - buran - May-26-2020

Couple of suggestions:
1. use while True: and break to exit the loop when valid inpu
2. Don't use all-catching except. Be specific - in this case you want to catch ValueError, so say so
3. What happens if input is number but outside of the desired range? No error is raised, but you can display respective message. If input is number and inside the range - just break out of the loop


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

Thanks everyone. I finally did it. So happy.
score = 0
while True : 
    score = input('number btw 0 and 1\n')
    try :
    	score = float(score)
    	if score>=0 and score<=1:
    		break
    except :
    	print('not number')
    



RE: How to properly catch this exception - perfringo - May-26-2020

To improve readability one can also write:

if 0 <= score <= 1:
...and all-catching except is still bad.


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

I have not gotten a firm hold on the except yet. Next time I will specify it. Thanks


RE: How to properly catch this exception - jefsummers - May-26-2020

This may get you closer
while True : 
    score = input('number btw 0 and 1\n')
    try :
        score = float(score)
        if score<=0 or score>=1:
            raise ValueError
    except ValueError:
        print('not number, at least not in range')
The part we haven't answered is how to raise an error. This is how. With it you will get an error to trigger the except block whether the person enters "5" or "a".


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

(May-26-2020, 09:00 PM)jefsummers Wrote: This may get you closer
while True : 
    score = input('number btw 0 and 1\n')
    try :
        score = float(score)
        if score<=0 or score>=1:
            raise ValueError
    except ValueError:
        print('not number, at least not in range')
The part we haven't answered is how to raise an error. This is how. With it you will get an error to trigger the except block whether the person enters "5" or "a".

I think this is elegant. This wraps up everything. Thanks


RE: How to properly catch this exception - buran - May-27-2020

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.