Project #1
Create a program that converts from one currency to a
different currency. (It is an error to try to convert
a currency to itself.)
Add to the existing code below:
- loop until the user wants to exit
- verify/validate "from" code
- verify/validate "to" code
- verify/validate "amount" (greater that zero)
Nice but not necessary.
- report type of error
- display the name of the currency being converted to
Project #2
Turn Project #1 into a GUI with valid (to/from) codes pulldown menus.
Also check for a valid amount. Report errors.
Code
# ====================================================================
# Currency Conversion
# ====================================================================
# Project-from/base-on:
# www.youtube.com/watch?v=s7HU6gd_-uc
# 3 Python Automation Projects So Useful They Feel Illegal
# --------------------------------------------------------------------
# API Documentation:
# www.exchangerate-api.com/
# www.exchangerate-api.com/docs/overview
# www.exchangerate-api.com/docs/supported-currencies
# --------------------------------------------------------------------
# Exchange Rate API Attribution:
# www.exchangerate-api.com/docs/free
# ====================================================================
import sys
import datetime
import requests
# ---- ask the user for input
##print()
##amount = float(input("amount: "))
##frm = input("From: ").strip().upper() # dollars
##to = input("To: ").strip().upper() # euros
# ---- test data, remove when no longer testing
amount = 1000001.00
frm = 'USD'
to = 'EUR'
# ---- display current date/time
print()
print(datetime.datetime.now())
# ---- request the currency conversion rates
# ---- (return json formatted data)
data = requests.get(f"https://open.er-api.com/v6/latest/{frm}").json()
# ---- display the conversion results
print()
print(f"Convert {amount:,.2f} from {frm} to {to}")
print()
if data["result"] == "success" and to in data["rates"]:
print(f"{amount*data['rates'][to]:,.2f} {to}")
else:
print("Currency conversion error")
sys.exit()
# ---- display the returned data
print()
for k in data.keys():
if k != 'rates':
print(f'{k:21}: {data[k]}')
counter = 0
print()
print(f'{len(data["rates"])} conversion rates')
for k,v in data['rates'].items():
print (f'{k:4} {v:.2f}')
if counter > 6: break
counter += 1
print()