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

2017-06-15 Thread Neil Cerutti
On 2017-06-14, Peter Otten <__pete...@web.de> wrote:
> Sebastian Silva wrote:
>
>> Or shorter:
>> 
>> if unit in 'Cc':
>
> Don't do that. You are in for nasty surprises: 
>
 def check(unit):
> ... if unit in "Cc":
> ... return "Celsius"
> ... return "unknown"
> ... 
 check("c")
> 'Celsius'
 check("C")
> 'Celsius'
 check("F")
> 'unknown'
>
> Fine so far. But now:
>
 check("Cc")
> 'Celsius'
 check("")
> 'Celsius'

Woah! I bet I've got that bug in several of my programs. Thanks!

-- 
Neil Cerutti

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


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

2017-06-15 Thread Bo Yu

On Thu, Jun 15, 2017 at 12:52:10PM +, Neil Cerutti wrote:

On 2017-06-14, Peter Otten <__pete...@web.de> wrote:

Sebastian Silva wrote:


Or shorter:

if unit in 'Cc':


Don't do that. You are in for nasty surprises:


def check(unit):

... if unit in "Cc":
... return "Celsius"
... return "unknown"
...

check("c")

'Celsius'

check("C")

'Celsius'

check("F")

'unknown'

Fine so far. But now:


check("Cc")

'Celsius'

check("")

'Celsius'

In fact,


check("cC")

'unknown'


Best


Woah! I bet I've got that bug in several of my programs. Thanks!

--
Neil Cerutti

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

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


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

2017-06-15 Thread David Rock

> On Jun 15, 2017, at 13:16, William Gan  wrote:
> 
> 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.

I’m glad it helped.  For completeness, in case you didn’t notice, your elif 
statement has the same issue.

elif unit == 'F' or 'f’:
c = (temp - 32) * 5 / 9
print(str(temp) + ' F is equivalent to ' + "%.2f" % c + ' C.’


—
David Rock
da...@graniteweb.com




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


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

2017-06-15 Thread William Gan
Hi Alan,

Very much thanks again for your help.

Your elucidation helped me gain better understanding on this issue. I have 
resolved that error.

Thank you also for your counsel on this second issue.

Best regards.
 

-Original Message-
From: Alan Gauld [mailto:alan.ga...@yahoo.co.uk] 
Sent: Thursday, June 15, 2017 2:15 AM
To: tutor@python.org
Subject: Re: [Tutor] Fahrenheit to Celsius Conversion another problem and 
Programming Paradigm

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

it reads as

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



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


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

2017-06-15 Thread William Gan
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  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


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

2017-06-15 Thread William Gan
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:sebast...@fuentelibre.org] 
Sent: Thursday, June 15, 2017 1:53 AM
To: William Gan ; tutor@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



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