Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to save a variable to an array every time a while loop runs?

I want to save the result of my while loop every time it runs to an array. Then once all conversions have been run, the array that stores all the values will be summed.

raw_user_age = input("Number: ")

x = int(raw_user_age)

g=0

while g < x :
    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)
        g += 1
        print(returnedamount)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

First create your array before the beginning of the loop:

values_returned = []

You can use the append method to add an element to the array every time the loop runs:

values_returned.append(returnedamount)

After the loop is done, you can use the sum() function to get the sum of all values in the array:

sum(values_returned)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading