This project uses hard-coded example exchange rates and does not provide live financial information.
Source code
"""Educational currency converter using fixed example rates."""
import math
RATES = {
"USD": {"EUR": 0.92, "GBP": 0.81, "USD": 1},
"EUR": {"USD": 1.09, "GBP": 0.83, "EUR": 1},
"GBP": {"USD": 1.24, "EUR": 1.20, "GBP": 1},
}
def convert_currency(amount, from_currency, to_currency, rates):
if from_currency not in rates or to_currency not in rates[from_currency]:
raise ValueError("Unsupported currency or conversion.")
if not math.isfinite(amount) or amount < 0:
raise ValueError("Amount must be a finite number of 0 or more.")
result = amount * rates[from_currency][to_currency]
if not math.isfinite(result):
raise ValueError("Amount is too large to convert.")
return result
def read_currency(prompt):
while True:
currency = input(prompt).strip().upper()
if currency in RATES:
return currency
print("Invalid currency. Please enter USD, EUR or GBP.")
def main():
print("Welcome to Harlow College Currency Converter")
print("Educational example rates; these are not live market rates.")
from_currency = read_currency("Convert from (USD, EUR, GBP): ")
to_currency = read_currency("Convert to (USD, EUR, GBP): ")
while True:
try:
amount = float(input(f"Enter the amount in {from_currency}: "))
converted = convert_currency(amount, from_currency, to_currency, RATES)
except ValueError:
print("Invalid amount. Enter a finite number of 0 or more, small enough to convert.")
continue
break
print(f"{amount} {from_currency} is equal to {converted:.2f} {to_currency}")
if __name__ == "__main__":
main()