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

Alan Gauld alan.gauld at yahoo.co.uk
Wed Jun 14 14:15:24 EDT 2017

```On 14/06/17 15:20, William Gan wrote:

> print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.')
>
> if unit == 'C' or 'c':

You have hit a common error for beginners. reading this as a human
it is quite clear what is meant but the computer sees it
differently. It sees:

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

Now, a non empty string like 'c' is always considered True in a
boolean context so, the interpreter interprets it as:

if (unit == 'C') or True:

And since any 'or' test where one element is True is evaluated to True

if True:

and so the 'if' part is always executed.

How to avoid this? There are several options:

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

But that gets cumbersome if more than two values.
Better is:

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

This is best if there are multiple true options
not just upper/lower case
or

if unit.lower() == 'c':

This is best is the strings are longer than a
single letter. (You can use unit.upper() too,
that's just an arbitrary choice)

> 1.       Is it possible that I may not have the right aptitude
or mental paradigm to do computer programming?

Possible, but unlikely, most folks with a basic math ability
can pick up programming. You may not be a natural, and may
never be a programming guru, but you should be able to get
to the stage of competence.

> However, I am having difficulty learning it.

It is difficult, despite what some books would have you believe.
If it wasn't difficult there would be no need to teach it as
a university subject!

You have to train your mind to think like a computer (as in
the case above) and to break things down into often painfully
detailed steps. But that is just practice.

> I have been learning for a few months already and I am
> not learning fast enough.

Says who? I've been programming for over 40 years and am
still learning new things every week.

Don't give up, and keep asking questions.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

```