line shift? (baby steps 2)

Artemisio calidusdk at hotmail.com
Mon Aug 23 21:13:52 CEST 2004


Reinhold Birkenfeld <reinhold-birkenfeld-nospam at wolke7.net> wrote in message news:<2oueokFe36qgU2 at uni-berlin.de>...
> Artemisio wrote:
> > I have done a small currency calculator. It works and I'm very glad.
> > But...I'd like to have a line shift if user types a wrong choice.
> > Please, look at the code and output example down here:
> > 
> > # -*- coding: ISO-8859-1 -*-
> > import string
> > 
> > eudk_currency= 7.47
> > dkeu_currency= 0.13
> > usdk_currency= 5.93
> > dkus_currency= 0.16
> > euus_currency= 1.19
> > useu_currency= 0.78
> 
> As a hint not pertaining to your actual problem: Use a dictionary to
> store the values, like this:
> 
> currencies = { "EUDK" : 7.47, "DKEU" : 0.13, (...) }
> 
> Then you do not have to do if statements like this
> 
> if currency_str == "DKEU":
>     result= amount_int* dkeu_currency
>     print amount_int, "Danish Crowns correspond to", result, "Euro."
> 
> (...)
> 
> You can directly use the input string as index into the dictionary:
> 
> result = amount_int * currencies[currency_str]
> print (...)
> 
> Reinhold

Great! Thank you pals! I'll have a closer look to your suggestion
Reinhold, it seems a slick way to do the same thing



More information about the Python-list mailing list