I am currently working on an ATM using python, and one part of it is currency exchange. I have extracted the real-time exchange rate using API, with base currency as EUR. Since the From currency varies from user input, I got an if-statement to indicate whenever the From currency is not EUR, it will change its calculation method. However, Python skips my if-statement and jump to the next line.
account = {"user1":{"password": "secret",
"balance": {"USD": 10000,
"HKD": 10},
},
}
data = requests.get('http://data.fixer.io/api/latest?access_key=my_access_key').json()
Currency = data['rates']
...
acc_select = int(input('Please choose your account (Account number): '))
FROM_currency = account['user1']['balance'][acc_select]
TO_currency = input('To Currency (e.g. USD): ')
To_currency = TO_currency.upper()
amount = int(input(f'Enter amount of {TO_currency} you would like to get: '))
if FROM_currency != 'EUR':
ex_amount = amount * Currency[FROM_currency]
ex_amount = round(amount / Currency[TO_currency])
print(f'{ex_amount} {FROM_currency} = {amount} {TO_currency})
When I was testing to exchange for 50 HKD using USD, I suppose the result would be 10 USD = 78 HKD. However, the result was 8 USD = 78 HKD, meaning that Python skips the if-statement even my FROM_currency is not EUR.
Thank you and your help is much appreciated.
Aucun commentaire:
Enregistrer un commentaire