[Tutor] (no subject)

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Jun 20 00:48:14 CEST 2012

On 19/06/2012 21:07, Dave Angel wrote:
> On 06/19/2012 03:35 PM, Selby Rowley-Cannon wrote:
>> Mailing list;
>>          I have a small, [for the most part] functioning translation app for Rydish, a language created for the sole purpose of an RPG. The only problem is when I enter a word that has not yet been translated, I get this error:
>> Traceback (most recent call last):
>>    File "translator.py", line 25, in<module>
>>      Etranslate()
>>    File "translator.py", line 14, in Etranslate
>>      print(Edictionary[Eword])
>> KeyError: 'world'
>> Is there a way to print a user-freindly message instead of displaying a traceback?
>> Thanks,
>>          -Selby
> Even easier than catching an exception is to test for the condition you
> expect might not pass.  Assuming your Edictionary is a dict, all you
> need to do is to make the reference conditional on the presence of that
> particular key.
> if Eword in Edictionary:
>      print(Edictionary[Eword])
> else:
>      print("oops....

But note the comparison between LBYL and EAFP - I'll leave those 
interested to google for it :)


Mark Lawrence.

More information about the Tutor mailing list