更改要在循环内使用的变量(Python)

时间:2014-01-31 19:10:37

标签: python arrays variables python-3.x

所以我正在编写一个跳棋游戏,而我遇到的问题是在循环中创建几个部分。我有代码的类创建部分,我不会在这里发布,其余的,我发布,但我不知道如何更改循环即将使用的变量。如果你能伸出援助之手,我会很感激。

很抱歉将我的代码发布为图片,我是这个网站(和编程)的新手,并且无法格式化以便网站接受我的帖子。我真的希望你能帮助我!

感谢您的帮助!

进一步澄清:每次循环运行时我都需要使用不同的“片段”创建。这意味着第一个循环必须创建piece1,然后是piece2,然后是piece3 ......然后转发

编辑:发布整个代码。我知道格式错了,无法帮助它。所以,希望有人能解决它。

class Piece:
    def __init__(self, kind, yposition, xposition):
        self.color = kind
        self.ypos = xposition
        self.xpos = yposition

    def getColor(self):
        return self.getColor

    def adjustY(self, change):
        self.ypos = self.ypos + change

    def adjustX(self, change):
        self.xpos = self.xpos + change

    def getY(self):
        return self.ypos

    def getX(self):
        return self.xpos

    def mover(self, direction):
        self.direc = direction
        if self.direc == "right" and self.color == "white":
            for n in alist:
                if n.getY == (self.getY - 1) and n.getX == (self.getX + 1):
                    pass
                    # NOT YET IMPLEMENTED

                else:
                    self.adjustY(-1)
                    self.adjustX(+1)

        elif self.direc == "left" and self.color == "white":
            for n in alist:
                if n.getY == (self.getY - 1) and n.getX == (self.getX - 1):
                    pass
                    # NOT YET IMPLEMENTED

                else:
                    self.adjustY(-1)
                    self.adjustX(-1)
        elif self.direc == "right" and self.color == "black":
            for n in alist:
                if n.getY == (self.getY + 1) and n.getX == (self.getX + 1):
                    pass
                    # NOT YET IMPLEMENTED

                else:
                    self.adjustY(+1)
                    self.adjustX(+1)

        else:
            for n in alist:
                if n.getY == (self.getY + 1) and n.getX == (self.getX - 1):
                    pass
                    # NOT YET IMPLEMENTED

                else:
                    self.adjustY(+1)
                    self.adjustX(-1)


piece1 = 0
piece2 = 0
piece3 = 0
piece4 = 0
piece5 = 0
piece6 = 0
piece7 = 0
piece8 = 0
piece9 = 0
piece10 = 0
piece11 = 0
piece12 = 0

alistb1 = [piece1,piece2,piece3,piece4,piece5,piece6,piece7,piece8,piece9,piece10,piece11,piece12]

k = 2

for i in range(0,11):
    if i >= 0 and i <5:
        j = 8
        m = 0

    elif i >= 5 and i < 9:
        j = 7
        m = 1

    else:
        j = 6
        m = 0
    alistb1[i] = Piece("white",j,(m + 1 + i * k))
    print(alistb1[i].getY())

# print(piece7.getY()) test reasons

PS:def mover尚未准备就绪。

2 个答案:

答案 0 :(得分:0)

使用dictionary和a loop

pieces = {}
# I'm assuming you want 12 pieces since your list has 12 pieces      
for i in range(1,13):  # range starts at m so, range(m,n) iterates from m up to n-1
    # I would suggest using more descriptive variable names if you can, row or column for example
    if i >= 0 and i <5:
        j = 8
        m = 0

    elif i >= 5 and i < 9:
       j = 7
       m = 1

    else:
       j = 6
       m = 0
    pieces['piece{}'.format(i)] = Piece("white",j,(m + 1 + i * k))

这应该做你想要的,除非我误解你。此外,这不是C ++,你不需要那些get方法,只需Piece.color即可获得一个颜色属性。

