Skip to content

Commit ca08769

Browse files
committed
added currency convertor
1 parent a131e88 commit ca08769

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 🌐 Currency Convertor (Python)
2+
3+
A simple Python CLI tool to convert currency using real time exchange rates fetched from the free Hexarate API.
4+
5+
---
6+
7+
## 🚀 Features
8+
- Real time exchange rates (no API keys required)
9+
- Convert any amount between any two currencies
10+
- Display results in a clean CLI format.
11+
12+
---
13+
## Usage
14+
15+
python3 currency_converter.py <FROM_CURRENCY> <TO_CURRENCY> <AMOUNT>
16+
Example: python3 currency_converter.py USD EUR 100
17+
18+
Output: 100.00 USD = 94.23 EUR
19+
20+
21+
## 🧰 Requirements
22+
23+
Install the dependencies using pip:
24+
```bash
25+
pip install requests
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import requests
4+
5+
def convert_currency(from_currency, to_currency, amount):
6+
API_URL = f"https://hexarate.paikama.co/api/rates/latest/{from_currency}?target={to_currency}"
7+
response = requests.get(API_URL)
8+
data = response.json()
9+
result = amount*(data["data"]["mid"])
10+
11+
if response.status_code != 200:
12+
raise Exception(f"API request failed: {response.status_code}")
13+
14+
return result
15+
16+
def main():
17+
if len(sys.argv) != 4:
18+
print("Usage: python currency_converter.py <FROM> <TO> <AMOUNT>")
19+
print("Example: python currency_converter.py USD EUR 100")
20+
sys.exit(1)
21+
22+
from_currency = sys.argv[1]
23+
to_currency = sys.argv[2]
24+
amount = float(sys.argv[3])
25+
26+
try:
27+
result = convert_currency(from_currency, to_currency, amount)
28+
print(f"{amount:.2f} {from_currency.upper()} = {result:.2f} {to_currency.upper()}")
29+
except Exception as e:
30+
print(f"Error: {e}")
31+
32+
if __name__ == "__main__":
33+
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

0 commit comments

Comments
 (0)