创建网格

时间:2013-12-06 20:45:44

标签: python-3.x grid

我被要求创建一个显示[1, ‘*’, ‘*’, ‘*’], [‘*’ , 2, ‘*’, ‘*’], [‘*’, ‘*’, 3, ‘*’]和最后一行[‘*’, ‘*’, ‘*’, 4]的网格。我得到了下面的课程,但不是因为对它的热爱,我可以像上面那样打印网格。我已经完成了所有*但没有运气,任何帮助以获得正确的方向都会有所帮助。

由于

import math
def calculateDistance(loc1, loc2):
    x = (loc2[0]-loc1[0])
    y = (loc2[1]-loc1[1])
    addXY = (x*x)+(y*y)
    return math.sqrt(addXY)

testDistance = calculateDistance((1,2), (5,3))
print(testDistance)

###The implementation of the Grid ADT (Grid Class)

class Grid:

def __init__(self, n):
    self.listOfRows = []
    for i in range(n):
        row = []
        for j in range(n):
            row.append('*')
        self.listOfRows.append(row) 


def setValue (self, x, y, value):
    self.listOfRows[x - 1][y - 1] = value

def __str__(self):
    return str(self.listOfRows)

1 个答案:

答案 0 :(得分:0)

为什么你不能这样做:

grid = Grid(4)
grid.setValue(0,0,1)
grid.setValue(1,1,2)
grid.setValue(2,2,3)
grid.setValue(3,3,4)
相关问题