Hi David,

Very much thanks for taking time to help.

Your explanation has helped me understand that syntax issue better. I have 
resolved that error.

Your counsel on the second issue has given me encouragement. Thank you.

Best regards.


-----Original Message-----
From: David Rock [mailto:da...@graniteweb.com] 
Sent: Thursday, June 15, 2017 2:04 AM
To: tutor@python.org
Subject: Re: [Tutor] Fahrenheit to Celsius Conversion another problem and 
Programming Paradigm


> On Jun 14, 2017, at 09:20, William Gan <ganwill...@outlook.com> wrote:
> 
> However, today I modified only the print instruction a little to try to print 
> out ℃ (in the second print clause). When I subsequently ran the script all 
> the outputs were executed from the if clause, even when I input other letters 
> (Please see below. I have removed the code to print degree C).
> 
> 
> if unit == 'C' or 'c':
> 
>    f = temp * 9 / 5 + 32
> 
>    print(str(temp) + ' C is equivalent to ' + '%.2f' % f + ' F.')
> 
> elif unit == 'F' or 'f':
> 
>    c = (temp - 32) * 5 / 9
> 
>    print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.')


The problem is your if statement is flawed.

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

This is not doing what you think it is.  You are expecting:
if unit == ‘C’ or unit == ‘c’

When doing an ‘or’ statement, each part of the logic on each side of the ‘or’ 
is evaluated, so if unit == ‘C’ is true, or if ‘c’ is true

is what’s actually being checked.  a bare character, ‘c’ will always evaluate 
to True, so the if is always true.

What you actually want (checking if unit is ‘C’ or ‘c’) can be done a few ways. 
 The most common are:

if unit == ‘C’ or unit ==‘c’:

or

if unit in [‘C’, ‘c’]:



> ISSUE 2:
> 
> The second issue relates to the last statement above “I have looked at it 
> many times today and could not see the error”.
> 
> 
> 
> I was hoping that someone, perhaps one with pedagogical experience and 
> knowledge, could advise the following:
> 
> 1.       Is it possible that I may not have the right aptitude or mental 
> paradigm to do computer programming?

Unlikely.  Programming is not about aptitude, it’s more about spending time 
understanding the rules.  The above is a prime example of getting to understand 
the rules.  Logic rules are notoriously specific; they check exactly what you 
tell them to check, which is not always what you think you are asking.  It just 
takes time.

> 
> 2.       Nevertheless, I intend to keep learning and practicing, but wonder 
> if there is an effective way to get a breakthrough into the programming 
> paradigm? If so, kindly advise how and direct me to a suitable resource to do 
> it.

Really, just keep trying things.  When you run into something like this, the 
most effective way to troubleshoot is narrow it down to the essential issue.  
In this case, “why is the if statement always evaluating to true?”  Look at the 
parts and re-read what each does (eg, reread how the ‘or’ operator works).

You are doing fine. :-)



—
David Rock
da...@graniteweb.com




_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to