While and for statements iterating wrong number of times

while loop example
while loop python
while loop c++
do while loop
while loop java
while loop matlab
for loop
do while loop in c

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.

Programming - While Loop, A "While" Loop is used to repeat a specific block of code an unknown number of If we (or the computer) knows exactly how many times to execute a section of is incorrect. ask the user to input another value. go back to line 2 (the while)  It executes a block of statements number of times until the condition becomes false. Loops are of 2 types: entry-controlled and exit-controlled. 'C' programming provides us 1) while 2) do-while and 3) for loop.


That's right. random.randint should be put once within the loop like below:

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:
    holder=random.randint(min,max)
    if holder == 1:
        print ("The die landed on 1.")
        counter_1 +=1
        result.append(1)

    elif holder== 2:
        print ("The die landed on 2.")
        counter_2 +=1
        result.append(2)

    elif holder == 3:
        print ("The die landed on 3.")
        counter_3 +=1
        result.append(3)

    elif holder == 4:
        print ("The die landed on 4.")
        counter_4 +=1
        result.append(4)

    elif holder == 5:
        print ("The die landed on 5.")
        counter_5 +=1
        result.append(5)

    elif holder == 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.")

while loop with a wrong condition, But your loop condition is wrong so you end up running the loop one choice = int (raw_input ("How many number of times , u gonna input the  While loop; For loop #1) While loop: While loop in python is used to execute multiple statement or codes repeatedly until the given condition is true. We use while loop when we don’t know the number of times to iterate. Syntax: while (expression): block of statements Increment or decrement operator


Invent Your Own Computer Games with Python, 4th Edition, When the Python interpreter sees a syntax error, it's because your code isn't written in for a while loop incorrectly, so that it loops the wrong number of times. For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10. Loops are a way to repeat the same code multiple times. The “while” loop. The while loop has the following syntax:


C A Software Engineering Approach: A Software Engineering Approach, A common programming error is to iterate through a loop the wrong number of times. Usually, when you're off, you're off by one because you have used the  The "While" Loop A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't know how many times the user may enter a larger number, so we keep asking "while the number is not between 1 and 10".


Loops: while(), for() and do .. while(), The while() loop has the form: In this rather contrived example, x is halved every time the loop is executed until Loops that repeat the wrong number of times. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Iterate a certain number of times without storing the iteration number anywhere [duplicate]


Failure-Modes-Based Software Reading, For C or C++, the statement a=b assigns b to a and returns the rvalue of the if, if​/else, switch/case, and loops (while, for, or do while) statements are used to a wrong number of times including infinite loops, or each loop accesses wrong  You can use Exit While when you test for a condition that could cause an endless loop, which is a loop that could run an extremely large or even infinite number of times. You can then use Exit While to escape the loop. You can place any number of Exit While statements anywhere in the While loop.