Python Forum

Full Version: determine if an number is in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi, am new at python trying to learn
is there away for me to know if its in the list?

import random

def  rollBall():

    system = [0,9,12,20,24,25,35]
    
    roll = random.randint(0,37)
    if roll = system[0,1,2,3,4,5,6]:
    return True
tried this and seems "invalid syntax"
Python makes this easy:

import random
 
def  rollBall():
    roll = random.randint(0,37)
    if roll in [0,1,2,3,4,5,6]:
        return True
There are other problems with your original code which may be causing the error. I can't be certain which because you haven't shared the traceback. Here are the other problems I see:
  • Line 8: equality comparison uses "=="; "=" is used for assignment
  • Line 8: Because system is a list, the interpreter is attempting to slice it with the information between the square brackets. However, it cannot because that is the wrong format. I'm not sure what you were attempting to do here.