Home > Blockchain >  How to show requested data
How to show requested data

Time:11-16

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)

CodePudding user response:

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)).

CodePudding user response:

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))
  • Related