简体   繁体   中英

counting a number of spaces in a list python

I am making a text based game, and you can place a ship piece off the game board (not a intended feature). I have been searching the web for a way to count 2 characters into a list and see if it is a certain character (so i can tell if someone placed off the board), but so far i have not found any solution. Anyone have any ideas?

It would be more helpful to have an example of what your list looks like, but here's one possible solution.

Assuming you have a list of X's and O's like so:

example_list = ['X', 'O', 'O', 'X']

Now:

# Check if the second character is 'O'
if example_list[1] == 'O':
    # Do something
else:
    # Do something else

Note that this would work the same way with the string "XOOX" , ie

>>> example_string = "ABCD"
>>> print example_string[1]
B
>>> example_string[1] == "B"
True
>>> example_string[1] == "X"
False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM