File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <div align =" center " >
2+
3+ # Currency Converter
4+
5+ </div >
6+
7+ ### Description:
8+ This Python script converts an amount from one currency to another using real-time exchange rates fetched from an API.
9+
10+ ### How to Use:
11+ 1 . Run the script in a Python environment.
12+ 2 . Enter the amount to convert when prompted.
13+ 3 . Enter the source currency (e.g., USD, EUR) when prompted.
14+ 4 . Enter the target currency (e.g., USD, EUR) when prompted.
15+ 5 . The script will fetch the real-time exchange rates and perform the currency conversion, displaying the result.
Original file line number Diff line number Diff line change 1+ import requests
2+
3+
4+ def convert_currency (amount , from_currency , to_currency ):
5+ api_key = 'YOUR_API_KEY'
6+ url = f'https://api.exchangerate-api.com/v4/latest/{ from_currency } '
7+ response = requests .get (url )
8+ data = response .json ()
9+ exchange_rate = data ['rates' ][to_currency ]
10+ converted_amount = amount * exchange_rate
11+ return converted_amount
12+
13+
14+ def main ():
15+ amount = float (input ("Enter the amount to convert: " ))
16+ from_currency = input ("Enter the source currency: " ).upper ()
17+ to_currency = input ("Enter the target currency: " ).upper ()
18+ converted_amount = convert_currency (amount , from_currency , to_currency )
19+ print (f"{ amount } { from_currency } is equal to { converted_amount } { to_currency } " )
20+
21+
22+ if __name__ == "__main__" :
23+ main ()
You can’t perform that action at this time.
0 commit comments