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


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 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 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 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 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-14 Thread Alex Kleider

On 2017-06-14 12:22, Mats Wichmann wrote:


Of course if you do any serious argument handling, it's better to use
something like optparse (and earlier argparse) module so you're not
reinventing a wheel which has been massively worked on already.



At the suggestion of a posting on this list some years ago, I've been 
using docopt rather than optparse or argparse and have found it provides 
much more with much less work.



pip install docopt

___
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-14 Thread Mats Wichmann
On 06/14/2017 12:18 PM, Sibylle Koczian wrote:

> Correct usage would be:
> 
> if myvar == val1 or myval == val2:
> or
> if myvar in (val1, val2):


Just piling on here to say I find the second form very useful to collect
arguments in a "friendly" way, if you don't have a reason to very
rigidly constrain them. For example, if you have an on/off type switch
in your arguments (or "input()" type calls), you can say something like

if myarg in ('T', 't', 'True', 'true', 'Y', 'y', 'Yes', 'yes', '1',
'ON', 'On', 'on'):

Since that's getting too long, we can smash the casing:

if myarg.lower() in ('t', 'true', 'y', 'yes', '1', 'on'):


Of course if you do any serious argument handling, it's better to use
something like optparse (and earlier argparse) module so you're not
reinventing a wheel which has been massively worked on already.

___
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-14 Thread Peter Otten
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'


___
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-14 Thread Sibylle Koczian

Am 14.06.2017 um 16:20 schrieb William Gan:

Good day Everyone,

I am seeking help on two issues.

ISSUE 1:
Yesterday I posted a problem on this tiny script I wrote for temperature 
conversion (as practice for a newbie). My error was pointed out to me that 
there is a difference in upper and lowercase letters. After correcting that 
error, I remember the tests I ran produced the correct outputs.

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).

print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.')
unit = input('Enter C or F: ')
temp = int(input('Enter temperature: '))

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.')
else:
print('Please enter C or F in upper- or lowercase.')

The if statement block is to convert Celsius to Fahrenheit.
When I entered ‘C’ or ‘c’ and 100 for temperature, the output is correct: 100 C 
is equivalent to 212.00 F.

The elif statement block is to convert Fahrenheit to Celsius.
When I entered ‘f’ or another letter, in this case ‘z’ and ‘g’, and 212 for 
temperature, I got: 212 C is equivalent to 413.60 F.

I have looked at it many times today and could not see the error. Please advise.

Reading other threads in this list might have helped more - this is  
quite a frequent beginner error.


if myvar == val1 or val2:
...

is parsed as

if (myvar == val1) or val2:

That is true if myvar == val1; it is also true if val2 has any value  
that Python regards as true. This last condition would only be false if  
val2 were 0, None, an empty list, an empty dictionary, the empty set or  
another object with some sort of null value. There is no comparison  
between myvar and val2.


Correct usage would be:

if myvar == val1 or myval == val2:
or
if myvar in (val1, val2):

Because this sort of problem has appeared so often in this list I looked  
into the official tutorial. There is 5.7, More on Conditions, but I'm  
not sure if that's enough for a beginner.


HTH
Sibylle


___
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-14 Thread Sebastian Silva
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


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

2017-06-14 Thread Alan Gauld via Tutor
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-14 Thread David Rock

> 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


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

2017-06-14 Thread William Gan
Good day Everyone,

I am seeking help on two issues.

ISSUE 1:
Yesterday I posted a problem on this tiny script I wrote for temperature 
conversion (as practice for a newbie). My error was pointed out to me that 
there is a difference in upper and lowercase letters. After correcting that 
error, I remember the tests I ran produced the correct outputs.


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).



print('Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius.')

unit = input('Enter C or F: ')

temp = int(input('Enter temperature: '))



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.')

else:

print('Please enter C or F in upper- or lowercase.')



The if statement block is to convert Celsius to Fahrenheit.

When I entered ‘C’ or ‘c’ and 100 for temperature, the output is correct: 100 C 
is equivalent to 212.00 F.



The elif statement block is to convert Fahrenheit to Celsius.

When I entered ‘f’ or another letter, in this case ‘z’ and ‘g’, and 212 for 
temperature, I got: 212 C is equivalent to 413.60 F.



I have looked at it many times today and could not see the error. Please advise.



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?

I think I don’t. My background is in finance, accounting and economics. When I 
have difficulty in certain concepts in these fields I could figure it out 
eventually, in reasonable time.

However, I am having difficulty learning it. I have been learning for a few 
months already and I am not learning fast enough.

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.



Many thanks.



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