Had a general question regarding Invalid Decimal Literal.
I’ve created a basic currency conversion script for ad spends on pivot tables that use USD and I’m converting it to GBP.
I’ve gone over the script and have tried to optimise by using online resources and using Stack Overflow. I’ve experimented with several solutions but I can’t seem to figure out nor understand why it’s happening and how to apply best practice from making ridiculous mistakes.
I’ve attached the code below for your review:
import requests
class Currency_convertor:
# empty dict to store the conversion rates
rates = {}
def __init__(self, url):
data = requests.get(url).json()
# Extracting only the rates from the json data
self.rates = data["rates"]
# function to do a simple cross multiplication between
# the amount and the conversion rates
def convert(self, from_currency, to_currency, amount):
initial_amount = amount
if from_currency != 'EUR' :
amount = amount / self.rates[from_currency]
# limiting the precision to 2 decimal places
amount = round(amount * self.rates[to_currency], 2)
print('{} {} = {} {}'.format(initial_amount, from_currency, amount, to_currency))
***# Driver code
if __name__ == "__main__":
url = str.__add__('http://data.fixer.io/api/latest?access_key=', 95e8617f265030569da10ad979de7ed4)**
c = Currency_convertor(url)
from_country = input("From Country: ")
to_country = input("TO Country: ")
amount = int(input("Amount: ")))*
c.convert(from_country, to_country, amount)
Any advice would be helpful.
Thank you in advance.
It’s been mentioned in the comments that I wasn’t clear the part of the Driver code were it’s causing a Syntax Error with Invalid Decimal Literal has been fixed but it has been replaced with another error: TypeError: expected 1 argument, got 0.
>Solution :
Your driver code has two very basic errors:
-
Your access key (which is a string) is missing quotes around it. Python is trying to interpret it as a number, which is why you get the "invalid decimal" error. Just put quotes around it (better yet, skip the whole
str.__add__thing and just hardcode the whole string since it’s a constant anyway). -
You have an errant
)on one of yourinputlines.
Fixed code:
if __name__ == "__main__":
url = 'http://data.fixer.io/api/latest?access_key=95e8617f265030569da10ad979de7ed4'
c = Currency_convertor(url)
from_country = input("From Country: ")
to_country = input("TO Country: ")
amount = int(input("Amount: "))
c.convert(from_country, to_country, amount)
If you want to separate the access key from the rest of the url, I’d suggest using an f-string. F-strings would also simplify the formatting/rounding you’re doing in your convert function:
import requests
class CurrencyConverter:
def __init__(self, access_key):
# Extract the rates from the json data
self.rates = requests.get(
f'http://data.fixer.io/api/latest?access_key={access_key}'
).json()["rates"]
def convert(self, from_currency, to_currency, amount):
"""Cross multiply the amount and the conversion rates, print result."""
result = amount * self.rates[to_currency] / self.rates[from_currency]
# Round to 2 decimal places.
print(f"{amount} {from_currency} = {result:.2f} {to_currency}")
if __name__ == "__main__":
c = CurrencyConverter('95e8617f265030569da10ad979de7ed4')
from_country = input("From Country: ")
to_country = input("To Country: ")
amount = int(input("Amount: "))
c.convert(from_country, to_country, amount)
From Country: USD
To Country: GBP
Amount: 100
100 USD = 73.88 GBP