Find the number of the same elements on both Python lists.

Asked 2 years ago, Updated 2 years ago, 15 views

import random
yi= 0
while yi != 1:
    a = int (input('Enter a number:')
    b = int (input('Enter a number: '))
    c = int (input('Enter a number: '))
    d = int (input('Enter a number: ')))
    e = int (input('Enter a number: '))
    listme = [a, b, c, d, e]
    gg = random.sample(range(1, 15),5)
    r1 = random.sample(range(1, 15),5)
    r2 = random.sample(range(1, 15),5)
    r3 = random.sample(range(1, 15),5)
    r4 = random.sample(range(1, 15),5)
    r5 = random.sample(range(1, 15),5)
    r6 = random.sample(range(1, 15),5)
    r7 = random.sample(range(1, 15),5)
    r8 = random.sample(range(1, 15),5)
    r9 = random.sample(range(1, 15),5)
    listme.sort()
    gg.sort()
    r1.sort()
    r2.sort()
    r3.sort()
    r4.sort()
    r5.sort()
    r6.sort()
    r7.sort()
    r8.sort()
    r9.sort()
    yii=100 #0 is written as 100 so that no one can come out
    menumber=0
    r1n=0
    r2n=0
    r3n=0
    r4n=0
    r5n=0
    r6n=0
    r7n=0
    r8n=0
    r9n=0
    n= [yii, menumber, r1n, r2n, r3n, r4n, r5n, r6n, r7n, r8n, r9n]
    n.sort(reverse=True)
    print('This time, winning number: ',gg)
    print('Your number: ',listme',' correct number: ',menumber)
    print('number of robot 1: ',r1,' correct number: ',r1n)
    print('number of robot 2: ',r2,' correct number: ',r2n)
    print('number of robot 3: ',r3,' correct number: ',r3n)
    print('number of robot 4: ',r4', correct number: ',r4n)
    print('number of robot 5: ',r5', correct number: ',r5n)
    print('number of robot6: ',r6', correct number: ',r6n)
    print('number of robot7: ',r7', correct number: ',r7n)
    print('number of robot 8: ',r8', correct number: ',r8n)
    print('number of robot 9: ',r9', correct number: ',r9n)
    print('You're', n.index(member), 'back!')
    mode=input('Do you want to continue? (y/n):')
    if mode == 'y':
        yi=0
    elif mode == 'n':
        yi=1

How do you express the number of correct answers?

python

2022-09-20 19:56

1 Answers

Python's set is useful in this case.

For the set-to-set & operation, you can find the intersection, and you can find the number of intersections in len.


import random

def main():
    yi = 0
    while yi != 1:
        # : a int (('Enter number :' input)).
        # : b int (('Enter number :' input)).
        # c =int(input('숫자를 입력하세요: '))
        # : d int (('Enter number :' input)).
        # : e int (('Enter number :' input)).
        Listme [Enter int (" (" A number of flight engineer : " _ for in range (5)].
        gg = random.sample(range(1, 15), 5)
        # r1 = random.sample(range(1, 15),5)
        # r2 = random.sample(range(1, 15),5)
        # r3 = random.sample(range(1, 15),5)
        # r4 = random.sample(range(1, 15),5)
        # # r5 = random.sample(range(1, 15),5)
        # # r6 = random.sample(range(1, 15),5)
        # # r7 = random.sample(range(1, 15),5)
        # # r8 = random.sample(range(1, 15),5)
        # # r9 = random.sample(range(1, 15),5)
        r = [random.sample(range(1, 15), 5) for i in range(9)]
        # # listme.sort()
        # # gg.sort()
        # # r1.sort()
        # # r2.sort()
        # # r3.sort()
        # # r4.sort()
        # # r5.sort()
        # # r6.sort()
        # # r7.sort()
        # # r8.sort()
        # # r9.sort()
        # yii=100 #0 is written as 100 so that no one can come out

        menumber = len(set(gg) & set(listme))
        # # r1n=0
        # # r2n=0
        # # r3n=0
        # # r4n=0
        # # r5n=0
        # # r6n=0
        # # r7n=0
        # # r8n=0
        # # r9n=0
        rn = [len(set(gg) & set(r[i])) for i in range(9)]

        n = rn + [menumber]
        n.sort(reverse=True)
        print("This time, winning number: ", gg)
        print("Your number: ", listme, "Correct number: ", number)
        # print('number of robot 1: ',r1,' correct number: ',r1n)
        # print('number of robot 2: ',r2,' correct number: ',r2n)
        # print('number of robot 3: ',r3,' correct number: ',r3n)
        # print('number of robot 4: ',r4', correct number: ',r4n)
        # print('number of robot 5: ',r5', correct number: ',r5n)
        # print('number of robot6: ',r6', correct number: ',r6n)
        # print('number of robot7: ',r7', correct number: ',r7n)
        # print('number of robot 8: ',r8', correct number: ',r8n)
        # print('number of robot 9: ',r9', correct number: ',r9n)
        # print('You're', n.index(member), 'back!')
        for i in range(9):
            print("f"number of robot {i+1}: {r[i]}, correct count: {rn[i]}")
        print(f) You're {n.index(member) +1} etc.)
        mode = input("Do you want to continue? (y/n): ")
        if mode == "y":
            yi = 0
        elif mode == "n":
            yi = 1


if __name__ == "__main__":
    main()


2022-09-20 19:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.