Write a python program which prints the frequency of the numbers that were
given as input by the user. Stop taking input when you find the string “STOP”. Do
not print the frequency of numbers that were not given as input. Use a dictionary
to solve the problem

Write a python program which prints the frequency of the numbers that were given as input by the user Stop taking input when you find the string STOP Do not pri class=

Respuesta :

Answer:

The program is as follows:

my_list = []

inp = input()

while inp.lower() != "stop":

   my_list.append(int(inp))

   inp = input()

Dict_freq = {}

for i in my_list:

   if (i in Dict_freq):

       Dict_freq[i]+= 1

   else:

       Dict_freq[i] = 1

for key, value in Dict_freq.items():

   print (key,"-",value,"times")

Explanation:

This initializes a list of elements

my_list = []

This gets input for the first element

inp = input()

The following iteration is repeated until user inputs stop --- assume all user inputs are valid

while inp.lower() != "stop":

This appends the input to the list

   my_list.append(int(inp))

This gets another input from the user

   inp = input()

This creates an empty dictionary

Dict_freq = {}

This iterates through the list

for i in my_list:

This adds each element and the frequency to the list

   if (i in Dict_freq):

       Dict_freq[i]+= 1

   else:

       Dict_freq[i] = 1

Iterate through the dictionary

for key, value in Dict_freq.items():

Print the dictionary elements and the frequency

   print (key,"-",value,"times")