Write a program that fills an array of 10 elements with random numbers from 1 to 10, and then swaps the first element with the second, the third with the fourth, and so on. Display the original and transformed array
Here is my solution, but Python doesn't want to sort the array and it stays the same:
from random import randint
numbers = []
for i in range(10):
numbers.append(randint(1, 10))
print(numbers)
a = 0
for a in range(10):
numbers[-1], numbers[i] = numbers[i], numbers[-1]
a = a + 2
print(numbers)
I have tried replacing elements with a loop by numbers[a] = numbers[a+1]
, But I kept getting the error:
IndexError: list index out of range