Python中文
首页
教程
问答
标签
搜索
登录
注册
一个带有_str________________________________
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我必须用python编写一个Tic-tac-toe程序,我使用Jupyter笔记本。我想用<code>__str__</code>方法编写一个类。我首先在类Terrain中尝试了一个名为<code>afficher</code>的方法。它似乎起作用了。但是当我尝试使用<code>__str__</code>方法时,它不起作用</p> <pre class="lang-py prettyprint-override"><code> 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' </code></pre> <p>***需要用<code>__str__</code>方法替换<code>affiche</code>方法***</p> <pre class="lang-py prettyprint-override"><code> 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 </code></pre> <p>**这是我需要的输出,但是使用Terrain类中的<code>__str__</code>方法**</p> <pre class="lang-py prettyprint-override"><code> terrain = Terrain() terrain terrain.jouer(3) terrain.jouer(2) terrain.jouer(4) terrain.jouer(6) terrain.jouer(5) terrain.afficher() </code></pre> <p>***这就是我在Terrain类中替换afficher方法的方式(它不起作用…我不知道为什么…)</p> <pre class="lang-py prettyprint-override"><code> 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' </code></pre> <pre class="lang-py prettyprint-override"><code> 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 </code></pre> <pre class="lang-py prettyprint-override"><code> terrain = Terrain() terrain terrain.jouer(3) terrain.jouer(2) terrain.jouer(4) terrain.jouer(6) terrain.jouer(5) print(terrain) </code></pre> <p>对不起我的英语</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>非常感谢你的邀请。 我试过这个,它很管用</p> <p>" def<strong>str</strong>(自我): 内容=[]<br/> 对于范围(9)内的i: 如果(i+1)%3!=0: content.append(self.grill[i].occupe+'|') 其他: content.append(self.grill[i].occupe+'\n') 返回“”。加入(内容)</p> <p>"</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何合并多个PDF文件?
10 回答
如何合并多个xarray数据变量及其坐标?
10 回答
如何合并多个列中具有重复值的行
8 回答
如何合并多个唯一id
2 回答
如何合并多个图纸并使用图纸名称的名称重命名列名?
4 回答
如何合并多个字典并添加同一个键的值?(Python)
3 回答
如何合并多个搜索结果文件(pkl)以将它们全部打印在一起?
3 回答
如何合并多个数据帧
3 回答
如何合并多个数据帧并使用Pandas为假人添加列?
4 回答
如何合并多个数据帧并按时间戳排序
4 回答
如何合并多个数据帧的列表并用另一个lis标记每列
6 回答
如何合并多个数据框中的列
6 回答
如何合并多个文件?
6 回答
如何合并多个查询集?
5 回答
如何合并多个绘图?
7 回答
如何合并多个词典
2 回答
如何合并多个输入数据集(数据帧)?
7 回答
如何合并多条记录中拆分的文本行
5 回答
如何合并多索引列datafram
3 回答
如何合并多级(即多索引)数据帧?
4 回答