Jaesun Lee
Jaesun Lee

Reputation: 15

How do I loop an if statement until needed and then stop

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)

Upvotes: 0

Views: 34

Answers (2)

Kelvin Ducray
Kelvin Ducray

Reputation: 296

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')

Upvotes: 1

Sione Hoghen K
Sione Hoghen K

Reputation: 33

Use "While" Instead of "if"

Upvotes: 0

Related Questions