Reputation: 1
Write a program that reads 6 temperatures (in the same format as before), and displays the maximum, minimum, and mean of the values. Hint: You should know there are built-in functions for max and min. If you hunt, you might also find one for the mean.
I managed to do this program. ############################################################################
Modify the previous program so that it can process any number of values. The input terminates when the user just pressed "Enter" at the prompt rather than entering a value.
I want my program to calculate the maximum, minimum and average of any numbers entered by the user using the built in functions such as import. The user will have to be able to enter as many numbers he can in the input
import statistics
def my_function():
temp=input("Enter temperature:")
temp=int(temp)
mean_value=statistics.mean(temp)
print(mean_value)
my_function()
Upvotes: 0
Views: 1152
Reputation: 2894
You don't need to import anything to compute minimum, maximum and average. You can use min(x)
, max(x)
and sum(x)/len(x)
to compute these values from a list of numeric values.
To do so in your program, however, you have to store multiple inputs in a list of numeric values. You can do so by initiating an empty list at the beginning and prompting new numbers until no more number is entered:
def my_function():
temps = [] ## Empty list to be filled
asking = True ## Switch to keep asking
while asking:
temp=input("Enter temperature:")
try:
temp=int(temp)
temps.append(temp) ## If a number was entered, add it to the list
except: ## Stop asking if no number was entered
asking = False
if len(temps)>0:
mean_value=sum(temps)/len(temps)
min_value = min(temps)
max_value = max(temps)
print(mean_value,min_value,max_value)
else:
print("no numbers were entered")
Upvotes: 1