简体   繁体   中英

Use iterating variable to check value of dict key?

here is my code

tiles = ["  a  ", "  b  ", "  c  ", "  d  ", "  e  ", "  f  ", "  g  ", "  h  ", "  i  "]
check = {'0': False, '1': False, '2': False, '3': False, '4': False, '5': False,
         '6': False, '7': False, '8': False}

tile = input("Player 1: What tile?")

for index, z in enumerate(tiles):
    if int(tile) == index and ********:
        tiles[index] = str("xxxxx")

What I want to be able to do, is on line 8, check if tile is the same as index, and also check the value of the key(this part is the ********)

Basically I want to use the iterating variable index to request and check the value of the key that is the same as index . This might look like

if int(tile) == index and check[?index?] == False:

Please help and I am more than wiling to explain further as I am not very good at explaining these kind of things.

Try:

if int(tile) == index and not check[str(index)]:

There are chances that tile is already an integer (if you entered one). Keys of check are strings.

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