我想在python中创建一个简单的tic tac toe游戏 . 这是我的代码到目前为止,但当我运行它,它要求一个点选择我输入一个数字,它显示板但然后它返回此错误:第32行,在输入= int(输入(“选择一个点:“))TypeError:'int'对象不可调用
请帮助我解决这个问题,因为用户必须选择一个号码 . 我正在使用python 3 .
import random
board = [0,1,2,3,4,5,6,7,8]
def show():
print (board[0]," | ", board[1]," | ", board[2])
print ("----------------")
print (board[3]," | ", board[4]," | ", board[5])
print ("----------------")
print (board[6]," | ", board[7]," | ", board[8])
def checkLine(char, spot1, spot2, spot3):
if board[spot1] == char and board[spot2] == char and board[spot3] == char:
return True
def checkAll(char):
if checkLine(char, 0, 1, 2):
True
if checkLine(char, 1, 4, 7):
True
if checkLine(char, 2, 5, 8):
True
if checkLine(char, 6, 7, 8):
True
if checkLine(char, 3, 4, 5):
True
if checkLine(char, 2, 4, 6):
True
if checkLine(char, 0, 4, 6):
True
while True:
input = int(input("Select a spot: "))
if board[input] != 'x' and board[input] != 'o':
board[input] = 'x'
if checkAll('x') == True:
print("-- X WINS --")
break;
random.seed()
opponent = random.randint(0,8)
while True:
if board[opponent] != 'o' and board[opponent] != 'x':
board[opponent] = 'o'
if checkAll('o') == True:
print("-- O WINS --")
break;
break;
else:
print ('This spot is taken')
show()
2 回答
您为
input
分配一个整数:不要那样做 . 将名称
input
用于其他对象后,内置函数input
将不再可用 . 所以在第二个循环中,input
不是以前的函数,而是整数 .使用其他名称:
问题是,第一次运行此行时,您将整数值分配给
input
,第二次调用input("")
时会因为调用整数而出错!通过更改变量名称可以解决问题 .要解决它,您只需更改变量名称: