Python array [HELP]

Asked

Viewed 149 times

-2

Description: Make a program that displays the average between the highest and lowest value of an integer vector with twenty user-given elements and, at the end, displays only those elements that are greater than average.

Input format: Twenty whole numbers, one per line.

Output format: In the first line, the phrase "media: #" (without quotation marks, in lower case, without accentuation and with # replaced by the average of the numbers read and with two decimal places); in the following lines, the values given in the input are greater than the average, in the same sequence as read and one per line

Code that is being used:

arr = []
soma = 0
for p in range(20):
    x= int(input())
    arr.append(x)
    soma = soma + x

media = soma // len(arr)

if media < 0:
    print('media: 0.00')
else:
    print('media: %.2f' % media)

for b in range(20):
    if arr[b] > media:
        if arr[b] != 0:
            print(arr[b])

When I submit, the first two test cases of the program, it works, however, the last two test cases, it does not work, and the program does not hint what is occurring, I think there is some wrong part, or some gambiarra that I have done...

Entrada:
5
1
7
-6
-9
-6
0
-2
10
-6
-8
3
-4
-9
9
8
-7
3
-7
-10

Saída

media: 0.00
5
1
7
10
3
9
8
3
  • And what you need help with?

  • our code is not working!

  • arr = [] soma= 0 for _ in range(20): x = int(input()) arr.append(x) soma = sum + x media = sum // Len(arr) if media <= 0: print("media: 0.00") Else: print("media: %.2f" media) for b in range(20): if arr[b] > media: if arr[b] != 0: print(arr[b])

  • Edit the question, add the code and explain what has been done and what is currently happening.

  • editions are made!

  • 1) Ask for the average between the highest and lowest value of the list, not the average of all; 2) Why is 0 displayed when the calculated mean is negative? Not in the statement specifying this; 3) Why use // to calculate the average? It shouldn’t be a real number? 4) Why, when displaying above-average values, is it checked if the value is different from 0? If it is zero it should not be displayed even if it may be above average? It also does not say in the statement on this.

  • I will edit the entries and outputs that the program also

  • What about the four questions in the previous comment?

Show 3 more comments

1 answer

0


From what I understood from the description of the problem, the mean must be made between the maximum value and the minimum value of the vector. In your program you try to average all vector values. I believe the program below meets your description:

vec = list()

for i in range(20):
    vec.append(int(input()))

media = (max(vec) + min(vec)) / 2

print("media: %.2f" % media)

for i in range(20):
    if vec[i] > media:
        print(vec[i])
  • MANOOOOOOOO VALEU!!!!!!

  • serio was worth a lot]

  • @Baria I’m glad I could help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.