-2
import random 

num_list = []
for i in range(0, 100 + 1):
    num_list.append(i)

for i in range(50):
    output = random.sample(num_list, 3)
    print(output)
    if 0 or 100 in output:
        print('yes')

I am trying to print a list of 3 numbers in random (0 to 100) for 50 times. When 0 or 100 is on the list, I want to print 'yes' in the console. However, when I run the code, only when the list has 100 shows 'yes', but not in the situations when 0 is on the list. What causes the problem? Did I use the 'or' operator mistakenly?

234ff
  • 99
  • 3

2 Answers2

0

if 0 or 100 in output: is parsed as

if
  (0)
  or
  (100 in output)

and 0 is always falsy.

You'll want if 0 in output or 100 in output, i.e.

if
  (0 in output)
  or
  (100 in output)
AKX
  • 152,115
  • 15
  • 115
  • 172
0

The statement

if 0 or 100 in output:

is interpreted as

if (0) or (100 in output):

So the condition is true only if 100 is present in output. To fix this you need

if 0 in output or 100 in output:
CaptainDaVinci
  • 975
  • 7
  • 23