Random break,pass and continue statements Python

Viewed 71

I'm trying to randomly break and pass loop but random.choice([pass,break]) just won't work and shows invalid syntax. Any Suggestions..?

1 Answers

When you use pass and break as arguments in random.choice, python thinks they are variable names. But they're not.

res = random.choice(['pass','break'])
if res == 'pass':
  pass
if res == 'break':
  break
Related