Python 类型错误:';int';对象不是可下标的AI程序测试

Python 类型错误:';int';对象不是可下标的AI程序测试,python,artificial-intelligence,Python,Artificial Intelligence,我正在写一个程序,它将试图找出你输入的5个数字,就像人工智能一样。但是,当我运行代码时,会出现以下错误: TypeError:“int”对象不可下标 这是我的代码-我做错了什么 __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

我正在写一个程序,它将试图找出你输入的5个数字,就像人工智能一样。但是,当我运行代码时,会出现以下错误:
TypeError:“int”对象不可下标

这是我的代码-我做错了什么

__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

仔细查看错误消息——它将准确地告诉您问题所在的线路。然后你可以缩小问题的范围并进行一些调试。给某人一个家庭作业问题的替代实现,而不描述他们做错了什么,或者他们如何避免再次犯同样的错误?这似乎正是分发鱼与传授技能的缩影。@CharlesDuffy我认为代码是不言自明的。但我明白你的意思。这确实是一个易于阅读和遵循的代码;我不怀疑这是什么不言自明。然而,它如何回答OP的问题并不明显,除了提供一个根本不会提出问题的家庭作业答案。