繁体   English   中英

“TypeError:list indices必须是整数或切片,而不是str”

[英]“TypeError: list indices must be integers or slices, not str”

我是编码的新手。 我学习了python的基础知识,现在我开始了我的第一个项目。我正在尝试编写Tic Tac Toe。 我做了bacic设计,现在我正在研究真正的代码。 我试图通过坐标系来实现这一点。我要求用户找到他想去的位置。 我将这些信息列入清单。 然后我使用这些信息来改变其中一个数字,它代表Tic Tac Toe中的x或o但是当它应该改变数字时它会给我一个错误:“TypeError:list indices必须是整数或切片,而不是str”

game = [["a b c"],
   [0, 0, 0],
   [0, 0, 0],
   [0, 0, 0],]

a = 0
b = 1
c = 2

def show_display():

    global count

    count = 0
    for row in (game):
        print (count, row)
        count += 1

    print (" ")

show_display()

x = input("Spieler 1,bitte geben sie ihren nächsten Zuge ein."
          "Achten sie auf eine korrekte Schreibweise! Beispiel:[a1]     Ihre Eingabe:")

print(x[1])
game[(x[2])][(x[1])] = 1 <-here is the problem

show_display()

这是输出:(我认为没关系)

0 ['a b c']
1 [0, 0, 0]
2 [0, 0, 0]
3 [0, 0, 0]

Spieler 1,bitte geben sie ihren nächsten Zuge ein.Achten sie auf eine korrekte Schreibweise! Beispiel:[a1] Ihre Eingabe: [a1]
a

(这没有正确显示)

这解决了它。


game = [["a b c"],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
       ]

def show_display():

    global count

    count = 0
    for row in (game):
        print (count, row)
        count += 1

    print (" ")

def main():

    a = 0
    b = 1
    c = 2
    show_display()

    x = input("Spieler 1,bitte geben sie ihren nächsten Zuge ein."
              "Achten sie auf eine korrekte Schreibweise! Beispiel:[a1]     Ihre Eingabe:")

    if(x[0] == "a"):
        game[int(x[1])][0] = 1
    elif(x[0] == "b"):
        game[int(x[1])][1] = 1
    elif(x[0] == "c"):
        game[int(x[1])][2] = 1

    show_display()

if __name__ == '__main__':
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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