How do I print how many times a while loop has run?
Example:
from random import*
c=0
while c<3 :
a=randint(1,6)
b=randint(1,6)
if a==b:
print(a,b)
c=c+1
How do I print how many times a while loop has run?
Example:
from random import*
c=0
while c<3 :
a=randint(1,6)
b=randint(1,6)
if a==b:
print(a,b)
c=c+1
You can just add another counter.
from random import *
c = 0
loop_counter = 0
while c < 3:
a = randint(1,6)
b = randint(1,6)
if a==b:
print(a,b)
c += 1
loop_counter += 1
print(f"The loop run {loop_counter} times.")
By adding a "counter variable", something that increases for each loop.
As an example:
from random import*
c=0
counter = 0 #define
while c<3 :
counter += 1 #add one
print (counter) #print
a=randint(1,6)
b=randint(1,6)
if a==b:
print(a,b)
c=c+1
You can also just print "counter" after the while loop has ended to get a final tally.
from random import *
c=0
Counter=0
while c<3 :
a=randint(1,6)
b=randint(1,6)
if a==b:
print(a,b)
c=c+1
Counter+=1
print (Counter)