dimanche 10 octobre 2021

Adding condition in list in python

I do not how to add extra conditions to this code.

(With only using randint)

For example with list [1, 2, 3, 4, 0] I need to generate random number expect the last one(covered in my code) but the next condition is, it can not choose a index number which value is 0. So for list [3, 3, 0, 3, 3, 7] it can only indexes 0,1,3,4(not 2 and 5 because I can not include the last number and index with value0).

My code so far:

import random

our =[ 2, 3, 4, 0]

random_index = random.randint(0,len(our)-2)

random_number = our[random_index]

print(random_number)

I will be very glad for any help.

Aucun commentaire:

Enregistrer un commentaire