当我尝试在IDLE中运行我的程序(我现在使用的文本编辑器,记事本++表示缩进错误,我不知道为什么),它只会执行__init__
中的代码,该代码显示它已创建变成一个物体。但之后的行我尝试使用主要的方法,并没有做任何事情。我也改变了一种不同的方法,但也没有奏效。这里是我的程序:(Python)当我在我的类定义之后调用它时,为什么我的方法对象不会执行?
import sys
class Main:
def __init__(self):
WinX = False
WinO = False
Turn = 'X'
LastTurn = 'X'
a, b, c, d, e, f, g, h, i = ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
PosList = []
PosList.append(a)
PosList.append(b)
PosList.append(c)
PosList.append(d)
PosList.append(e)
PosList.append(f)
PosList.append(g)
PosList.append(h)
PosList.append(i)
self.board = '+---+---+---+\n| ' + PosList[0] +' | '+ PosList[1] +' | '+ PosList[2] +' |\n+---+---+---+\n| '+ PosList[3] +' | '+ PosList[4] +' | '+ PosList[5] +' |\n+---+---+---+\n| '+ PosList[6] +' | '+ PosList[7] +' | '+ PosList[8] +' |\n+---+---+---+'
print self.board
def UpdateTurn(self):
if LastTurn == 'X':
Turn == 'O'
elif LastTurn == 'O':
Turn == 'X'
LastTurn = Turn
def WinChecker(self):
if a and b and c == 'X' or a and d and g == 'X' or a and e and i == 'X' or g and e and c == 'X' or g and h and i == 'X' or c and f and i == 'X':
WinX = True
if a and b and c == 'O' or a and d and g == 'O' or a and e and i == 'O' or g and e and c == 'O' or g and h and i == 'O' or c and f and i == 'O':
WinO = True
def UpdateBoard(self):
print self.board
def Starter(self):
while True:
try:
i = int(input(''))
except TypeError:
print 'Not a Number, Try Again.'
continue
i -= 1
PosList[i] = Turn
self.UpdateBoard
self.WinChecker
if Winx == True:
print 'X Wins!'
sys.exit()
elif Wino == True:
print 'O Wins!'
sys.exit()
self.UpdateTurn
s = Main()
s.Starter
我只是(4天)完成了python自己的教程。
Python中的方法用括号调用。使用s.Starter()来执行该方法。 – Tisho
这里还有另一个问题。你永远不会跳出输入循环。 – Keith
直到他们给出了有效的答案之前,我从来没有跳出过输入循环。那么,那就是我反正想的。好吧,只是尝试过,你是对的,需要解决这个问题......其实我只是忘了缩进函数的其余部分。我需要在下一次注意。尽管感谢您的回答,但它帮助我通过代码来查找。 – Rees