Posts: 23
Threads: 4
Joined: Nov 2017
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(......)
Posts: 14
Threads: 2
Joined: Aug 2017
Nov302017, 02:11 AM
(This post was last modified: Nov302017, 02:11 AM by Prrz.)
(Nov302017, 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 builtin functions like max() and min()? Without using builtin 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 builtin 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
Posts: 23
Threads: 4
Joined: Nov 2017
(Nov302017, 02:11 AM)Prrz Wrote: (Nov302017, 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 builtin functions like max() and min()? Without using builtin 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 builtin 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
Posts: 10,719
Threads: 403
Joined: Sep 2016
>>> zz = [5, 78, 4, 1, 974, 22, 1053]
>>> min(zz)
1
>>> max(zz)
1053
>>> sum(zz)/len(zz)
305.0
>>>
Posts: 23
Threads: 4
Joined: Nov 2017
(Nov302017, 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
