forked from GotoCode/mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexchange.py
More file actions
90 lines (44 loc) · 1.4 KB
/
Copy pathexchange.py
File metadata and controls
90 lines (44 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#
# exchange.py - an application to retrieve currency rates
#
# Exchange JSON API Credit - fixer.io
#
# external modules
import requests
import json
# variables
source_curr = None
target_curr = None
value = None
response = None
curr_dict = None
unofficial_currencies = {}
result = None
supported_currencies = json.loads(requests.get("http://api.fixer.io/latest?base=EUR").text)["rates"].keys()
supported_currencies.sort()
# helper functions
def src_to_tgt_currency(source, target, value):
# hard-coded exchange rates for
# currencies not found in fixer.io
if source == "USD" and target == "QAR":
return value * 3.65
elif source == "QAR" and target == "USD":
return value * 0.27
response = requests.get("http://api.fixer.io/latest?base=" + source)
rates_dict = json.loads(response.text)
return value * rates_dict["rates"][target]
# sample user interface
def main():
print
print "Supported currencies are..."
print
for curr in supported_currencies:
print curr
print
source_curr = str(raw_input("Source currency: "))
target_curr = str(raw_input("Target currency: "))
print
value = float(raw_input("Value in " + source_curr + ": "))
result = src_to_tgt_currency(source_curr, target_curr, value)
print
print str(value) + " " + source_curr + " --> " + str(result) + " " + target_curr + "\n"