Store Values generated in a while loop on a list in python
Question:
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 []?
Answers:
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.
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 []?
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.