0

I want to loop my driver code and, once done, ask the user if they want to convert another currency into MYR. Everytime through the loop, 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. 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)
2
  • 1
    Do not add your access key to your question, a key should not be visible on any of your code. Commented Dec 10, 2021 at 9:05
  • 1
    If this is your real access key, you should rotate it instantly. Commented Dec 10, 2021 at 9:06

2 Answers 2

1

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')
Sign up to request clarification or add additional context in comments.

Comments

0

Use "While" Instead of "if"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.