Z
Z
zhabaa2021-10-20 22:51:01
Python
zhabaa, 2021-10-20 22:51:01

Can the code be shortened?

I am just starting to learn how to code. Tell me how you can shorten this code and make it more aesthetically pleasing. (I understand that this is all subjective)

from os import system
system("cls")

n = 10
a = []
sum = 0
count = 0


for i in range(n):
    a.append(int(input("Введите число >> ")))

for s in range(len(a)):
    if a[s] > 9 and a[s] < 99:
        sum += a[s]
        count += 1
        
sr = sum/10
if count > 0:
    print("Среднее значение 2-х - ", sr)
else:
    print("Нет")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2021-10-20
@zhabaa

n = 10
a = []

for __ in range(n):
    a.append(int(input("Введите число >> ")))

out = list(filter(lambda x:x>9 and x<99, a))
try:
    print(f"Среднее значение 2-х - {sum(out)/len(out)}")
except ZeroDivisionError:
    print('Нет')

R
Ronald McDonald, 2021-10-20
@Zoominger

It would be possible not to enter numbers in a separate loop, but to enter in the same one where you are calculating the sum of numbers, that is, not to store these numbers in an array.
Well, the purpose of the count variable is not entirely clear.
And so everything is normal.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question