-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (58 loc) · 2.14 KB
/
main.py
File metadata and controls
66 lines (58 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import requests
from tabulate import tabulate
# Dictionary of available currencies
AVAILABLE_CURRENCIES = {
"USD": "US Dollar",
"EUR": "Euro",
"GBP": "Pound Sterling",
"CHF": "Swiss Franc",
"NOK": "Norwegian Krone",
"DKK": "Danish Krone",
"CAD": "Canadian Dollar",
"SEK": "Swedish Krona"
}
# Function fetches the current exchange rates for all currencies
def fetch_rates(currencies):
try:
symbols = ",".join(currencies.keys())
response = requests.get(f"https://api.frankfurter.app/latest?base=PLN&symbols={symbols}")
response.raise_for_status()
data = response.json()
return data["rates"], data.get("date", "N/A")
except requests.RequestException:
print("Error: Failed to connect to the API.")
return None, None
# Main program loop
while True:
try:
print("\n" + "="*50)
amount_pln = float(input("Enter amount in PLN: "))
if amount_pln < 0:
print("Error: Amount cannot be negative!")
continue
except ValueError:
print("Error: Please enter a valid number!")
continue
rates, rate_date = fetch_rates(AVAILABLE_CURRENCIES)
if rates:
print(f"\nConverting {amount_pln} PLN to all available currencies:")
print(f"Exchange rate date: {rate_date}")
table_data = []
for code, name in AVAILABLE_CURRENCIES.items():
rate = rates.get(code)
if rate:
result = amount_pln * rate
table_data.append([code, name, f"{result:.2f}", f"{rate:.4f}"])
print(tabulate(table_data, headers=["Code", "Currency", "Amount", "Rate"], tablefmt="grid"))
else:
print("Error: Failed to fetch exchange rates.")
# Ask the user if they want to perform another conversion
while True:
continue_choice = input("\nDo you want to convert another amount? (yes/no): ").strip().lower()
if continue_choice == "yes":
break
elif continue_choice == "no":
print("Goodbye!")
exit(0)
else:
print("Invalid choice - please type 'yes' or 'no'.")