Python Forum
currency converter using forex-python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
currency converter using forex-python
#1
I installed forex-python as pip in cmd and package in pycharm
I used an online code for currency conversion, which is throwing an error as shown below.
from forex_python.converter import CurrencyRates
cr = CurrencyRates()
amount = int(input("Please enter the amount you want to convert: "))
from_currency = input("Please enter the currency code that has to be converted: ").upper()
to_currency = input("Please enter the currency code to convert: ").upper()
print("You are converting", amount, from_currency, "to", to_currency,".")
output = cr.convert(from_currency, to_currency, amount)
print("The converted rate is:", output)
Output:
Please enter the amount you want to convert: 123 Please enter the currency code that has to be converted: usd Please enter the currency code to convert: cad You are converting 123 USD to CAD .
Traceback (most recent call last):
Error:
File "C:\Users\13366\PycharmProjects\pythonProject\Project assignment\currency conertor.py", line 8, in <module> output = cr.convert(from_currency, to_currency, amount) File "C:\Users\13366\PycharmProjects\pythonProject\venv\lib\site-packages\forex_python\converter.py", line 108, in convert raise RatesNotAvailableError("Currency Rates Source Not Ready") forex_python.converter.RatesNotAvailableError: Currency Rates Source Not Ready Process finished with exit code 1
deanhystad write Mar-07-2024, 03:26 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
(Mar-06-2024, 10:48 PM)preethy12ka4 Wrote: I used an online code for currency conversion, which is throwing an error as shown below.
The answer is in the source code. The module tries to fetch a web resource at the site theforexapi.com with a date parameter, and it receives a response status different from 200 which it interpretes as an unsuccessful request.

There is nothing you can do except perhaps file an issue to the developer. If you look at the issues in the project page, you will see that several people have similar problems using this package.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Quick test is down,if look at converter.py
def _source_url(self):
     return "https://theforexapi.com/api/"
So the theforexapi👀 is down,then nothing will work in this library.

If look at isssus so has ddofborg has done a altentaive.
Test it no has PyPi install just a Repo,so clone down and run.
G:\div_code\forex_env
(forex_env) λ git clone https://github.com/ddofborg/exchange_rates.git
Cloning into 'exchange_rates'...
remote: Enumerating objects: 25, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 25 (delta 10), reused 6 (delta 2), pack-reused 0
Receiving objects: 100% (25/25), 8.59 KiB | 4.29 MiB/s, done.
Resolving deltas: 100% (10/10), done.

G:\div_code\forex_env
(forex_env) λ ls
exchange_rates/  Include/  Lib/  pyvenv.cfg  Scripts/

G:\div_code\forex_env
(forex_env) λ cd exchange_rates\

G:\div_code\forex_env\exchange_rates (main -> origin)
(forex_env) λ ptpython
>>> from exchange_rates import get_exchange_rates
>>> get_exchange_rates('USD', target_currencies=['EUR', 'CAD', 'USD'], on_date='2023-10-01')
{'EUR': 0.9496676163342831, 'CAD': 1.3613485280151947, 'USD': 1.0}

# Test a newer date
>>> get_exchange_rates('USD', target_currencies=['EUR', 'CAD', 'USD'], on_date='2024-03-05')
{'EUR': 0.9217439395335976, 'CAD': 1.3592957876301963, 'USD': 1.0}
Reply
#4
Also if do some coding can extend exchange_rates to do your task.
from exchange_rates import get_exchange_rates

def conversion_rate(base_currency, target_currency, on_date=None):
    rates = get_exchange_rates(base_currency, target_currencies=[target_currency], on_date=on_date)
    return rates[target_currency]

def convert_currency(base_currency, target_currency, amount, on_date=None):
    rate = conversion_rate(base_currency, target_currency, on_date=on_date)
    return amount * rate

if __name__ == '__main__':
    base_currency = 'USD'
    target_currency = 'CAD'
    amount = 123
    date = '2024-03-07'
    converted_amount = convert_currency(base_currency, target_currency, amount, on_date=date)
    print(f"123 USD is equivalent to {converted_amount:.3f} {target_currency} on {date}")
Output:
123 USD is equivalent to 166.013 CAD on 2024-03-07
Reply
#5
I used a package called CurrencyConverter and it worked fine. Thank you for the reply
Reply
#6
(Mar-07-2024, 11:36 PM)preethy12ka4 Wrote: thank you for the reply. I am really new to python and learning things. i installed the package exchange_rates in pycharm. I ran the code you gave me and it is throwing the below error. have I installed the wrong package ?
Yes wrong package,as mention code is not on PyPi is a Repo exchange_rates.
So can clone down code as i show using Git.
Or if click at green code button there is a Download Zip link.
Reply
#7
can you please help me do an exception if we enter a wrong currency code

try:
from currency_converter import CurrencyConverter
cr = CurrencyConverter()
amount = float(input("Please enter the amount you want to convert: "))
from_currency = input("Please enter the currency code that has to be converted: ").upper()
to_currency = input("Please enter the currency code to convert: ").upper()
print("You are converting", amount, from_currency, "to", to_currency,".")
output = cr.convert(amount,from_currency, to_currency )
except exit:
print("test")
else:
print(amount,from_currency,"equals",output,to_currency)
Gribouillis write Mar-08-2024, 05:45 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#8
Example like this.
try:
    cr = CurrencyConverter()
    amount = float(input("Please enter the amount you want to convert: "))
    from_currency = input("Please enter the currency code that has to be converted: ").upper()
    to_currency = input("Please enter the currency code to convert: ").upper()
    print(f"You are converting {amount} {from_currency} to {to_currency}.")
    output = cr.convert(amount, from_currency, to_currency)
    print(f"{amount} {from_currency} equals {output} {to_currency}")
except ValueError:
    print("Invalid input! Please ensure the amount is a number.")
except Exception as error:
    print(f"An error occurred: {error}")
Reply
#9
thank you for the help, it worked
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,279 Jun-29-2023, 11:57 AM
Last Post: gologica
  Help with basic weight converter PythonSquizzel 3 1,421 Jun-29-2022, 02:42 PM
Last Post: deanhystad
  xml extract currency 3lnyn0 5 1,703 Apr-30-2022, 10:29 AM
Last Post: snippsat
  calculate daily return in percent in forex as to some strategy? alen 1 2,221 Mar-12-2021, 10:03 AM
Last Post: buran
  Converter Souls99 2 2,394 Aug-01-2020, 01:27 AM
Last Post: Souls99
  Help me get this image converter code working? NeTgHoSt 0 2,089 Jul-14-2020, 10:36 PM
Last Post: NeTgHoSt
  Currency converter Scott 5 3,007 Jun-14-2020, 11:59 PM
Last Post: Scott
  How to use 2to3.py converter under Windows OS samsonite 2 7,324 Mar-02-2019, 05:49 AM
Last Post: samsonite
  Currency formatting the third element in a 2D list RedSkeleton007 3 3,218 Mar-09-2018, 12:53 PM
Last Post: j.crater
  Can't get a downloaded file converter to work Godotisnothere 1 3,188 Jan-24-2017, 06:01 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020