# While And For Statements Iterating Wrong Number Of Times

## 27 October 2018 - 1 answer

My question is why is it repeating random amount of times instead of 100 times ? The `print (len(result))` was added just to check how many iterations did it actually go over, and it's always a random number instead of 100 times.

I also tried using `for i in range(100)` but it didn't work as well.

``````# Die simulation

import random
min = 1
max = 6

counter_1 = 0
counter_2 = 0
counter_3 = 0
counter_4 = 0
counter_5 = 0
counter_6 = 0

i = 0
result = []

while i < 100:
if random.randint(min,max) == 1:
print ("The die landed on 1.")
counter_1 +=1
result.append(1)

elif random.randint(min,max) == 2:
print ("The die landed on 2.")
counter_2 +=1
result.append(2)

elif random.randint(min,max) == 3:
print ("The die landed on 3.")
counter_3 +=1
result.append(3)

elif random.randint(min,max) == 4:
print ("The die landed on 4.")
counter_4 +=1
result.append(4)

elif random.randint(min,max) == 5:
print ("The die landed on 5.")
counter_5 +=1
result.append(5)

elif random.randint(min,max) == 6:
print ("The die landed on 6.")
counter_6 +=1
result.append(6)

i +=1

print (result)
print (len(result))
print ("In a total of", 100, "number of simulations, The die landed on 1", counter_1, "times, landed on 2", counter_2, "times, landed on 3", counter_3 ,"times, ", end = '')
print ("landed on 4" , counter_4, "times, landed on 5", counter_5, "times, landed on 6", counter_6, "times.")
``````
The problem is that you're calling `random.randint` on each `if` and `elif` check - with the result that it's possible that none of the checks pass, resulting in fewer than 100 numbers in your final array. You should call this function just once per iteration, store the result in a variable, and do the checks on that.