Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/python

rates = [("USD", "EUR", 0.86), ("EUR", "JPY", 137.05)]


def convert(rates, value, from_a, to_b):
"""Convert one currency stright to another using current exchange rate"""
if from_a == to_b:
return value
else:
new_list = [(start, end, rate)
for (start, end, rate)
in rates if from_a == start]
elput = new_list[0]
new_rate = elput[2]
new_value = new_rate * value
return round(new_value, 2)


def convert_backwards(rates, value, from_a, to_b):
"""Convert one currency to another using the reverse function"""
new_list = [(end, start, rate)
for (end, start, rate)
in rates if to_b == start]
elput = new_list[0]
new_rate = elput[2]
new_value = (value / new_rate)
return round(new_value, 2)
28 changes: 28 additions & 0 deletions test_currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/python
import currency as cy

rates = [("USD", "EUR", 0.86), ("EUR", "JPY", 137.05)]


def test_dollar_for_dollar():
assert cy.convert(rates, 1, "USD", "USD") == 1


def test_conversion():
assert cy.convert(rates, 1, "USD", "EUR") == .86


def test_converting_more():
assert cy.convert(rates, 444, "EUR", "JPY") == 60850.2


def test_convert_backwards():
assert cy.convert_backwards(rates, 323, "EUR", "USD") == 44267.15


def test_convert_backwards():
assert cy.convert_backwards(rates, 500, "JPY", "EUR") == 581.4


def test_convert_backwards():
assert cy.convert_backwards(rates, 2, "JPY", "EUR") == 2.33