Python Forum
determine if an number is in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
determine if an number is in a list
#1
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"
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,186 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,848 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,276 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,017 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 3,082 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  Determine number of all the immediately adjacent points in python zackk 1 1,856 Feb-06-2021, 09:23 AM
Last Post: zackk
  Count number of occurrences of list items in list of tuples t4keheart 1 2,367 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 1,964 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 1,940 Jul-24-2020, 01:46 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020