'How do I compare a list with an interger? [duplicate]
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]
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nimantha |