使用字典访问作品pieces['piece1'].whatever()。但是为了简洁起见,您不需要pieces['piece{}.format(i)]只能pieces[i],而且可以访问pieces[1].whatever()

有关字典http://docs.python.org/3.3/tutorial/datastructures.html#dictionaries的更多信息

我所拥有的并且没有任何错误:

class Piece:
    def __init__(self, kind, yposition, xposition):
        self.color = kind
        self.ypos = xposition
        self.xpos = yposition

    def getColor(self):
        return self.getColor

    def adjustY(self, change):
        self.ypos = self.ypos + change

    def adjustX(self, change):
        self.xpos = self.xpos + change

    def getY(self):
        return self.ypos

    def getX(self):
        return self.xpos

    def mover(self, direction):
        self.direc = direction
        if self.direc == "right" and self.color == "white":
            for n in alist:
                if n.getY == (self.getY - 1) and n.getX == (self.getX + 1):
                    pass
                    # NOT YET IMPLEMENTED

                else:
                    self.adjustY(-1)
                    self.adjustX(+1)

        elif self.direc == "left" and self.color == "white":
            for n in alist:
                if n.getY == (self.getY - 1) and n.getX == (self.getX - 1):
                    pass
                    # NOT YET IMPLEMENTED

                else:
                    self.adjustY(-1)
                    self.adjustX(-1)
        elif self.direc == "right" and self.color == "black":
            for n in alist:
                if n.getY == (self.getY + 1) and n.getX == (self.getX + 1):
                    pass
                    # NOT YET IMPLEMENTED

                else:
                    self.adjustY(+1)
                    self.adjustX(+1)

        else:
            for n in alist:
                if n.getY == (self.getY + 1) and n.getX == (self.getX - 1):
                    pass
                    # NOT YET IMPLEMENTED

                else:
                    self.adjustY(+1)
                    self.adjustX(-1)

k=2
pieces = {}
# I'm assuming you want 12 pieces since your list has 12 pieces      
for i in range(1,13):  # range starts at m so, range(m,n) iterates from m up to n-1
    # I would suggest using more descriptive variable names if you can, row or column for example
    if i >= 0 and i <5:
        j = 8
        m = 0

    elif i >= 5 and i < 9:
       j = 7
       m = 1

    else:
       j = 6
       m = 0
    pieces['piece{}'.format(i)] = Piece("white",j,(m + 1 + i * k))

输出:

>>> pieces['piece1'].color
'white'
>>> pieces['piece3'].color
'white'
>>> pieces['piece3'].xpos
8
>>> for key in pieces:
    print(key, pieces[key])


piece8 <__main__.Piece object at 0x000000000329A4E0>
piece9 <__main__.Piece object at 0x000000000329A550>
piece6 <__main__.Piece object at 0x000000000329A400>
piece7 <__main__.Piece object at 0x000000000329A470>
piece4 <__main__.Piece object at 0x000000000329A320>
piece5 <__main__.Piece object at 0x000000000329A390>
piece2 <__main__.Piece object at 0x0000000003287DA0>
piece3 <__main__.Piece object at 0x000000000329A2B0>
piece1 <__main__.Piece object at 0x00000000031D9CF8>
piece10 <__main__.Piece object at 0x000000000329A5C0>
piece11 <__main__.Piece object at 0x000000000329A630>
piece12 <__main__.Piece object at 0x000000000329A6A0>
>>> pieces['piece3'].mover('right')
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    pieces['piece3'].mover('right')
  File "C:/Users/Hannah/Documents/thing.py", line 25, in mover
    for n in alist:
NameError: global name 'alist' is not defined
>>> pieces['piece3'].xpos
8
>>> pieces['piece3'].adjustX(1)
>>> pieces['piece3'].xpos
9

请记住,词典是无序的,因此它们打印的顺序是任意的。

由于我的代码版本中没有alist,因此预计会有移动回溯。您需要修改mover()才能使用字典。使用dicts的一些有用方法:

>>> for n in pieces.values():  # iterates over the values in a dict
        n.color


'white'
'white'
'white'
'white'
'white'
'white'
'white'
'white'
'white'
'white'
'white'
'white'

>>> for n in pieces.keys():  # iterates over the keys
        print(n)


piece8
piece9
piece6
piece7
piece4
piece5
piece2
piece3
piece1
piece10
piece11
piece12

答案 1 :(得分:0)

您无需为每件作品分配变量。您已经在使用列表作为您的作品。您可以只编写piece1,而不是撰写pieces[0]。 (您需要注意列表以索引0开头。)

range拥有专有权限。这意味着它不包括在内,您的范围以小于该值的一个结束。您想使用range(0,12)

在python中,您可以动态添加到列表中。您无需分配足够的空间来容纳您的作品。您可以使用列表的.append()方法。

现在编写代码的一种方法是:

pieces = []
for i in range(0, 12):      # 0-11
    if i < 5:
        pieces.append(Piece("white", 8, 1 + i*2))
    elif i < 9:
        pieces.append(Piece("white", 7, 2 + i*2))
    else:
        pieces.append(Piece("white", 6, 1 + i*2))

我冒昧地简化了条件语句(i将始终为>= 0,如果i < 5为假,则反向i >= 5为真,所以您无需在elif)中重新声明并删除jkm这些不必要的变量,可以用文字替换以节省存储器中。

还有一件事:getColor的实现将返回函数对象本身。我想你想做:

def getColor():
    return self.color