繁体   English   中英

当参数之一不在 scope 中时,在主 function 中运行 function 的最佳方法

[英]Best way to run a function in main function when one of the parameters is not in the scope

基本上我的大脑现在无法正常工作,无法找出解决此错误的最佳方法。 builtins.NameError: name 'numIters' is not defined我知道这个问题是 numIters 没有在其 scope 中定义,但不知道解决该问题的最佳解决方案。

这是我的大部分代码

import random
alg = int(input("Select the sorting algorithm \n 1 - linear search \n 2 - binary search \nEnter Choice: "))
n = int(input("Choose the size of the list: ")) 
def main():
    createList(n,alg)
    print(runTest(values,n,alg))
    printResults(alg,n,numIters)
    #print(linearSearch(values,2))
    #print(binarySearch(values, 2))


def createList(n,alg):
    global values
    values = []
    random.seed(1456)
    for j in range(n):
        values.append(random.randint(0, 2*n))

    while len(values) == n:
        if alg == 2:
            values.sort()
            print(values)
            return values
        elif alg == 1:
            print(values)
            return values

def linearSearch(values, target):
    numIters = 0
    for i in range(len(values)):
        numIters = numIters + 1
        if values[i] == target:
            return numIters
    return -1
def binarySearch(values, target):
    numIters = 0
    start = 0
    high = len(values) - 1
    while start <= high:

        middle = (start + high)//2

        if values[middle] == target:

            numIters = numIters + 1
            return numIters
        elif values[middle] > target:
            numIters = numIters + 1
            high = middle - 1
        else:
            numIters = numIters + 1
            start = middle + 1

    return -1
def runTest(values,n,alg):
    if alg == 2:
        count = 0
        for j in range(n * 2):
            count = count + 1
            tgt = random.randint(0, 2*n)
            binarySearch(values, tgt)
        return count
    elif alg == 1:
        count = 0
        for j in range(n * 2):
            count = count + 1
            tgt = random.randint(0, 2*n)
            linearSearch(values, tgt)
        return count

def printResults(alg, n, numIters):
    avgIter = n / numIters
    if alg == 2:
        algType = Binary
    if alg == 1:
        algType = Linear
    print("Results \n n = %d \n %s = %f.2 " % (n,algtype,avgIter))

main()

提前感谢您提供的任何帮助,因为我仍在努力学习和了解 python 的整体工作原理。

您需要返回numIters以便将其传递给下一个 function。 看起来它当前从binarySearchlinearSearch返回到runTest ,但在那里被丢弃; 像这样冒泡(我将添加类型注释和注释以帮助我跟踪正在发生的事情):

from typing import List, Tuple

def runTest(values: List[int], n: int, alg: int) -> Tuple[int, int]:
    """Returns count, numIters"""
    numIters = 0  # default value in case n is so small we don't iterate
    if alg == 2:
        count = 0
        for j in range(n * 2):
            count = count + 1
            tgt = random.randint(0, 2*n)
            numIters = binarySearch(values, tgt)
        return count, numIters
    elif alg == 1:
        count = 0
        for j in range(n * 2):
            count = count + 1
            tgt = random.randint(0, 2*n)
            numIters = linearSearch(values, tgt)
        return count, numIters
    raise ValueError("alg needs to be 1 or 2!")

现在在您的main()中,您可以执行以下操作:

def main():
    createList(n, alg)
    count, numIters = runTest(values, n, alg)
    print(count)
    printResults(alg, n, numIters)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM