Home > Net >  Make algorithm that checks which integer user inputs the most?
Make algorithm that checks which integer user inputs the most?

Time:01-02

So, I want to make an algorithm, that prints out random numbers (from 1-10), and then checks which numbers the user mostly inputs.

For example:

The first random numbers are: 3, 7, 1

The second random numbers are: 2, 4, 3

The third random numbers are: 1, 9, 3

and the input afterwards is:

3, 3, 9

The algorithm should check those 3 inputs, and then prints out which number the user has mostly typed. (So for this it would be 3.)

For now I have imported random and just checked if there are no numbers that are similar.

import random

number_1 = random.randint(1, 10)
number_2 = random.randint(1, 10)
number_3 = random.randint(1, 10)

if number_1 == number_2 or number_1 == number_3 or number_2 == number_3:
    exit(f"Number 1: {number_1}\nNumber 2: {number_2}\nNumber 3: {number_3}")

I will research a bit more, but I still wanted to post it here because I surely need some help.

CodePudding user response:

Based on your description and the above comments, the following may be the probable solution :

import statistics

input_data = [ ] # To store all values.
for i in range(5): # Some no. of iterations.
    input_data.extend([*map(int, input(f"Round {i 1} (Enter any 3 numbers between 1 and 10 separated by comma):").split(","))])

print("The most entered number(s) :", statistics.multimode(input_data))

CodePudding user response:

You can use a dictionary that maps each number to the frequency of occurring and then check the number that occurred the most by looping through the values. As an implementation:

occur = {}
x = input ("number")
if x in occur.keys():
  occur[x]  = 1
else:
  occur[x] = 1

After registering the values in a dictionary, you could loop upon the values in order to know the maximum repeating

max_value = -1
output = ''
for key in occur.keys():
  if occur[key] > max_value:
    max_value = occur_key
    output = key

The last step is the output of the done work

print ("The most repeating value is "   output   " with the number of repetitions of "   str(max_value))
  • Related