简体   繁体   English

python中的类、属性和方法

[英]Class , attribute and method in python

Hi i am new to python and doing this exercise online i am stuck.Here is the exercise and what I have already done someone can help me.嗨,我是 python 新手,在网上做这个练习我被卡住了。这是练习,我已经做了什么,有人可以帮助我。

Define a Case class that contains a single occupy attribute.定义一个包含单个占用属性的 Case 类。

This attribute will automatically take the value ' ' during instantiation.此属性将在实例化期间自动采用值“”。

Define in the Case class a play1 method which will give the value 'X' to the attribute occupied if the case is not occupied.在 Case 类中定义一个 play1 方法,如果 case 未被占用,该方法将为占用的属性赋予值“X”。

Define in the Case class a play2 method which will give the value 'O' to the attribute occupied if the case is not occupied.在 Case 类中定义一个 play2 方法,如果 case 没有被占用,该方法将为占用的属性赋予值 'O'。

Here is the first part of my code :这是我的代码的第一部分:

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

Define a Terrain class that has two attributes: grid and tower.定义一个具有两个属性的地形类:网格和塔。 The grid attribute is a 9-element list of type Case. grid 属性是一个 Case 类型的 9 元素列表。

The turn attribute is an integer that equals 1 if it's player 1's turn to play and 2 if it's player 2's turn. turn 属性是一个整数,如果轮到玩家 1 玩,则等于 1,如果轮到玩家 2,则等于 2。

The lap attribute will be automatically initialized with the value 1. Define in the Terrain class the method str which will allow you to use the print function on objects of this class. lap 属性将使用值 1 自动初始化。在 Terrain 类中定义str方法,该方法将允许您对此类的对象使用 print 函数。

The print function should display in a first line the content of boxes 0 to 2, then in a second line the content of boxes 3 to 5 and finally in a third line the content of boxes 6 to 8.打印功能应该在第一行显示框 0 到 2 的内容,然后在第二行显示框 3 到 5 的内容,最后在第三行显示框 6 到 8 的内容。

The boxes will be separated by the character '|'这些框将由字符“|”分隔and each line will end with the \\ n character that matches the end of line character.并且每一行都将以与行尾字符匹配的 \\n 字符结尾。

Define in the class Terrain a method play which will take as parameter an integer ranging from 0 to 8. Depending on the player whose turn it is to play, this method will call the methods play1 or play2 of the cell corresponding to the integer passed in parameter.在类 Terrain 中定义一个方法 play ,它的参数是一个 0 到 8 的整数。根据轮到的玩家,该方法将调用与传入的整数对应的单元格的方法 play1 或 play2范围。

It will then be necessary to modify the value of the turn attribute so that the next player can play.然后有必要修改 turn 属性的值,以便下一个玩家可以玩。

 class Terrain():
def __init__(self, tour , grille = [0,1,2,3,4,5,6,7,8]):
    self.grille = grille
    self.tour = 1
def __str__(self):
     for i in range(3):
        return self (" | ".__str__() +str(grille[i+3]), end='')  
        print("     0)  1)  2)")
    for i in range(3,6):
        print(" | "+str(grille[i+3]), end='')  
        print("     0)  1)  2)")   
    for i in range(6,9):
        print(" | "+str(grille[i+3]), end='')  

and I'm blocking I don't understand the str method someone can help me.我阻止了我不明白有人可以帮助我的str方法。

str takes in an argument which is something like an integer or a float, and converts it into a String (word). str接受一个类似于整数或浮点数的参数,并将其转换为String (单词)。 To add a string and a number together, you need to convert that number into a string (a word) first.要将string和数字相加,您需要先将该数字转换为string (单词)。

The __str__ method is used to specify how your class will look like when it is treated as a string . __str__方法用于指定类被视为字符串时的外观 For example, cionsider a class like this:例如, cionsider 一个这样的类:

class myClass:
    def __init__(self, x, y) -> None:
        self.x = x
        self.y = y

When you call print(myClass(2, 3)) you will get something like:当您调用print(myClass(2, 3))您会得到如下信息:

< __main__ .myClass object at 0x00....> < __main__ .myClass 对象在 0x00....>

But, if you have a class like this:但是,如果您有这样的课程:

class myClass:
    def __init__(self, x, y) -> None:
        self.x = x
        self.y = y

    def __str__(self) -> str:
        return f"x: {self.x}\ny: {self.y}"

Then, when you call print(myClass(2, 3)) you will get然后,当你调用print(myClass(2, 3))你会得到

x: 2 ×:2

y: 3 y: 3

In your case, I think the code should look like this:在你的情况下,我认为代码应该是这样的:

class Case:
    def __init__(self) -> None:
        self.occupy = ' '

    def play1(self) -> None:
        if self.occupy == ' ':
            self.occupy = 'X'

    def play2(self) -> None:
        if self.occupy == ' ':
            self.occupy = 'O'

class Terrain:
    def __init__(self, turn: int) -> None:
        self.grid = [Case(), Case(), Case(),
                     Case(), Case(), Case(),
                     Case(), Case(), Case()]
        self.turn = turn
        self.lap = 1

    def __str__(self) -> str:
        line1 = [x.occupy for x in self.grid[:3]]
        line2 = [x.occupy for x in self.grid[3:6]]
        line3 = [x.occupy for x in self.grid[6:]]
        final_string = '\n'.join(['|'.join(line) for line in [line1, line2, line3]])
        return final_string

Or, if you are like me, and love doing one-liners:或者,如果你像我一样,喜欢做单行:

class Case:
    def __init__(self) -> None:
        self.occupy = ' '
    def play1(self) -> None:
        if self.occupy == ' ':
            self.occupy = 'X'
    def play2(self) -> None:
        if self.occupy == ' ':
            self.occupy = 'O'

class Terrain:
    def __init__(self, turn: int) -> None:
        self.grid = [Case() for _ in range(9)]
        self.turn = turn
        self.lap = 1
    def __str__(self) -> str:
        return '\n'.join(map(lambda x: '|'.join(map(lambda y: y.occupy, self.grid[3*x:3*x+3])), range(3)))

Also, remember that the value returned by __str__ must be a string.另外,请记住__str__返回的值必须是字符串。

and about what we can do以及我们能做什么

'Define, in the class Terrain, a method play which will take as parameter an integer ranging from 0 to 8. Depending on the player whose turn it is to play, this method will call the methods play1 or play2 of the cell corresponding to the integer passed in parameter.' '在类Terrain中定义一个方法play,其参数为0到8之间的整数。根据轮到的玩家,该方法将调用对应单元格的方法play1或play2整数传入参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM