我有这个功能

def getInput(rows, cols, myList):
    myList = [[0]*(cols-2) for i in range(rows-2)] #creates the board
    for i in myList: # adds -1 to beginning and end of each list to make border
        i.append(-1)
        i.insert(0,-1)
    myList.insert(0,[-1]*(cols)) #adds top border
    myList.append([-1]*(cols)) #adds bottom border

    while True:
        rows = input("Please enter the row of a cell to turn on or 'q' to exit: ")
        if rows == 'q': # if q then end while loop
            break
        cols = input("Please enter the column of a cell to turn on: ")
        print()
        myList[int(rows)][int(cols)] = 1 # changes chosen cells from 0(dead) to 1(alive)
    return myList


而且我需要知道一种使此功能不间断或继续进行的方法。

最佳答案

我认为应该这样做:

...
rows = ""
while rows != 'q':
    rows = input("Please enter the row of a cell to turn on or 'q' to exit: ")
    if rows != 'q': # if q then end while loop
        cols = input("Please enter the column of a cell to turn on: ")
        print()
        myList[int(rows)][int(cols)] = 1 # changes chosen cells from 0(dead) to 1(alive)
return myList


仅当行不为True时,才进入if块,并且如果在任何运行中将行初始化为"q",则while循环将在下一次运行中自动终止。

关于python - Python Make函数不中断或继续,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29521184/

10-12 21:47