一个带有_str________________________________

2024-06-25 06:06:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须用python编写一个Tic-tac-toe程序,我使用Jupyter笔记本。我想用__str__方法编写一个类。我首先在类Terrain中尝试了一个名为afficher的方法。它似乎起作用了。但是当我尝试使用__str__方法时,它不起作用

    class Case:
        def __init__(self, a = ' '):
            self.occupe = a
        
        def jouer1(self):
            if self.occupe == ' ':
                self.occupe = 'X'
            
        def jouer2(self):
            if self.occupe == ' ':
                self.occupe = 'O' 

***需要用__str__方法替换affiche方法***

    class Terrain:
        def __init__(self):
            self.grille = []
            for i in range(0, 9):
                self.grille.append(Case())
            
        self.tour = 1
        
    
        def afficher(self):        
            for i in range(9):
                if (i + 1)%3 != 0:
                    print(self.grille[i].occupe + ' | ', end =" ")
                else:
                    print(self.grille[i].occupe)

                
        def jouer(self, k):
            if self.tour == 1:
                self.grille[k].jouer1()          
                self.tour = 2
            else:
                self.grille[k].jouer2()
                self.tour = 1 

**这是我需要的输出,但是使用Terrain类中的__str__方法**

    terrain = Terrain()
    terrain

    terrain.jouer(3)
    terrain.jouer(2)
    terrain.jouer(4)
    terrain.jouer(6)
    terrain.jouer(5)

    terrain.afficher() 

***这就是我在Terrain类中替换afficher方法的方式(它不起作用…我不知道为什么…)

    class Case:
        def __init__(self, a = ' '):
            self.occupe = a
        
        def jouer1(self):
            if self.occupe == ' ':
                self.occupe = 'X'
            
        def jouer2(self):
            if self.occupe == ' ':
                self.occupe = 'O'
    class Terrain:
        def __init__(self):
            self.grille = []
            for i in range(0, 9):
                self.grille.append(Case())
            
        self.tour = 1
        
    
        def __str__(self):        
            for i in range(9):
                if (i + 1)%3 != 0:
                    return self.grille[i].occupe + ' | '
                else:
                    return self.grille[i].occupe + ' \ '

                
        def jouer(self, k):
            if self.tour == 1:
                self.grille[k].jouer1()          
                self.tour = 2
            else:
                self.grille[k].jouer2()
                self.tour = 1 
    terrain = Terrain()
    terrain

    terrain.jouer(3)
    terrain.jouer(2)
    terrain.jouer(4)
    terrain.jouer(6)
    terrain.jouer(5)

    print(terrain) 

对不起我的英语


Tags: 方法selfifinitdefclasscasestr
2条回答

非常感谢你的邀请。 我试过这个,它很管用

" defstr(自我): 内容=[]
对于范围(9)内的i: 如果(i+1)%3!=0: content.append(self.grill[i].occupe+'|') 其他: content.append(self.grill[i].occupe+'\n') 返回“”。加入(内容)

"

请尝试以下方法为整个范围而不是仅为一行生成字符串

def __str__(self):
    content = []        
    for i in range(9):
        if (i + 1)%3 != 0:
            content.append(self.grille[i].occupe + ' | ')
        else:
            content.append(self.grille[i].occupe)
    return '\n'.join(content)

相关问题 更多 >