为什么我的代码的一部分不能在函数末尾运行?

时间:2019-04-03 22:30:44

标签: python

我正在创建一个程序,该程序生成一系列随机的管道字符以创建管道块。在我的ranPipe()函数的末尾,有一个临时转换器,可将代表管道字符的文本更改为该字符。出于某种原因,它似乎没有运行-在转换器的for循环内和外部(仅在函数末尾)都使用print()s对其进行测试,似乎没有运行(或在至少,它不会打印)。

我不能只为转换创建一个新函数,因为它依赖于ranPipe()中使用的变量,而且我对全局变量声明不是很好,因此它引起的问题超出了解决的范围。还必须在ranPipe()函数的引用点,因为它应该在代码中前面提到的检查之后进行。

为什么会这样,我该如何解决? (下面的代码)

import random

finCheck = 0
pipeNums = {
    ##Note: Structure of values is icon, upcon, downcon, leftcon, rightcon
    "vertCon": "║1100",
    "horiCon": "═0011",
    "leftCornUpper": "╔0101",
    "rightCornUpper": "╗0110",
    "leftCornLower": "╚1001",
    "rightCornLower": "╝1010",
    "vertConMidL": "╠1101",
    "vertConMidR": "╣1110",
    "horiConMidN": "╦0111",
    "horiConMidS": "╩1011",
    "cross": "╬1111"
}

def ranPipe(north, south, left, right):
    ##Generates a random piece
    curPipe = random.choice(list(pipeNums))

    ##Resets piece check
    finCheck = 0

    ##Start of piece check looop
    while finCheck < 3:
        ##If the piece doesn't match previous piece, regen.
        while (curPipe[1] == "1" and prePipe[2] == "0") or (curPipe[3] == "1" and prePipe[4] == "0") or (curPipe[4] == "1" and prePipe[3] == "0"):
            curPipe = random.choice(list(pipeNums))
        ##Adds to check total when this completes
        finCheck += 1

        ##Additional checks for edge pieces.
        while True:
            if north == 1:
                while curPipe[1] == "1":
                    curPipe = random.choice(list(pipeNums))
                return curPipe
            elif south == 1:
                while curPipe[2] == "1":
                    curPipe = random.choice(list(pipeNums))
                return curPipe
            elif left == 1:
                while curPipe[3] == "1":
                    curPipe = random.choice(list(pipeNums))
                return curPipe
            elif right == 1:
                while curPipe[4] == "1":
                    curPipe = random.choice(list(pipeNums))
                return curPipe
            break

        ##Adds to check total when this completes
        finCheck += 1

        ##Runs one more general check. Adds if it passes, otherwise all check points are removed and it repeats.
        while True:
            if (curPipe[1] == "1" and prePipe[2] == "0") or (curPipe[3] == "1" and prePipe[4] == "0") or (curPipe[4] == "1" and prePipe[3] == "0"):
                finCheck = 0
                curPipe = random.choice(list(pipeNums))
            else:
                finCheck += 1
                break

    ##Converts the value of the current pipe to its value rather than its name    
    conChart = ['║', '═', '╔', '╗', '╚', '╝', '╠', '╣', '╦', '╩', '╬']
    for x in range(conChart):
        if curPipe == pipeNums[x]:
            curPipe = conChart[x]

def digDig(length, width):
    print("What length do want your pipes to be?")
    length = int(input("Note: The length and width of your creation may vary, as it is counted in number of characters rather than a definite measure.\n"))
    width = int(input("And the width?\n"))

    ##Start of code, top corner
    prePipe = random.choice(list(pipeNums))
    print(prePipe)

    ##Top line
    for x in range(length):
        print(ranPipe(1, 0, 0, 1))

((我很高兴我的代码在(a)某些地方不是很Python语言,并且(b)是不完整的。尽管我一直很乐意帮助提高代码效率,但我还是特地寻求转换器问题的帮助,并且希望重点放在该代码上,而不是我费解的代码。

1 个答案:

答案 0 :(得分:0)

要回答您的问题,请使用参数ranPipe调用north=1,因此,当您进入第三个while循环时,行if north == 1:的计算结果为true,结果是正在执行返回操作并退出了函数。

就像我在遍历代码时所看到的事情的附录一样: 您正在函数prePipe中使用ranPipe,但尚未在该函数中定义它;您仅在digDig中定义了它,并且您正在使用for x in range(conChart),但是range函数接受一个整数:您需要指定len(conChart)