简体   繁体   English

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

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

I'm new to coding. 我是编码的新手。 I learned the basics of python and now I started my first project.I'm trying to code Tic Tac Toe. 我学习了python的基础知识,现在我开始了我的第一个项目。我正在尝试编写Tic Tac Toe。 I made the bacic design and now I'm work on the real code. 我做了bacic设计,现在我正在研究真正的代码。 I'm trying to realise this with an coordinate system.I'm asking the user for the position where he wants to go. 我试图通过坐标系来实现这一点。我要求用户找到他想去的位置。 This information I put into a list. 我将这些信息列入清单。 Then I use this information to change one of the numbers this represents the x or o in Tic Tac Toe but when it should change the number it gives me an Error the:"TypeError: list indices must be integers or slices, not str" 然后我使用这些信息来改变其中一个数字,它代表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()

That is the output:(It's ok I think) 这是输出:(我认为没关系)

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

(this isn't displayed properly) (这没有正确显示)

This fixed it. 这解决了它。


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.

相关问题 TypeError:列表索引必须是整数或切片,而不是 str - TypeError: List indices must be integers or slices and not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str “TypeError:列表索引必须是整数或切片,而不是 str” - "TypeError: list indices must be integers or slices, not str" TypeError:列表索引必须是整数或切片而不是 str - TypeError: list indices must be integers or slices not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str Python3-TypeError:列表索引必须是整数或切片,而不是str-List - Python3 - TypeError: list indices must be integers or slices, not str - List 使用 Python TypeError 解析 JSON:列表索引必须是整数或切片,而不是 str - Parsing JSON with Python TypeError: list indices must be integers or slices, not str 如何解决“类型错误:列表索引必须是整数或切片,而不是 str” - How to solve "TypeError: list indices must be integers or slices, not str" Scrapy TypeError:列表索引必须是整数或切片,而不是 str - Scrapy TypeError: list indices must be integers or slices, not str 遍历字典:类型错误:列表索引必须是整数或切片,而不是 str - Iterating through a dictionary: TypeError: list indices must be integers or slices, not str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM