使用迭代变量来检查dict键的值?

时间:2017-06-13 00:08:03

标签: python loops dictionary

这是我的代码

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")

我想要做的是,在第8行,检查tile是否与index相同,还检查键的值(这部分是********)

基本上我想使用迭代变量index来请求和检查与index相同的键的值。这可能看起来像

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

请帮助,我不仅仅是要进一步解释,因为我不太擅长解释这些事情。

1 个答案:

答案 0 :(得分:2)

尝试:

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

tile有可能已经是整数(如果您输入了整数)。 check的键是字符串。

相关问题