Reputation: 401
I need to bring a value from my Json file
{
"AUD": 1.5974,
"BGN": 1.9558,
"BRL": 4.0666,
"CAD": 1.5889,
"CHF": 1.1671,
"CNY": 7.8016,
"CZK": 25.399,
"DKK": 7.4484,
"GBP": 0.872,
"HKD": 9.6641,
"HRK": 7.4435,
"HUF": 311.66,
"IDR": 16938.0,
"ILS": 4.2959,
"INR": 80.183,
"ISK": 122.7,
"JPY": 129.75,
"KRW": 1330.5,
"MXN": 22.776,
"MYR": 4.819,
"NOK": 9.5308,
"NZD": 1.7031,
"PHP": 64.544,
"PLN": 4.2217,
"RON": 4.6675,
"RUB": 70.32,
"SEK": 10.12,
"SGD": 1.6207,
"THB": 38.512,
"TRY": 4.8372,
"USD": 1.2316,
"ZAR": 14.572
}
For example, in AUD: the 1.5974 value I need to bring..
My script:
import json
with open('2018-03-22.txt', 'r') as f:
data = json.load(f)
brl = (data["BRL"])
usd = float(data["USD"])
aud = (data["AUD"])
coin=str(input('choose coin: ')).lower()
amount=int(input('insert amount: '))
if coin==aud:
print(aud*amount)
When I print this, I won't see any error, but it doesn't print anything.
Do I need to convert str to float?
Thanks for your time and help.
Upvotes: 3
Views: 1278
Reputation: 51185
You are comparing coin
to aud
, which is equal to data['AUD']
, which is an integer. Try this instead:
if coin=='aud':
print(aud*amount)
However, a better approach might be something like:
try:
print(data[coin.upper()] * amount)
except:
print('Invalid currency')
Which would be a flexible way to convert to many different currencies, instead of checking for each currency.
Upvotes: 7