Skip to content

Commit 2e95942

Browse files
Update currency_calculator.py (DhanushNehru#318)
Key Points: • Input Handling: The program takes the base currency and the currency to exchange into from the user. If the input is blank, the loop exits. • Cache: The program uses a cache to store exchange rates for USD and EUR. If the rate is already in the cache, it retrieves it; otherwise, it fetches the rate from the API. • Exchange Rate API: The rates are fetched from floatrates.com in JSON format. • Currency Conversion: The amount is exchanged based on the cached or newly fetched rate, and the result is displayed.
1 parent 928efee commit 2e95942

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed
+22-4
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,55 @@
1+
# Importing necessary modules for currency formatting and HTTP requests
12
from locale import currency
23
import requests
34
import json
45

5-
currency = input().lower()
6+
# Get the base currency input from the user and convert it to lowercase
7+
currency = input("Enter the base currency (e.g., USD, EUR): ").lower()
8+
9+
# Initialize an empty cache to store exchange rates
610
cache = {}
711

12+
# Infinite loop to process exchange requests until the user exits
813
while True:
914

10-
currency_exch = input().lower()
15+
# Get the target currency input from the user and convert it to lowercase
16+
currency_exch = input("Enter the currency to exchange to (leave blank to exit): ").lower()
1117

18+
# If the input is blank, break out of the loop (exit condition)
1219
if currency_exch == '':
1320
break
1421

15-
amount_to_exch = int(input())
22+
# Get the amount to exchange from the user
23+
amount_to_exch = int(input("Enter the amount to exchange: "))
1624

25+
# URL for getting exchange rates from floatrates.com
1726
URL = f'http://www.floatrates.com/daily/{currency}.json'
1827

28+
# Fetch the exchange rates in JSON format
1929
exch = json.loads(requests.get(URL).text)
2030

31+
# Update cache for USD and EUR based on the base currency
2132
if currency == 'usd':
33+
# If base currency is USD, cache EUR rate
2234
cache.update(eur=exch['eur']['rate'])
2335
elif currency == 'eur':
36+
# If base currency is EUR, cache USD rate
2437
cache.update(usd=exch['usd']['rate'])
2538
else:
39+
# For other base currencies, cache both USD and EUR rates
2640
cache.update(usd=exch['usd']['rate'], eur=exch['eur']['rate'])
2741

2842
print("Checking the cache...")
43+
44+
# Check if the target currency's rate is in the cache
2945
if currency_exch in cache:
46+
# If the rate is in the cache, calculate the exchanged amount
3047
rate = round(amount_to_exch * cache[currency_exch], 2)
3148
print("Oh! It is in the cache!")
3249
print(f"You received {rate} {currency_exch.upper()}.")
3350
else:
34-
#print("Sorry, but it is not in the cache!")
51+
# If the rate is not in the cache, fetch it from the exchange rates and store it in cache
52+
print("Sorry, but it is not in the cache!")
3553
cache[currency_exch] = exch[currency_exch]['rate']
3654
rate = round(amount_to_exch * cache[currency_exch], 2)
3755
print(f"You received {rate} {currency_exch.upper()}.")

0 commit comments

Comments
 (0)