python - How to shorten a list of multiple 'or' operators that go through all elements in a list -
edit: question isn't duplicate i'm asking how shorten task written out in full , question said duplicate mine asks why 'a == b or c or d' evaluates true know answer of , isn't i'm stuck with.
i started making function have no idea shorten 'or' task first of doesn't , second if list 100 elements long ridiculous typing moves[n] way moves[n+99].
any suggestions of how shorten can't think of side note need integers know have done
if playermove == '1 2 3 4 5 6 7 8 9'.split():
however playermove integer , numbers strings.
def getplayermove(playermove): while true: n=0 moves = [1,2,3,4,5,6,7,8,9] if playermove == moves[n] or moves[n+1] or moves[n+2] or moves[+3] or moves[n+4] or moves[n+5] or moves[n+6] or moves[n+7] or moves[n+8] or moves[n+9]: return playermove else: playermove = int(input("enter number 1-9"))
you want check membership use in
operator. more pythonic way use set preserving moves since complexity of membership checking approximately o(1):
moves = {1,2,3,4,5,6,7,8,9} if playermove in moves: #
Comments
Post a Comment