'Currency Converter in Python
I am learning python and I have a task to write a program that converts currencies. It needs to support those currencies: BGN, USD, EUR, GBP with those rates:
Rate USD EUR GBP 1 BGN 1.79549 1.95583 2.53405
There is automatic test engine that will input value to convert + entry currency + target currency. The output should be a number converted by the rates.
I thought I could use a dictionary and do something like the code below. Would you help find why it is not working and find the elegant solution?
value = float(input())
in_curr = input()
out_curr = input()
dict = {'BGN': 1, 'USD': 1.79549, 'EUR': 1.95583, 'GBP': 2.53405}
def currency_converter (value,in_curr,out_curr):
return((dict[in_curr] / dict[out_curr]) * value)
Solution 1:[1]
Maybe because you didn't use print function
You can view the result directly by using IPython (i.e. Jupyter Notebook)
Your function looks fine and I would suggest you to eliminate the function like
print((dict[in_curr] / dict[out_curr]) * value) #python 3
or change the function into:
def currency_converter (value = value,in_curr = in_curr,out_curr = out_curr):
Then use print(currency_converter())
to display the final result
Solution 2:[2]
This app converts currencies from USD,EUR etc to Naira using dictionary:
currency = {"USD": 580, "GBP":700, "CAD": 400, "EUR": 650, "CHF": 450, "YEN":200}
print('''Welcome to your currency converter App
For USD conversion to Naira type USD,
For GBP conversion to Naira type GBP,
For CAD conversion to Naira type CAD,
For EUR conversion to Naira type EUR,
For CHF conversion to Naira type CHF,
For YEN conversion to Naira type YEN,*''')
signs = input('Type the currency you want to convert to: ').upper()
amount = float(input('Enter the amount you want to convert: '))
if signs =="USD" or signs =="GBP" or signs =="CAD" or signs =="EUR" or signs =="CHF" or signs =="YEN":
conversion = amount * currency[signs]
print("The conversion of {}{} to naria is NGN {}".format(signs,amount, conversion))
else:
print("You have entered a wrong value for the conversion type, {} is not among the list for conversion".format(signs))
Solution 3:[3]
This is another solution using list instead of dictionary
currency = [580,700,400,650,450,200]
symbols = ["USD", "GBP", "CAD", "EUR","CHF","YEN"]
print('''Welcome to your currency converter App
For USD conversion to Naira type 0,
For GBP conversion to Naira type 1,
For CAD conversion to Naira type 2,
For EUR conversion to Naira type 3,
For CHF conversion to Naira type 4,
For YEN conversion to Naira type 5,*''')
signs = int(input('Type the currency you want to convert to: '))
amount = float(input('Enter the amount you want to convert: '))
if signs ==0 or signs ==1 or signs ==2 or signs ==3 or signs ==4 or signs ==5:
conversion = amount * currency[signs]
currency_symbols = symbols[signs]
print("The conversion of {}{} to naria is NGN {}".format(currency_symbols,amount, conversion))
else:
print("You have entered a wrong value for the conversion")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | ehiaguina |
Solution 3 | ehiaguina |