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 currency conversion information (rates)
# ---- (return as json formatted data)
print()
print(f"Convert {amount:,.2f} {frm} to {to}")
data = requests.get(f"https://open.er-api.com/v6/latest/{frm}").json()
# ---- display conversion results
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 all of the conversion rates
print()
print('conversion rates')
for k,v in data['rates'].items():
print (f'{k:4} {v:.2f}')
print()