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
19 changes: 19 additions & 0 deletions currency_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def get_rate(rates, orig_cur, new_cur):
"""Pulls coversion rate between two currencys"""
for rate in rates:
if orig_cur in rate[0] and new_cur in rate[1]:
conv = round(rate[2],2)
return conv
elif orig_cur in rate[1] and new_cur in rate[0]:
conv = round(1 / rate[2],2)
return conv


def convert(rates, value, orig_cur, new_cur):
"""Convert value of one current into another"""
if orig_cur == new_cur:
return value
else:
rate = get_rate(rates, orig_cur, new_cur)
final = value * rate
return rate
24 changes: 24 additions & 0 deletions test_currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from currency_convert import *

rates = [("USD", "EUR", .74), ("EUR", "JPY", 145.949)]

def test_convert_match():
assert convert(rates, 1, "USD", "USD") == 1

def test_convert_doleur():
assert convert(rates, 1, "USD", "EUR") == .74

def test_convert_eurdol():
assert convert(rates, 1, "EUR", "USD") == 1.35

def test_convertEURJPY():
assert convert(rates, 1, "EUR", "JPY") == 145.95

def test_convertJPYEUR():
assert convert(rates, 1, "JPY", "EUR") == .01

def test_rate_pull():
assert get_rate(rates, "USD", "EUR") == .74
assert get_rate(rates, "EUR", "USD") == 1.35
assert get_rate(rates, "JPY", "EUR") == .01
assert get_rate(rates, "EUR", "JPY") == 145.95