Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 550e708

Browse files
authored
Merge pull request #410 from Salman666142/patch-3
Enhanced version of Currency converter
2 parents 4b2ab21 + 806a24e commit 550e708

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
"""
2+
THis program is capable of converting from one currency to another as of today itself.
3+
It uses the api at fixer.io and then calculates the value of the currency in terms of the other as of today.
4+
"""
5+
6+
# https://github.com/chavarera/python-mini-projects/issues
7+
# https://medium.com/@cereblanco/setup-black-and-isort-in-vscode-514804590bf9
8+
# Source: https://fixer.io/quickstart
9+
# Imp read: https://stackoverflow.com/questions/3139879/how-do-i-get-currency-exchange-rates-via-an-api-such-as-google-finance
10+
11+
12+
import requests
13+
import json
14+
import sys
15+
from pprint import pprint
16+
17+
# The below 4 lines bring out the value of currency from the api at fixer.io. I had to register there, the key is unique to me.
18+
url = "http://data.fixer.io/api/latest?access_key=33ec7c73f8a4eb6b9b5b5f95118b2275"
19+
data = requests.get(url).text
20+
data2 = json.loads(data) # brings whether request was successful,timestamp etc
21+
fx = data2["rates"]
22+
currencies = [
23+
"AED : Emirati Dirham,United Arab Emirates Dirham",
24+
"AFN : Afghan Afghani,Afghanistan Afghani",
25+
"ALL : Albanian Lek,Albania Lek",
26+
"AMD : Armenian Dram,Armenia Dram",
27+
"ANG : Dutch Guilder,Netherlands Antilles Guilder,Bonaire,Curaçao,Saba,Sint Eustatius,Sint Maarten",
28+
"AOA : Angolan Kwanza,Angola Kwanza",
29+
"ARS : Argentine Peso,Argentina Peso,Islas Malvinas",
30+
"AUD : Australian Dollar,Australia Dollar,Christmas Island,Cocos (Keeling) Islands,Norfolk Island,Ashmore and Cartier Islands,Australian Antarctic Territory,Coral Sea Islands,Heard Island,McDonald Islands,Kiribati,Nauru",
31+
"AWG : Aruban or Dutch Guilder,Aruba Guilder",
32+
"AZN : Azerbaijan Manat,Azerbaijan Manat",
33+
"BAM : Bosnian Convertible Mark,Bosnia and Herzegovina Convertible Mark",
34+
"BBD : Barbadian or Bajan Dollar,Barbados Dollar",
35+
"BDT : Bangladeshi Taka,Bangladesh Taka",
36+
"BGN : Bulgarian Lev,Bulgaria Lev",
37+
"BHD : Bahraini Dinar,Bahrain Dinar",
38+
"BIF : Burundian Franc,Burundi Franc",
39+
"BMD : Bermudian Dollar,Bermuda Dollar",
40+
"BND : Bruneian Dollar,Brunei Darussalam Dollar",
41+
"BOB : Bolivian Bolíviano,Bolivia Bolíviano",
42+
"BRL : Brazilian Real,Brazil Real",
43+
"BSD : Bahamian Dollar,Bahamas Dollar",
44+
"BTC : Bitcoin,BTC, XBT",
45+
"BTN : Bhutanese Ngultrum,Bhutan Ngultrum",
46+
"BWP : Botswana Pula,Botswana Pula",
47+
"BYN : Belarusian Ruble,Belarus Ruble",
48+
"BYR : Belarusian Ruble,Belarus Ruble",
49+
"BZD : Belizean Dollar,Belize Dollar",
50+
"CAD : Canadian Dollar,Canada Dollar",
51+
"CDF : Congolese Franc,Congo/Kinshasa Franc",
52+
"CHF : Swiss Franc,Switzerland Franc,Liechtenstein,Campione d'Italia,Büsingen am Hochrhein",
53+
"CLF : Chilean Unit of Account",
54+
"CLP : Chilean Peso,Chile Peso",
55+
"CNY : Chinese Yuan Renminbi,China Yuan Renminbi",
56+
"COP : Colombian Peso,Colombia Peso",
57+
"CRC : Costa Rican Colon,Costa Rica Colon",
58+
"CUC : Cuban Convertible Peso,Cuba Convertible Peso",
59+
"CUP : Cuban Peso,Cuba Peso",
60+
"CVE : Cape Verdean Escudo,Cape Verde Escudo",
61+
"CZK : Czech Koruna,Czech Republic Koruna",
62+
"DJF : Djiboutian Franc,Djibouti Franc",
63+
"DKK : Danish Krone,Denmark Krone,Faroe Islands,Greenland",
64+
"DOP : Dominican Peso,Dominican Republic Peso",
65+
"DZD : Algerian Dinar,Algeria Dinar",
66+
"EGP : Egyptian Pound,Egypt Pound,Gaza Strip",
67+
"ERN : Eritrean Nakfa,Eritrea Nakfa",
68+
"ETB : Ethiopian Birr,Ethiopia Birr,Eritrea",
69+
"EUR : Euro,Euro Member Countries,Andorra,Austria,Azores,Baleares (Balearic Islands),Belgium,Canary Islands,Cyprus,Finland,France,French Guiana,French Southern Territories,Germany,Greece,Guadeloupe,Holland (Netherlands),Holy See (Vatican City),Ireland (Eire),Italy,Luxembourg,Madeira Islands,Malta,Monaco,Montenegro,Netherlands",
70+
"FJD : Fijian Dollar,Fiji Dollar",
71+
"FKP : Falkland Island Pound,Falkland Islands (Malvinas) Pound",
72+
"GBP : British Pound,United Kingdom Pound,United Kingdom (UK),England,Northern Ireland,Scotland,Wales,Falkland Islands,Gibraltar,Guernsey,Isle of Man,Jersey,Saint Helena and Ascension,South Georgia and the South Sandwich Islands,Tristan da Cunha",
73+
"GEL : Georgian Lari,Georgia Lari",
74+
"GGP : Guernsey Pound,Guernsey Pound",
75+
"GHS : Ghanaian Cedi,Ghana Cedi",
76+
"GIP : Gibraltar Pound,Gibraltar Pound",
77+
"GMD : Gambian Dalasi,Gambia Dalasi",
78+
"GNF : Guinean Franc,Guinea Franc",
79+
"GTQ : Guatemalan Quetzal,Guatemala Quetzal",
80+
"GYD : Guyanese Dollar,Guyana Dollar",
81+
"HKD : Hong Kong Dollar,Hong Kong Dollar",
82+
"HNL : Honduran Lempira,Honduras Lempira",
83+
"HRK : Croatian Kuna,Croatia Kuna",
84+
"HTG : Haitian Gourde,Haiti Gourde",
85+
"HUF : Hungarian Forint,Hungary Forint",
86+
"IDR : Indonesian Rupiah,Indonesia Rupiah,East Timor",
87+
"ILS : Israeli Shekel,Israel Shekel,Palestinian Territories",
88+
"IMP : Isle of Man Pound,Isle of Man Pound",
89+
"INR : Indian Rupee,India Rupee,Bhutan,Nepal",
90+
"IQD : Iraqi Dinar,Iraq Dinar",
91+
"IRR : Iranian Rial,Iran Rial",
92+
"ISK : Icelandic Krona,Iceland Krona",
93+
"JEP : Jersey Pound,Jersey Pound",
94+
"JMD : Jamaican Dollar,Jamaica Dollar",
95+
"JOD : Jordanian Dinar,Jordan Dinar",
96+
"JPY : Japanese Yen,Japan Yen",
97+
"KES : Kenyan Shilling,Kenya Shilling",
98+
"KGS : Kyrgyzstani Som,Kyrgyzstan Som",
99+
"KHR : Cambodian Riel,Cambodia Riel",
100+
"KMF : Comorian Franc,Comorian Franc",
101+
"KPW : North Korean Won,Korea (North) Won",
102+
"KRW : South Korean Won,Korea (South) Won",
103+
"KWD : Kuwaiti Dinar,Kuwait Dinar",
104+
"KYD : Caymanian Dollar,Cayman Islands Dollar",
105+
"KZT : Kazakhstani Tenge,Kazakhstan Tenge",
106+
"LAK : Lao Kip,Laos Kip",
107+
"LBP : Lebanese Pound,Lebanon Pound",
108+
"LKR : Sri Lankan Rupee,Sri Lanka Rupee",
109+
"LRD : Liberian Dollar,Liberia Dollar",
110+
"LSL : Basotho Loti,Lesotho Loti",
111+
"LTL : Lithuanian litas",
112+
"LVL : Latvia Lats",
113+
"LYD : Libyan Dinar,Libya Dinar",
114+
"MAD : Moroccan Dirham,Morocco Dirham,Western Sahara",
115+
"MDL : Moldovan Leu,Moldova Leu",
116+
"MGA : Malagasy Ariary,Madagascar Ariary",
117+
"MKD : Macedonian Denar,Macedonia Denar",
118+
"MMK : Burmese Kyat,Myanmar (Burma) Kyat",
119+
"MNT : Mongolian Tughrik,Mongolia Tughrik",
120+
"MOP : Macau Pataca,Macau Pataca",
121+
"MRU : Mauritanian Ouguiya,Mauritania Ouguiya",
122+
"MUR : Mauritian Rupee,Mauritius Rupee",
123+
"MVR : Maldivian Rufiyaa,Maldives (Maldive Islands) Rufiyaa",
124+
"MWK : Malawian Kwacha,Malawi Kwacha",
125+
"MXN : Mexican Peso,Mexico Peso",
126+
"MYR : Malaysian Ringgit,Malaysia Ringgit",
127+
"MZN : Mozambican Metical,Mozambique Metical",
128+
"NAD : Namibian Dollar,Namibia Dollar",
129+
"NGN : Nigerian Naira,Nigeria Naira",
130+
"NIO : Nicaraguan Cordoba,Nicaragua Cordoba",
131+
"NOK : Norwegian Krone,Norway Krone,Bouvet Island,Svalbard,Jan Mayen,Queen Maud Land,Peter I Island",
132+
"NPR : Nepalese Rupee,Nepal Rupee,India (unofficially near India-Nepal border)",
133+
"NZD : New Zealand Dollar,New Zealand Dollar,Cook Islands,Niue,Pitcairn Islands,Tokelau",
134+
"OMR : Omani Rial,Oman Rial",
135+
"PAB : Panamanian Balboa,Panama Balboa",
136+
"PEN : Peruvian Sol,Peru Sol",
137+
"PGK : Papua New Guinean Kina,Papua New Guinea Kina",
138+
"PHP : Philippine Peso,Philippines Peso",
139+
"PKR : Pakistani Rupee,Pakistan Rupee",
140+
"PLN : Polish Zloty,Poland Zloty",
141+
"PYG : Paraguayan Guarani,Paraguay Guarani",
142+
"QAR : Qatari Riyal,Qatar Riyal",
143+
"RON : Romanian Leu,Romania Leu",
144+
"RSD : Serbian Dinar,Serbia Dinar",
145+
"RUB : Russian Ruble,Russia Ruble,Tajikistan,Abkhazia,South Ossetia",
146+
"RWF : Rwandan Franc,Rwanda Franc",
147+
"SAR : Saudi Arabian Riyal,Saudi Arabia Riyal",
148+
"SBD : Solomon Islander Dollar,Solomon Islands Dollar",
149+
"SCR : Seychellois Rupee,Seychelles Rupee",
150+
"SDG : Sudanese Pound,Sudan Pound",
151+
"SEK : Swedish Krona,Sweden Krona",
152+
"SGD : Singapore Dollar,Singapore Dollar",
153+
"SHP : Saint Helenian Pound,Saint Helena Pound",
154+
"SLL : Sierra Leonean Leone,Sierra Leone Leone",
155+
"SOS : Somali Shilling,Somalia Shilling",
156+
"SRD : Surinamese Dollar,Suriname Dollar",
157+
"STN : Sao Tomean Dobra,São Tomé and Príncipe Dobra",
158+
"SVC : Salvadoran Colon,El Salvador Colon",
159+
"SYP : Syrian Pound,Syria Pound",
160+
"SZL : Swazi Lilangeni,eSwatini Lilangeni",
161+
"THB : Thai Baht,Thailand Baht",
162+
"TJS : Tajikistani Somoni,Tajikistan Somoni",
163+
"TMT : Turkmenistani Manat,Turkmenistan Manat",
164+
"TND : Tunisian Dinar,Tunisia Dinar",
165+
"TOP : Tongan Pa'anga,Tonga Pa'anga",
166+
"TRY : Turkish Lira,Turkey Lira,North Cyprus",
167+
"TTD : Trinidadian Dollar,Trinidad and Tobago Dollar,Trinidad,Tobago",
168+
"TWD : Taiwan New Dollar,Taiwan New Dollar",
169+
"TZS : Tanzanian Shilling,Tanzania Shilling",
170+
"UAH : Ukrainian Hryvnia,Ukraine Hryvnia",
171+
"UGX : Ugandan Shilling,Uganda Shilling",
172+
"USD : US Dollar,United States Dollar,America,American Samoa,American Virgin Islands,British Indian Ocean "
173+
"Territory,British Virgin Islands,Ecuador,El Salvador,Guam,Haiti,Micronesia,Northern Mariana Islands,Palau,"
174+
"Panama,Puerto Rico,Turks and Caicos Islands,United States Minor Outlying Islands,Wake Island,East Timor",
175+
"UYU : Uruguayan Peso,Uruguay Peso",
176+
"UZS : Uzbekistani Som,Uzbekistan Som",
177+
"VEF : Venezuelan Bolívar,Venezuela Bolívar",
178+
"VND : Vietnamese Dong,Viet Nam Dong",
179+
"VUV : Ni-Vanuatu Vatu,Vanuatu Vatu",
180+
"WST : Samoan Tala,Samoa Tala",
181+
"XAF : Central African CFA Franc BEAC,Communauté Financière Africaine (BEAC) CFA Franc BEAC,Cameroon,"
182+
"Central African Republic,Chad,Congo/Brazzaville,Equatorial Guinea,Gabon",
183+
"XAG : Silver Ounce,Silver",
184+
"XAU : Gold Ounce,Gold",
185+
"XCD : East Caribbean Dollar,East Caribbean Dollar,Anguilla,Antigua and Barbuda,Dominica,Grenada,The Grenadines "
186+
"and Saint Vincent,Montserrat",
187+
"XDR : IMF Special Drawing Rights,International Monetary Fund (IMF) Special Drawing Rights",
188+
"XOF : CFA Franc,Communauté Financière Africaine (BCEAO) Franc,Benin,Burkina Faso,Ivory Coast,"
189+
"Guinea-Bissau,Mali,Niger,Senegal,Togo",
190+
"XPF : CFP Franc,Comptoirs Français du Pacifique (CFP) Franc,French Polynesia,New Caledonia,Wallis and "
191+
"Futuna Islands",
192+
"YER : Yemeni Rial,Yemen Rial",
193+
"ZAR : South African Rand,South Africa Rand,Lesotho,Namibia",
194+
"ZMK : Zambian Kwacha,Zambia Kwacha",
195+
"ZMW : Zambian Kwacha,Zambia Kwacha",
196+
"ZWL : Zimbabwean Dollar,Zimbabwe Dollar",
197+
]
198+
199+
200+
# enhancement 3
201+
def displays_storage():
202+
file2 = open("conversion storage.txt", "r+")
203+
print(file2.read())
204+
file2.close()
205+
206+
207+
# The below function calculates the actual conversion
208+
def function1():
209+
query = input(
210+
"Please specify the amount of currency to convert, from currency, to currency (with space in between).\nPress "
211+
"SHOW to see list of currencies available. \n press DISPLAY to see previous transactions"
212+
"\nPress Q to quit. \n ")
213+
if query == "Q":
214+
sys.exit()
215+
elif query == "SHOW":
216+
pprint(currencies)
217+
function1()
218+
elif query == "DISPLAY":
219+
displays_storage()
220+
function1()
221+
222+
else:
223+
qty, fromC, toC = query.split(" ")
224+
fromC = fromC.upper()
225+
toC = toC.upper()
226+
qty = float(round(int(qty), 2))
227+
amount = round(qty * fx[toC] / fx[fromC], 2)
228+
# opening the file in append mode
229+
file1 = open("conversion storage.txt", "a")
230+
# storing the cash conversion data
231+
file1.write(f"{qty} of currency {fromC} amounts to {amount} of currency {toC} today\n")
232+
# closes file after use
233+
file1.close()
234+
print(f"{qty} of currency {fromC} amounts to {amount} of currency {toC} today")
235+
236+
237+
try:
238+
function1()
239+
except KeyError:
240+
print("You seem to have inputted wrongly, retry!")
241+
function1()

0 commit comments

Comments
 (0)