简体   繁体   中英

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

Use "While" Instead of "if"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM