python: collections counter

®Geovin Du Dream Park™ / 2024-06-08 / 原文

 

   entrance_fee = [70, 80, 105, 110, 120, 125]
    entrance_fee = entrance_fee + [0]
    index =6
    k = 0
    while k<6 and index==6:  # 因为这里index 是6 ,初始值必须是6
        if 115<entrance_fee[k]:
            index=k
        k=k+1
    print("index",index)
    for k in range(6,index,-1):
        entrance_fee[k]=entrance_fee[k-1]
    entrance_fee[index]=115
    print(entrance_fee)

    result = ['Pass', 'Fail', 'Pass', 'Pass', 'Pass', 'Pass', 'Pass', 'Pass', 'Pass', 'Pass']
    p=Counter(result)  # 引用库  from collections import Counter
    #print(dict(p)) #分类计数的结果
    #print(p['Pass'])
    if(p["Pass"]>=5):
        print("S/he gets the certificate of completion.")
    else:
        print("S/he needs to pass more tests.")


    while True:
        num=0
        P=0
        result = []
         #也可以用while 或for
        s=0
        while num<10 and P<=5: #当达到5时,就可以确定极格,不用再计数了
            s=num+1
            test=input(str(s)+":input Pass/Fail")
            result.append(test.strip())  #去除两头空格
            num+=1
        '''
        for k in range(0,10):
            num=k+1
            test = input(str(num)+":input Pass/Fail")
            result.append(test.strip())  # 去除两头空格
        '''

        print("score",result)

        for k in range(0, 10):
            if result[k] == "Pass":
                P = P + 1
            if P == 5:  #5极格了,就结束
                break
        if P >= 5:
            print("S/he gets the certificate of completion.")
        else:
            print("S/he needs to pass more tests.")
        yn=input("Whether to continue testing(Y/N)?")
        if(yn=='Y' or yn=='y'):
            continue
        else:
            break