TypeError: the 'int' object is not writable AI PROGRAM TEST

advertisements

I'm trying to write a program, that will try and find out what 5 numbers you entered like some AI thing. However, when I run my code I'm getting the following error:
TypeError: 'int' object is not subscriptable

Here is my code — what am I doing wrong?

__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()


You can have a similar functionality with,

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])

Output:

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