MAC OS X 10.13.6
Anaconda python 3.6.5
Anaconda IPython 6.4.0
I am having difficulty in using the try, except form.
I copy and paste a minimal program to illustrate my problem.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 2 15:41:15 2018
@author: sydney
"""
uvc = 2
msg = "Bad argument provided, the value of uvc must be a float."
try:
type(uvc) == float
except TypeError as e:
print(e, msg)
try:
0.0 > uvc < 1.0
except ValueError:
print("Bad argument provided. The value of uvc must be "
"greater than 0.0 and less than 1.0.")
if type(uvc) != float:
raise TypeError("Bad argument provided. And this is also old test."
" The value of UVC must be a float. This is old test")
if uvc < 0.0 or uvc > 1.0:
raise ValueError("Bad argument provided. The value of uvc must be "
"greater than 0.0 and less than 1.0. This is old
test")
My problem is this.
The two, 'if statements' at the end of the little program work as
intended. And I have been using them in a larger program for some time
and they have been checked regularly and they work as intended.
I am now rewriting a class, as recommended by both Alan and Steven, in
which I have previously used these formulations.
This time, I thought that perhaps it would be more "pythonic" to write
them as try, except. But I cannot find the correct format. I have read
and reread the books and the documentation, but I am clearly stupid
today. Either I do not need this change or I have written the try,
except incorrectly. I am sure that I am making an elementary mistake.
Will someone please show tell me whether I should use try, except
instead of if ... and then what is the correct syntax.
Thanks as always.
--
_________
Professor Sydney Shall
Department of Haematology/Oncology
Phone: +(0)2078489200
E-Mail: sydney.shall
[Correspondents outside the College should add @kcl.ac.uk]
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor