python - (Python) 当我在类定义之后调用方法对象时,为什么它不执行?

标签 python class function object methods

当我尝试在 IDLE 中运行我的程序时(我目前使用的文本编辑器,notepad++ 说缩进错误,我不知道为什么),它只执行 __init__ 中的代码,这表明它已被创建为一个对象。但之后的行我尝试使用 main 方法,但它没有执行任何操作。我也将其更改为其他方法,但也不起作用。这是我的程序:

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 自己的教程。

最佳答案

调用 Starter 函数,例如

s.Starter()

而且还可能存在逻辑错误下面的 while 循环将始终为 true。

def Starter(self):
        while True:
            try:
                i = int(input(''))
            except TypeError:
                print 'Not a Number, Try Again.'
                continue

关于python - (Python) 当我在类定义之后调用方法对象时,为什么它不执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10967814/

相关文章:

javascript - JavaScript 中函数内函数 vs 函数返回函数

python - jinja2.异常.UndefinedError : 'form' is undefined even after passing 'form' in render_template

python - Tensorflow神经网络损失没有减少

android - 如何将字符串转换为类名

c++ - 模板特化的成员函数类型

r - 在 R 函数中修改变量

python - 无法在我的 mac 上为 Python2.7 安装 YAML

python - 检查奇数时 & 比 % 快吗?

python - 如何在python中获取某个类的所有实例?

c++ - 为什么在使用带有接口(interface)指针的多态行为时没有调用析构函数?