0

So as part of a program i need a line where if the input is not in a certain array the code wont carry on.

The best way to explain it is with a game of cards. You cant play a card that isnt in your hand(array) What would this look like in code form?

eg. if cardplayed (is not in) (myhand):
print("ect ect")
C Plant
  • 1
  • 1
  • Get rid of the parentheses and also `is`. You also need to indent the `print` call – roganjosh Nov 24 '19 at 12:28
  • Possible duplicate of [Finding the index of an item given a list containing it in Python](https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python) – Lix Nov 24 '19 at 12:28
  • 1
    @Lix but they're not trying to find the index, they just want to check membership – roganjosh Nov 24 '19 at 12:29

3 Answers3

2

Simply use the not in operator:

if cardplayed not in myhand:
    print("etc etc")

The opposite would be

if cardplayed in myhand:
   print("I have this card, man")
Laszlo Treszkai
  • 332
  • 1
  • 12
1

Regardless of if myhand is a numpy array or not:

if cardplayed not in myhand:
    print("ect ect")
seralouk
  • 30,938
  • 9
  • 118
  • 133
1

Having a sequence, (a list in this example), one may check if an item is present within that sequence using the not in operator: https://docs.python.org/3.8/library/stdtypes.html#common-sequence-operations

hand = ['Card1', 'Card2', 'Card3']
card = 'Other card'
if card not in hand:
    print('Breaking...')
Icebreaker454
  • 1,031
  • 7
  • 12