-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency_convert.py
More file actions
48 lines (38 loc) · 1.37 KB
/
currency_convert.py
File metadata and controls
48 lines (38 loc) · 1.37 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
import os
import sys
import requests
import datetime
def convert_currency(amount, from_currency, to_currency):
url = f'https://api.exchangerate-api.com/v4/latest/{from_currency}'
response = requests.get(url)
rates = response.json()['rates']
converted_amount = amount * rates[to_currency]
return converted_amount
def user_input():
amount = float(input("Enter amount: "))
from_currency = input("Currency to convert FROM (e.g. USD): ").upper()
to_currency = input("Currency to convert TO (e.g. php): ").upper()
today = datetime.date.today()
result = convert_currency(amount, from_currency, to_currency)
print('\n')
print(f"As of {today}:")
print(f"{from_currency} {amount:.2f} is equivalent to {to_currency} {result:.2f}")
def main():
os.system('cls' if os.name == 'nt' else 'clear')
app_name = 'Currency Converter'
print(f'{"-" * 48}')
print(f'{" " * 12}{app_name}{" " * 12}')
print(f'{"-" * 48}')
user_input()
while True:
response = input('\nDo you want to continue? (Y/N) ')
if response == 'y' or response == 'Y':
main()
elif response == 'n' or response == 'N':
print('\nThank you and have a great day.\n')
sys.exit()
else:
print('\nError: Please select y or n.\n')
continue
if __name__ == '__main__':
main()