簡體   English   中英

使用迭代變量檢查dict鍵的值?

[英]Use iterating variable to check value of dict key?

這是我的代碼

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相同,並檢查key的值(這部分是********)

基本上,我想使用迭代變量index來請求和檢查與index相同的鍵的值。 這可能看起來像

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

請幫助,我不願意進一步解釋,因為我不太擅長解釋這類事情。

嘗試:

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

tile有可能已經是整數(如果輸入了整數)。 check是字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM