From ea99039514f1dec52c11a641c20e96e87985b516 Mon Sep 17 00:00:00 2001 From: Brahmajit Mohapatra <57172489+BrahmajitMohapatra@users.noreply.github.com> Date: Thu, 10 Oct 2024 18:09:08 +0530 Subject: [PATCH] Update currency_calculator.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Currency Script/currency_calculator.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Currency Script/currency_calculator.py b/Currency Script/currency_calculator.py index 05f294fb..e0ae9585 100644 --- a/Currency Script/currency_calculator.py +++ b/Currency Script/currency_calculator.py @@ -1,37 +1,55 @@ +# Importing necessary modules for currency formatting and HTTP requests from locale import currency import requests import json -currency = input().lower() +# Get the base currency input from the user and convert it to lowercase +currency = input("Enter the base currency (e.g., USD, EUR): ").lower() + +# Initialize an empty cache to store exchange rates cache = {} +# Infinite loop to process exchange requests until the user exits while True: - currency_exch = input().lower() + # Get the target currency input from the user and convert it to lowercase + currency_exch = input("Enter the currency to exchange to (leave blank to exit): ").lower() + # If the input is blank, break out of the loop (exit condition) if currency_exch == '': break - amount_to_exch = int(input()) + # Get the amount to exchange from the user + amount_to_exch = int(input("Enter the amount to exchange: ")) + # URL for getting exchange rates from floatrates.com URL = f'http://www.floatrates.com/daily/{currency}.json' + # Fetch the exchange rates in JSON format exch = json.loads(requests.get(URL).text) + # Update cache for USD and EUR based on the base currency if currency == 'usd': + # If base currency is USD, cache EUR rate cache.update(eur=exch['eur']['rate']) elif currency == 'eur': + # If base currency is EUR, cache USD rate cache.update(usd=exch['usd']['rate']) else: + # For other base currencies, cache both USD and EUR rates cache.update(usd=exch['usd']['rate'], eur=exch['eur']['rate']) print("Checking the cache...") + + # Check if the target currency's rate is in the cache if currency_exch in cache: + # If the rate is in the cache, calculate the exchanged amount rate = round(amount_to_exch * cache[currency_exch], 2) print("Oh! It is in the cache!") print(f"You received {rate} {currency_exch.upper()}.") else: - #print("Sorry, but it is not in the cache!") + # If the rate is not in the cache, fetch it from the exchange rates and store it in cache + print("Sorry, but it is not in the cache!") cache[currency_exch] = exch[currency_exch]['rate'] rate = round(amount_to_exch * cache[currency_exch], 2) print(f"You received {rate} {currency_exch.upper()}.")