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

Bo Yu tsu.yubo at gmail.com
Thu Jun 15 17:54:21 EDT 2017


On Thu, Jun 15, 2017 at 12:52:10PM +0000, Neil Cerutti wrote:
>On 2017-06-14, Peter Otten <__peter__ at 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 at python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list