0

I don't know how to add extra conditions to this code. (Only using randint)

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

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.

4
  • does the index need to be exactly of what in the original list, or do you just care about getting a random numder that is not 0? Commented Oct 10, 2021 at 15:38
  • Do you need the index specifically, or do you only need the value at that index? Commented Oct 10, 2021 at 15:39
  • One thing you could do would be to leave the code like you have it but then add in a check if random_number is 0, and if so, generate a new index and check again. Commented Oct 10, 2021 at 15:40
  • @luther I only need the value of index using randind and with the requirements. Commented Oct 10, 2021 at 15:56

2 Answers 2

4

You can create a second list that stores the valid index values.

import random

our = [3, 3, 0, 3, 3, 7]

index = []

for i in range(0, len(our)-1) :
    if our[i] != 0 :
        index.append(i)

# index: [0, 1, 3, 4]

random_index = random.choice(index)

EDIT: You can perform a sanity check for a non-zero value being present.

The any() function returns True if any element of an iterable is True. 0 is treated as False and all non-zero numbers are True.

valid_index = any(our[:-1])

if valid_index:
    index = []
    for i in range(0, len(our)-1) :
        if our[i] != 0 :
            index.append(i)
Sign up to request clarification or add additional context in comments.

5 Comments

It's range(0, len(our)-1) because the requirement is that the last element of the list is not to be included.
Oh, my bad, I have to print the options, so with list the [3, 3, 0, 3, 3, 7] I have to print the options which can be used, so for this list, it can be one of those numbers (the most left number starts with 0) so 0,1,3,4. But if those numbers for example [0,0,0,1] are all 0, it returns none, can you help me with this please?
What do you want to happen in the scenario where you are left with no options?
I mean can even that scenario happen? The list will always be atleast two numbers long, so if the list is [0,1] it will return none, if for example is [1,0,1] it will always return "0"(it represent the position of 1 becasue only that number follows from requirement from this task.
I've added a conditional to perform a sanity check for a valid index.
0

You can use a while loop to check if the number equals 0 or not.

import random


our = [3, 6, 2, 0, 3, 0, 5]

random_number = 0
while random_number == 0:
    random_index = random.randint(0, len(our)-2)
    random_number = our[random_index]

print(random_number)

4 Comments

Oooo, so I am just checking the numbers and if the index's value is 0 I just try another check until it is not 0 right?
yes, exactly :)
Oh, my bad, I have to print the options, so with list[3, 6, 2, 0, 3, 0, 5] I have to print the options which can be use, so for this link it can be one of those numbers (the most left number starts with 0) so 0,1,2,4. Can you help me with this?
the answer above mine solevs that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.