[Tutor] Beginner problem: name 'convertToFahrenheit' is not defined
Kent Johnson
kent37 at tds.net
Sat Aug 16 13:31:27 CEST 2008
On Sat, Aug 16, 2008 at 1:22 AM, Joseph Bae <joeturf at gmail.com> wrote:
> convertTo == "C" and convertToCelsius(temp) or
> convertToFahrenheit(temp)
This idiom has a pitfall. Consider
A and B or C
If B can evaluate to a false value, such as None or 0, the expression
will not do what you intend. This idiom is only safe when you are sure
that B will never evaluate to false.
In Python 2.5 there is a better way to write it:
B if A else C
This is a safer way to write your program.
Kent
More information about the Tutor
mailing list