在python中添加一个计数器

时间:2017-01-29 20:27:08

标签: python counter

counter=0

import random

myNum = random.choice(('j', 'Nk', 'p', 'qr', 'stu'))

print(myNum)

if myNum != 'j':

    counter+=1

print(counter)

如何使计数器保持计数'j'不出现的次数并且在'j'出现后不重置?

2 个答案:

答案 0 :(得分:1)

回应您的编辑:

counter=0
import random
for i in range(0,100): #How many times you want to repeat
    myNum = random.choice(('j', 'Nk', 'p', 'qr', 'stu'))
    print(myNum)
    if myNum != 'j':
        counter+=1
print(counter)

counter=0
import random
while 1: #repeats forever
    myNum = random.choice(('j', 'Nk', 'p', 'qr', 'stu'))
    print(myNum)
    if myNum != 'j':
        counter+=1
print(counter)

答案 1 :(得分:0)

捐这个

counter+1

获取计数器的值,向它添加一个并将结果切换到垃圾箱中,如果你不将结果分配给任何它会丢失的......你的意思是counter+=1 < / p>