重复部分python函数?

时间:2014-01-10 04:36:14

标签: python function python-2.7 printing

以下是代码:

    rows = [[convert[random.randint(0,7)] for _ in range(5)] for _ in range(5)]
    cols = list(zip(*rows))
    printedrows = [["[X]","[X]","[X]","[X]","[X]","  <- V: {}   TOTAL: {}".format(row.count(0), sum(row))] for row in rows]
    printedcolvolt = ["V:{}".format(col.count(0)) for col in cols]
    printedcolsum = ["T:{}".format(sum(col)) for col in cols]
    numgood = numtiles - sum(row.count(0) for row in rows) - sum(row.count(1) for row in rows)

def revealc(x):
    global forcelose,rowcol,last
    rowcol = True
    newinput(x,1)
    newinput(x,2)
    newinput(x,3)
    newinput(x,4)
    last = True
    newinput(x,5)
    last = False
    rowcol = False
    forcelose = False
    scorelist = []
    skipnum = 0

def revealr(x):
    global forcelose,rowcol,last
    rowcol = True
    newinput(1,x)
    newinput(2,x)
    newinput(3,x)
    newinput(4,x)
    last = True
    newinput(5,x)
    last = False
    rowcol = False
    forcelose = False
    scorelist = []
    skipnum = 0

def newinput(x,y):
    global rowcol,wrongformat,redone,bankscore,curscore,numgood,inputloc,location,forcelose,last

    wrongformat = False
    redone = False

    print "x/y = ",x,y
    if rowcol:
        skipnum += 0

    if y == -1 and not skipnum == 5:
        rowcol = False
        inputloc = raw_input("\nLocation? ")
        if len(inputloc) != 2:
            wrongformat = True
        else:
            location = [str(inputloc[0]),str(inputloc[1])]
            if str(inputloc[1]).isalpha():
                wrongformat = True
            elif not wrongformat:
                location = [inputloc[0],int(inputloc[1])]
                if (0 < location[1] < 6):
                    if location[0].isalpha():
                        if location[0] == "r":
                            revealr(location[1])
                        elif location[0] == "c":
                            revealc(location[1])
                        else:
                            wrongformat = True
                    else:
                        location[0] = int(inputloc[0])
                else:
                    wrongformat = True
            else:
                wrongformat = True
    else:
        location = [x,y]

    print 1
    x,y = location[0]-1,location[1]-1
    print 2

    if not wrongformat and not str(location[0]).isalpha():
        if (-1 < x < 5) and (-1 < y < 5):
            print x,y
            if str(printedrows[y][x]) == str("["+str(rows[y][x])+"]") and not forcelose:
                if not rowcol:
                    redone = True
            elif rows[y][x] != 0 and not forcelose:
                printedrows[y][x] = "["+str(rows[y][x])+"]"
                if curscore <= 0:
                    curscore = int(rows[y][x])
                else:
                    curscore = curscore * int(rows[y][x])
                if rows[y][x] > 1:
                    if numgood > 1:
                        numgood -= 1
                    else:
                        winlose("win")
            else:
                if rowcol and not last:
                    forcelose = True
                    if rowcol <= 0:
                        scorelist.append(curscore)
                    else:
                        scorelist.append(int(rows[y][x]))
                else:
                    if last:
                        if curscore <= 0:
                            curscore = sum(scorelist)
                        else:
                            curscore = curscore * sum(scorelist)
                    winlose("lose")
        else:
            wrongformat = True
    else: 
        pass

这里的问题是,当揭示c / r的最后一部分通过newinput()时,它会重复。我不确定为什么,只要我能得到它以便它不会抛出“redone = True”我会很高兴。

newinput(0,-1)

输入“r3”但是:

x/y = 1 1
1
2
x/y = 0 0
2 1
1
2
x/y = 1 0
3 1
1
2
x/y = 2 0
4 1
1
2
3 0
1
2
3 0  # This part is repeated a second time. WHY?!

0 个答案:

没有答案