Python Forum
Help with array smallest biggest number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with array smallest biggest number
#1
Hello
I need some help to: User inserts 10 numbers that goes to an array, and then the program prints the smallest number, the biggest number and the average of all 10 numbers.
This is an exercise that will help me understand a few things...thanks for the help.

here is my code so far:

numberlist = [None]*10
highest = 0
lowest = 0
for n in range(10):
    numberlist[n]=int(input("insert a number "+str(n+1)+": "))


print(......)
Reply
#2
(Nov-30-2017, 12:44 AM)thanikos Wrote: Hello
I need some help to: User inserts 10 numbers that goes to an array, and then the program prints the smallest number, the biggest number and the average of all 10 numbers.
This is an exercise that will help me understand a few things...thanks for the help.

here is my code so far:

numberlist = [None]*10
highest = 0
lowest = 0
for n in range(10):
    numberlist[n]=int(input("insert a number "+str(n+1)+": "))


print(......)

Hello Thanikos,
are there any other specific instructions? For example, do you need to loop through the list to calculate average, maximum, and minimum values? As opposed to using built-in functions like max() and min()? Without using built-in functions it is a bit more work, but it is also great practice.
# Say this is your list, and we want to find the highest number
my_list = [1, 5, 8, 4, 2, 42, 53, 12]
# First you'd run through the list and find the number 1,
# Then 5 which is greater then one, okay that's our new max.
# Then 8 which again is greater than 5, our new max, let's make that our new max
# Then 4, which is not greater than 8 so skip over it
# Then 2, just like 4, skip it
# Then 42, which is greater than 8, our current max, let's replace it with 42
# Then 53, which again is greater than 42, replace it
# Then 12, which is not greater than 53 so skip it
# Now print the highest number, which would be 53.
# Same process for minimum number but reversed.
If you can use these built-in functions, you could change the name of numberlist[n] to something simple, say, command = input() for example. To calculate your values you'd have to append your inputs to your list after accepting the input with mylist.append(command). For average you could simply do sum(my_list) / len(my_list), for maximum it would be max(my_list), for minimum it would be min(my_list).


Hope that helps,
Prrz
Reply
#3
(Nov-30-2017, 02:11 AM)Prrz Wrote:
(Nov-30-2017, 12:44 AM)thanikos Wrote: Hello
I need some help to: User inserts 10 numbers that goes to an array, and then the program prints the smallest number, the biggest number and the average of all 10 numbers.
This is an exercise that will help me understand a few things...thanks for the help.

here is my code so far:

numberlist = [None]*10
highest = 0
lowest = 0
for n in range(10):
    numberlist[n]=int(input("insert a number "+str(n+1)+": "))







print(......)

Hello Thanikos,
are there any other specific instructions? For example, do you need to loop through the list to calculate average, maximum, and minimum values? As opposed to using built-in functions like max() and min()? Without using built-in functions it is a bit more work, but it is also great practice.
# Say this is your list, and we want to find the highest number
my_list = [1, 5, 8, 4, 2, 42, 53, 12]
# First you'd run through the list and find the number 1,
# Then 5 which is greater then one, okay that's our new max.
# Then 8 which again is greater than 5, our new max, let's make that our new max
# Then 4, which is not greater than 8 so skip over it
# Then 2, just like 4, skip it
# Then 42, which is greater than 8, our current max, let's replace it with 42
# Then 53, which again is greater than 42, replace it
# Then 12, which is not greater than 53 so skip it
# Now print the highest number, which would be 53.
# Same process for minimum number but reversed.
If you can use these built-in functions, you could change the name of numberlist[n] to something simple, say, command = input() for example. To calculate your values you'd have to append your inputs to your list after accepting the input with mylist.append(command). For average you could simply do sum(my_list) / len(my_list), for maximum it would be max(my_list), for minimum it would be min(my_list).


Hope that helps,
Prrz
Hello Prrz
The built in max and min did the work! Thanks. Here is the working code:
lst = [None]*10
for n in range(10):
    lst[n]=int(input("insert a number "+str(n+1)+": "))
    lst.append
print("maximum element is :",max(lst), "\nmnmm element is: ", min(lst),
      "\naverage is {:.0f}" .format(max(lst)/min(lst)))
Looks like something like this:
insert a number 1: 2
insert a number 2: 10
insert a number 3: 2
insert a number 4: 2
insert a number 5: 2
insert a number 6: 2
insert a number 7: 2
insert a number 8: 2
insert a number 9: 2
insert a number 10: 2
maximum element is : 10
mnmm element is: 2
average is 5
Reply
#4
>>> zz = [5, 78, 4, -1, 974, 22, 1053]
>>> min(zz)
-1
>>> max(zz)
1053
>>> sum(zz)/len(zz)
305.0
>>>
Reply
#5
(Nov-30-2017, 10:43 AM)Larz60+ Wrote:
>>> zz = [5, 78, 4, -1, 974, 22, 1053]
>>> min(zz)
-1
>>> max(zz)
1053
>>> sum(zz)/len(zz)
305.0
>>>

Thank you so much Larz60!
Now it is totaly correct
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get the biggest number from a two dimensional list rs74 13 3,941 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  Finding Row Number for Items in 2D array fafzal 2 2,406 Jan-10-2019, 06:11 AM
Last Post: fafzal
  Take the biggest value from array dwiga 9 5,897 Jul-20-2017, 01:28 AM
Last Post: dwiga

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020