Posts: 4
Threads: 1
Joined: Mar 2024
Mar-06-2024, 10:48 PM
(This post was last modified: Mar-07-2024, 03:26 AM by deanhystad.)
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.
Posts: 4,783
Threads: 76
Joined: Jan 2018
Mar-07-2024, 06:47 AM
(This post was last modified: Mar-07-2024, 06:47 AM by Gribouillis.)
(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 »
Posts: 7,313
Threads: 123
Joined: Sep 2016
Mar-07-2024, 06:00 PM
(This post was last modified: Mar-07-2024, 06:01 PM by snippsat.)
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}
Posts: 7,313
Threads: 123
Joined: Sep 2016
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
Posts: 4
Threads: 1
Joined: Mar 2024
Mar-07-2024, 11:36 PM
(This post was last modified: Mar-08-2024, 12:27 AM by preethy12ka4.)
I used a package called CurrencyConverter and it worked fine. Thank you for the reply
Posts: 7,313
Threads: 123
Joined: Sep 2016
Mar-07-2024, 11:58 PM
(This post was last modified: Mar-07-2024, 11:58 PM by snippsat.)
(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.
Posts: 4
Threads: 1
Joined: Mar 2024
Mar-08-2024, 12:55 AM
(This post was last modified: Mar-08-2024, 05:45 AM by Gribouillis.)
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)
Posts: 7,313
Threads: 123
Joined: Sep 2016
Mar-08-2024, 10:25 AM
(This post was last modified: Mar-08-2024, 10:25 AM by snippsat.)
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}")
Posts: 4
Threads: 1
Joined: Mar 2024
thank you for the help, it worked
|