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

Sebastian Silva sebastian at fuentelibre.org
Wed Jun 14 13:52:42 EDT 2017


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