200k views
0 votes
Write a program that prompts the user to enter a series of numbers between 0 and 10 asintegers. The user will enter all numbers on a single line, and scores will be separatedby spaces. You cannot assume the user will supply valid integers, nor can you assumethat they will enter positive numbers, so make sure that you validate your data. You donot need to re-prompt the user once they have entered a single line of data. Once youhave collected this data you should compute the following: • The largest value • Thesmallest value • The range of values • The mode (the value that occurs the most often) •A histogram that visualizes the frequency of each number Here’s a sample running of theprogram.

1 Answer

0 votes

Answer:

The program in Python is as follows:

import collections

from collections import Counter

from itertools import groupby

import statistics

str_input = input("Enter a series of numbers separated by space: ")

num_input = str_input.split(" ")

numList = []

for num in num_input:

if(num.lstrip('-').isdigit()):

if num[0] == "-" or int(num)>10:

print(num," is out of bound - rejecting")

else:

print(num," is valid - accepting")

numList.append(int(num))

else:

print(num," is not a number")

print("Largest number: ",max(numList))

print("Smallest number: ",min(numList))

print("Range: ",(max(numList) - min(numList)))

print("Mode: ",end = " ")

freqs = groupby(Counter(numList).most_common(), lambda x:x[1])

print([val for val,count in next(freqs)[1]])

count_freq = {}

count_freq = collections.Counter(numList)

for item in count_freq:

print(item, end = " ")

lent= int(count_freq[item])

for i in range(lent):

print("#",end=" ")

print()

Step-by-step explanation:

See attachment for program source file where comments are used as explanation. (Lines that begin with # are comments)

The frequency polygon is printed using #'s to represent the frequency of each list element

Write a program that prompts the user to enter a series of numbers between 0 and 10 asintegers-example-1
Write a program that prompts the user to enter a series of numbers between 0 and 10 asintegers-example-2
User Thehhv
by
4.1k points