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

Store Values generated in a while loop on a list in python

Just a simple example of what i want to do:

numberOfcalculations = 3
count = 1
while contador <= numberOfcalculations:
    num = int(input(' number:'))
    num2 = int(input(' other number:'))
    
    calculate = num * num2
    print(calculate)
    count = count + 1

How do i store the 3 different values that "calculate" will be worth in a list []?

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

>Solution :

When you initialize calculate as list type you can append values with + operator:

numberOfcalculations = 3
count = 1
calculate = []
while count <= numberOfcalculations:
    num = int(input(' number:'))
    num2 = int(input(' other number:'))
    
    calculate += [ num * num2 ]
    print(calculate)
    count = count + 1

Also you have to change contador somehow or you will end up in an infinite loop maybe. I used count here instead to give the user the ability to input 3 different calculations.

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