-1

I am working on some homework and am at a standstill. The question is as follows:

Create a program that asks the user to enter a series of positive numbers. The program should store the positive numbers in a list. When the user enters a negative number, the program should stop asking the user to enter numbers. The program should display the following data:

  • The lowest number in the list.

  • The highest number in the list.

  • The average of the numbers in the list.

I have started my code and am able to create the list, but I am stumped on how to display the highest and lowest numbers as well as the average. Any help would be appreciated.

number = 1
numbers = []
while ( number > 0):
    number = int(input("Please enter a positive number (Negative to stop: "))
    if number > 0 :
        numbers.append(number)
print (numbers)
Lem_
  • 1
  • 1
    Well, do you have the numbers to show? Once you calculate them, you show them simply by using `print()`. – gshpychka Nov 15 '21 at 20:53
  • `max(numbers); min(numbers); numpy.mean(numbers)` – It_is_Chris Nov 15 '21 at 20:54
  • I used the print(numbers) to display the list of numbers but I believe I need to display the highest and the lowest separate from the list – Lem_ Nov 15 '21 at 20:55
  • Further to the previous comment, in case you are not allowed to use numpy, please consider the definition of average and ```sum(numbers)``` and ```len(numbers)```. – fdireito Nov 15 '21 at 20:57
  • Once you have a list, finding the min, max, and mean are trivial and have already been asked and answered many times before. Please read these links: [How do I ask and answer homework questions?](//meta.stackoverflow.com/q/334822/843953) | [Open letter to students with homework problems](//softwareengineering.meta.stackexchange.com/q/6166/39188) | [How much research effort is expected of Stack Overflow users?](//meta.stackoverflow.com/a/261593/843953) – Pranav Hosangadi Nov 15 '21 at 21:18
  • Does this answer your question? [Find min, max, and average of a list](https://stackoverflow.com/questions/27009247/find-min-max-and-average-of-a-list) – Pranav Hosangadi Nov 15 '21 at 21:21

2 Answers2

0

Python has plenty of built-in ways to do so, though you could always write a function to do it yourself (which if this is a homework/learning thing, you should do be helpful for you). For example, to average them yourself:

count = 0
sum = 0
for i in range(0,len(numbers)):
    count += 1
    sum += numbers[i]
average = sum/count

Python also has a max(list) and min(list) function, but I think it'd probably be better to figure out how to do so without just print(max(list)).

Tyk
  • 68
  • 9
0

You can try something like this:

number = 1
numbers = []
while ( number > 0):
    number = int(input("Please enter a positive number (Negative to stop: "))
    if number > 0 :
        numbers.append(number)
print("Max: {}".format(str(sum(numbers)/len(numbers))
print("Min: {}".format(str(min(numbers))
print("Average: {}".format(str(max(numbers))
toom501
  • 324
  • 1
  • 3
  • 15