Re: [Tutor] try, except syntax

2018-08-02 Thread Steven D'Aprano
On Thu, Aug 02, 2018 at 12:19:41PM -0700, Marc Tompkins wrote: > What you want in that case is an assertion: > > try: > assert type(uvc)==float > except AssertionError as e: > print(e, msg) > > An assertion says "The following statement is True. If it isn't, I'm going > to throw an exce

Re: [Tutor] try, except syntax

2018-08-02 Thread Alan Gauld via Tutor
On 02/08/18 15:29, Shall, Sydney wrote: > 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) The try block contains an expression that will always evaluate to True or False and thus never rai

Re: [Tutor] try, except syntax

2018-08-02 Thread Marc Tompkins
try... except is meant to catch errors: places where your program would otherwise crash. It does NOT work as a truth check. In your example: > try: > type(uvc) == float > except TypeError as e: > print(e, msg) > > "type(uvc)==float" resolves to a standalone True or False, not an exception

Re: [Tutor] try, except syntax

2018-08-02 Thread Oscar Benjamin
On 2 August 2018 at 15:29, Shall, Sydney wrote: > > try: > type(uvc) == float > except TypeError as e: > print(e, msg) Let's try this interactively: >>> uvc = 2 >>> type(uvc) >>> type(uvc) == float False So calling type(uvc) doesn't raise an error. It returns the type "int". You then c

[Tutor] try, except syntax

2018-08-02 Thread Shall, Sydney
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