Home > OS >  Store Values generated in a while loop on a list in python
Store Values generated in a while loop on a list in python

Time:12-02

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 []?

CodePudding user response:

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.

  • Related