13.3k views
3 votes
Create a list of list of 10 random numbers and insert an even number at all odd indexes and odd number at odd indexes and have 20 numbers in the end in the list.

This is a python quest. I expect your answer to be copy pasted from python

If you get it right, you'll get 50 points :)

1 Answer

2 votes

Answer:

Step-by-step explanation:

import random

# Create a list of 10 random numbers

random_list = [random.randint(1, 100) for i in range(10)]

print("Random List:", random_list)

# Create a new list to store the final result

result_list = []

# Loop through the random_list and insert even/odd numbers at odd indexes

for i, num in enumerate(random_list):

if i % 2 == 0:

result_list.append(num)

else:

if num % 2 == 0:

result_list.append(num + 1)

else:

result_list.append(num)

# Insert even numbers at all odd indexes

for i in range(1, len(result_list), 2):

if result_list[i] % 2 == 0:

result_list.insert(i, result_list[i] + 1)

print("Final List:", result_list)

User Lordofmax
by
8.1k points