-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency_converter.py
54 lines (44 loc) · 1.8 KB
/
currency_converter.py
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
import sys
import requests
import colorama
from datetime import datetime
from colorama import Fore, Back
colorama.init(autoreset=True)
def convert_currency():
print()
print(Fore.YELLOW + " <<< CURRENCY CONVERTER >>> ")
print()
if __name__ == "__main__":
while(1):
user_currency = input(Fore.CYAN + " Enter your currency: ")
target_currency = input(Fore.CYAN + " Enter target currency: ")
while True:
try:
amount = float(input(Fore.YELLOW + " Enter amount: "))
except:
print(Fore.BLACK + Back.RED + " The amount must be a numeric value! ")
print()
continue
if not amount > 0:
print(Fore.BLACK + Back.RED + " The amount must be greater than 0 ")
print()
continue
else:
break
url = ("https://api.apilayer.com/fixer/convert?to="
+ target_currency + '&from=' + user_currency +
'&amount=' + str(amount))
payload = {}
headers = {"apikey": "YOUR API KEY"}
response = requests.request("GET", url, headers=headers, data=payload)
status_code = response.status_code
trgt_crrncy = target_currency.upper()
if status_code != 200:
print(Fore.BLACK + Back.RED + " There was an error. Please try again later ")
print()
quit()
result = response.json()
print()
print(Fore.BLACK + Back.GREEN + " Conversion result: " + trgt_crrncy + " " + str(result["result"]) + " ")
print()
convert_currency()