python3 How to select two elements on either side of a random element in a list?

Question!

I have finished this part of the code so far:

wedding = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
from random import randrange
random_index = randrange(0, len(wedding))
print('TV =', wedding[random_index])

I got stuck at a step that I need to find two elements on either side of the element that I have randomly selected.



Answers

When using arrays, always check for your array bounds in your code. The below code will output the value from the array that is to the left and right of the randomly selected index value. If either of the indexes are out of bounds, it will not output that value.

from random import randrange 

wedding = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
random_index = randrange(0, len(wedding)) 

print('TV = ', wedding[random_index])

if random_index-1 >= 0:
    print('Left of Index = ', wedding[random_index-1])
if random_index + 1 < len(wedding) - 1:
    print('Right of Index = ', wedding[random_index + 1])
By : PrestonM


try something like this.

You need the variable accessible in both functions so it needs to be declared outside those functions and then defined inside the first one and use clearInterval() to cancel an existing interval

var timer;
$('#name').mouseenter(function() {
   timer = setInterval(mosaic, 150);
}).mouseleave(function() {
    clearInterval(timer);
});


This video can help you solving your question :)
By: admin