[Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm

William Gan ganwilliam at outlook.com
Thu Jun 15 14:00:17 EDT 2017


Hi Sebastian,

Very much thanks for your help.

Your explanation and illustrations is clear. I was not aware of that syntax.

I now understand and the issue is resolved.

Thanks again. Cheers.


-----Original Message-----
From: Sebastian Silva [mailto:sebastian at fuentelibre.org] 
Sent: Thursday, June 15, 2017 1:53 AM
To: William Gan <ganwilliam at outlook.com>; tutor at python.org
Subject: Re: [Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm

Hi William,

Glad to see the tutor list is being of help in your learning.


On 14/06/17 09:20, William Gan wrote:
> if unit == 'C' or 'c':

In this case, it will always be true, because there are two conditions,
either:

  *  unit == 'C' or
  * 'c'

As you can see, the second condition is not a comparison, but a string expression, that Python always evaluates to True (except for '' empty string).

Thus, the combined condition is always true because the second expression is always True.

The correct condition would be:

    if unit=='C' or unit=='c':

Or perhaps more clear, also correct:

    if unit in ('C', 'c'):

Or shorter:

    if unit in 'Cc':

Hope it's useful,

Regards,

Sebastian





More information about the Tutor mailing list