TypeError: 'int' object is not subscriptable AI PROGRAM TEST

TypeError: 'int' object is not subscriptable AI PROGRAM TEST

我正在尝试编写一个程序,它将尝试找出您输入的 5 个数字,就像一些 AI 东西一样。但是,当我 运行 我的代码出现以下错误时:
TypeError: 'int' object is not subscriptable

这是我的代码——我做错了什么?

__author__ = 'Vansh'
import random

def get_num_code():
    x1 = int(input("Enter 5 digit number code one by one:\n"))
    x2 = int(input(""))
    x3 = int(input(""))
    x4 = int(input(""))
    x5 = int(input(""))
    x = [x1, x2, x3, x4, x5]
    return x

def ai0(x):
    y = random.randrange(1, 10)
    if x[0] == y:
        print("digit 1 found: {}".format(str(x[0])))
        ai1(x)
    else:
        print("Digit 1 not found")
        ai0(x)

def ai1(x):
    y = random.randrange(-1, 10)
    if x[1] == y:
        print("digit 2 found: {}".format(str(x[1])))
        ai2(x)
    else:
        print("Digit 2 not found")
        ai1(x)

def ai2(x):
    y = random.randrange(-1, 10)
    if x[2] == y:
        print("digit 3 found: {}".format(str(x[2])))
        ai3(x)
    else:
        print("Digit 3 not found")
        ai2(x)

def ai3(x):
    y = random.randrange(-1, 10)
    if x[3] == y:
        print("digit 4 found: {}".format(str(x[3])))
        ai4(x)
    else:
        print("Digit 4 not found")
        ai3(x)

def ai4(x):
    y = random.randrange(-1, 10)
    if x[4] == y:
        print("digit 5 found: {}".format(str(x[4])))
        final(x)
    else:
        print("Digit 5 not found")
        ai3(4)

def final(x):
    print("5 digit code FOUND: {}".format(str(x)))

def ai():
    x = get_num_code()
    ai0(x)

ai()

在您的 ai4 函数中,您传递了一个 int 作为参数,但它需要一个列表。

ai3(4)

应该是一个列表 - 其他函数这样称呼它

ai3(x)

您可以使用类似的功能,

import random

x = input("Enter a number: ")
for i in range(len(x)):
   randomGuess = random.randrange(1,10)
   while randomGuess != int(x[i]):
      print('Digit',i,'not found')
      randomGuess = random.randrange(1,10)
   print('Digit',i,'found:',x[i])

输出:

Enter a number: 123
Digit 0 not found
Digit 0 not found
Digit 0 found: 1
Digit 1 not found
Digit 1 not found
Digit 1 found: 2
Digit 2 found: 3