2

I am new to python, so I apologise if the answer is obvious.

I am trying to pick 10 random numbers from a range of 128 numbers. And then if a certain number is present in this list, then print the (list) or a (word) or something.

I can pick the 10 random numbers and print these random numbers, but I am having trouble printing if a number is present in this random list.

I am picking and printing the 10 random numbers in the following way:

import random
list = random.sample(range(128), 10)
print list

I am trying to print (something), if a number in this case 5, is present in this random list in the following way:

import random
list = random.sample(range(128), 10)
if list == 5:
    print list
3
  • @Kos -- Yeah -- I was debating whether I should fix that syntax error or not ... Commented Apr 4, 2013 at 13:07
  • @mgilson I did since no initial whitespace at all was present; go ahead and rollback if you find my edit invalid Commented Apr 4, 2013 at 13:09
  • @Kos -- Nah, probably not worth it here. If it ends up being an issue OP'll let us know. Commented Apr 4, 2013 at 13:10

2 Answers 2

3

Use the in operator: if 5 in list

Also list is the name of the type, so I suggest to use another name for the variable.

Sign up to request clarification or add additional context in comments.

Comments

0

you can make it like this.

if 5 in list:
    print list

1 Comment

Thanks for your help, I tried this but I am getting an error that says: SyntaxError:invalid syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.