-2

This is what code I currently have (But in simplified form):

Example = [0, 0, 0, 0, 1]
Test = random.randint(0,len(Exmaple))
if Example[Test] == 1:
    print("Working")
else:
    print("Not Working")

I want it to print "Working" by detecting if it lands on the integer 1 I could do Example[4] but I want to change the amount of items there are in the list, so it would work if I changed Example to [0, 0, 0, 0, 0, 1]

  • Instead of choosing a random index, you can choose a random element directly: `if random.choice(Example) == Test` – DeepSpace Apr 25 '22 at 11:50

1 Answers1

0

by changing if Example[Test] == 1: to random.choice(Example) == 1: it allows the code to print "Working" if it detects the interger 1

DeepSpace answered this in an comment:

Instead of choosing a random index, you can choose a random element directly: if random.choice(Example) == Test

Nimantha
  • 6,405
  • 6
  • 28
  • 69