我正在嘗試編寫一個程序,它會嘗試找出你輸入的5個數字,就像一些AI的東西。然而,當我跑我的代碼,我發現了以下錯誤:
TypeError: 'int' object is not subscriptable
TypeError:'int'object is not subcriptable AI程序測試
這裏是我的代碼—我究竟做錯了什麼?
__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()
仔細查看在錯誤信息 - 它會告訴你到底是誰在線的問題是。然後,您可以縮小問題範圍並進行一些調試。 – jme