Python currency converter
This commit is contained in:
29
scripts/executable_currency
Normal file
29
scripts/executable_currency
Normal file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import requests
|
||||
|
||||
def convert_currency(amount, from_currency, to_currency):
|
||||
url = f"https://api.frankfurter.app/latest?amount={amount}&from={from_currency}&to={to_currency}"
|
||||
response = requests.get(url)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
converted_amount = data['rates'][to_currency]
|
||||
print(f"{amount} {from_currency} = {converted_amount} {to_currency}")
|
||||
else:
|
||||
print("Error: Unable to fetch exchange rates.")
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 4:
|
||||
print("Usage: currency_converter.py <amount> <from_currency> <to_currency>")
|
||||
print("Example: currency_converter.py 100 CAD USD")
|
||||
sys.exit(1)
|
||||
|
||||
amount = float(sys.argv[3])
|
||||
from_currency = sys.argv[1].upper()
|
||||
to_currency = sys.argv[2].upper()
|
||||
|
||||
convert_currency(amount, from_currency, to_currency)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user