Вопрос задан 17.06.2023 в 00:58. Предмет Информатика. Спрашивает Блок Вячеслав.

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

до 5, виведе на екран утворений список та суму парних чисел в даному списку. Зробіть на Питоні.
0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Бакулин Андрей.

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)

0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

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.

0 0

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

Последние заданные вопросы в категории Информатика

Задать вопрос