Here is my code:
from random import randint
total = int(input('Enter the number of iterations'))
count=0
for i in range(1,total+1):
print('i',i)
number1=randint(1,5)
print('num1',number1)
number2=randint(1,5)
print('num2',number2)
if number1==number2:
count+=1
print('The number matched',count)
Here is the code result:
Enter the number of iterations
>>> 20
i 1
num1 2
num2 1
i 2
num1 5
num2 3
i 3
num1 5
num2 5
i 4
num1 4
num2 5
i 5
num1 1
num2 1
i 6
num1 5
num2 5
i 7
num1 4
num2 1
i 8
num1 5
num2 5
i 9
num1 4
num2 5
i 10
num1 3
num2 3
i 11
num1 5
num2 5
i 12
num1 1
num2 5
i 13
num1 1
num2 5
i 14
num1 2
num2 4
i 15
num1 1
num2 1
i 16
num1 1
num2 5
i 17
num1 3
num2 4
i 18
num1 2
num2 5
i 19
num1 2
num2 5
i 20
num1 1
num2 4
The number matched 7
I write a program that generates two numbers in the range 1 to 5 inclusive and outputs the string 'The numbers matched' if the numbers match. I want the program to not count the same numbers. For example in my program I want the result:
The number matched 3
5 5 ; 1 1 ; 3 3
You can collect the numbers that matched in a set. That way they will not be added more than once. At the end you can check the size of that set:
from random import randint
total = int(input('Enter the number of iterations: '))
duplicates = set()
for i in range(total):
number1 = randint(1, 5)
number2 = randint(1, 5)
if number1 == number2:
duplicates.add(number1)
print('The number of matches:', len(duplicates))
print('These numbers occurred in pairs:', sorted(duplicates))
A run could for instance output:
Enter the number of iterations: 20
The number of matches: 3
These numbers occurred in pairs: [1, 3, 4]