
Дано число n. Створити програму яка створить список з n цілих чисел з випадковими значеннями від 1
до 5, виведе на екран утворений список та суму парних чисел в даному списку. Зробіть на Питоні.

Ответы на вопрос

Python
from random import randint
n=int(input())
a=[]*n
a=[randint(1,5) for i in range(n)]
s=0
for i in a:
if (i%2) == 0:
s+=i
print(a)
print('Sum=',s)



Creating a Program to Generate a List of Random Numbers and Calculate the Sum of Even Numbers in Python
To create a program in Python that generates a list of n integers with random values from 1 to 5 and then displays the generated list along with the sum of the even numbers in the list, you can follow the steps below.
1. Import Required Modules: - First, import the necessary modules for generating random numbers and performing the required calculations.
```python import random ```
2. Generate the List and Calculate the Sum of Even Numbers: - Use a loop to generate n random integers and calculate the sum of the even numbers in the list.
```python n = 10 # Replace 10 with the desired value of n random_list = [random.randint(1, 5) for _ in range(n)] even_sum = sum(num for num in random_list if num % 2 == 0) ```
3. Display the Generated List and the Sum of Even Numbers: - Finally, display the generated list and the sum of the even numbers.
```python print("Generated List:", random_list) print("Sum of Even Numbers:", even_sum) ```
By running the above code, you will create a program that generates a list of n integers with random values from 1 to 5, displays the generated list, and calculates the sum of the even numbers in the list.
Feel free to adjust the value of `n` to generate a list of a different length.


Топ вопросов за вчера в категории Информатика








Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili