Currency converter in Python I am looking for some ways to make my code simpler for this currency converter.

currencies = {
    "BRL":"(Brazilian Real)",
    "USD":"(US Dollar)",
    "EUR":"(Euro)",
    "GBP":"(Great Britain Pound)"
}

def real_to_dollar():
    user_value = raw_input("How many reais? ")
    ammount = float(user_value)
    conversion = ammount * 0.26
    print str(user_value) + " reais is equal to " + str(conversion) + " dollars."

def dollar_to_real():
    pass

def dollar_to_euro():
    pass

def euro_to_dollar():
    pass

def euro_to_pound():
    pass

def pound_to_euro():
    pass

def real_to_euro():
    pass

def euro_to_real():
    pass

def real_to_pound():
    pass

def pound_to_real():
    pass

def dollar_to_pound():
    pass

def pound_to_dollar():
    pass

print "Welcome to currency converter."
print "Supported currencies:"
for currency in currencies:
    print currency + " " + currencies[currency]

user_choice1 = raw_input("Convert: ")
user_choice2 = raw_input("To: ")

if user_choice1 == "BRL" and user_choice2 == "USD":
    real_to_dollar()

elif user_choice1 == "USD" and user_choice2 == "BRL":
    dollar_to_real()
You might not have realised it yet, but all your unimplemented methods will very much look like real_to_dollar:


ask the user how much of the original currency he wants converted;
apply a specific conversion rate;
tell the user the result of the conversion in the desired currency.


So that should be a single function, parametrized by the elements that differ between various calls: name of the origin currency, name of the converted currency, exchange rate.

def convert_currency(from, to, rate):
    user_value = raw_input("How many {}? ".format(from))
    ammount = float(user_value)
    conversion = ammount * rate
    print " {} {} is equal to {} {}.".format(user_value, from, conversion, to)


Also note the use of format to simplify building a string with numbers.

You would then call it convert_currency("reais", "dollars", 0.26) or convert_currency(" dollars", "reias", 3.85).

However, having to use a bunch of ifs to know what parameters to use (like you would have used a bunch of ifs to know which function to call) is not that great and can be a burden to maintain and expand. You could use a second dictionnary to map couples of currencies to exchange rates:

RATES = {
    ("BRL", " USD"): 0.26,
    ("USD", "BRL"): 3.85,
    # Expand as necessary
}


And just have to get the rate using RATES[(user_choice1, user_choice2)].