#I want to loop my driver code and once done ask the user if they want to convert another currency into MYR and everytime save the returnedamount to an array and once the user has no more currencies they need to convert the program will add the varibles in the array together to produce the sum and print that and if required turn that sum into another currency. class Currency_convertor:
rates = {}
def __init__(self, url):
data = requests.get(url).json()
self.rates = data["rates"]
def convert(self, from_currency, to_currency, amount):
initial_amount = amount
if from_currency != 'EUR' :
amount = amount / self.rates[from_currency]
amount = round(amount * self.rates[to_currency], 2)
print('{} {} = {} {}'.format(initial_amount, from_currency, amount, to_currency))
return amount
Driver code
if name == "main":
YOUR_ACCESS_KEY = ''
url = 'https://api.exchangerate-api.com/v4/latest/USD'
c = Currency_convertor(url)
from_country = input("From Country: ")
to_country = input("TO Country: ")
amount = int(input("Amount: "))
returnedamount=c.convert(from_country, to_country, amount)
CodePudding user response:
Use "While" Instead of "if"
CodePudding user response:
Use a while statement.
Example:
x = 0
while x < 10:
# This will keep running until the condition is no longer True
print(x)
x = 1
# Then run any final stuff outside of the loop
print('Finished looping')