line shift? (baby steps 2)

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Mon Aug 23 10:56:56 EDT 2004


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

-- 
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich.  Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
  -- David Kastrup in de.comp.os.unix.linux.misc



More information about the Python-list mailing list