import sys
import requests
date = str(sys.argv[1])
from_currency=str(sys.argv[2]).upper()
to_currency= str(sys.argv[3]).upper()
amount=float(sys.argv[4])
response = requests.get(f"https://api.frankfurter.app/{date}?amount={amount}&from={from_currency}&to={to_currency}")
print(f"{amount} {from_currency} is {response.json()['rates'][to_currency]} {to_currency} on {date}")
I tried running the following command on the terminal
python main.py '2020-01-01' 'USD' 'GBP' 2
I was expecting "3 USD is 86.77 GBP on 2020-01-01", but instead I get this error, which I do not understand
Traceback (most recent call last):
File "C:\Users\samee\anaconda3\lib\site-packages\requests\models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\samee\anaconda3\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\samee\anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\samee\anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\samee\OneDrive\Desktop\PAI\Assignments\Assignment 1b\main.py", line 34, in <module>
print(f"{amount} {from_currency} is {response.json()['rates'][to_currency]} {to_currency} on {date}")
File "C:\Users\samee\anaconda3\lib\site-packages\requests\models.py", line 917, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: [Errno Expecting value] : 0
I ran your code, I am able to get the response. This error comes when sometime the website sends empty response.
By running your code, I got the response : 2.0 USD is 1.5147 GBP on 2020-01-01
To handle such empty or other response add Error Handling: