简体   繁体   English

调用函数为Python中循环的每次迭代获取新值

[英]Calling a function to obtain a new value for each iteration of a loop in Python

I have written a function, getSteps() which uses the random package, which should return a different value each time (it does when I run the function multiple times manually). 我编写了一个函数getSteps() ,该函数使用随机包,该函数每次都应返回一个不同的值(当我多次手动运行该函数时会返回一个不同的值)。

But when I call it inside a for loop or while loop, the script just calls the function once and then uses the value returned for the first iteration for each iteration thereafter. 但是,当我在for循环或while循环中调用它时,脚本只调用一次函数,然后将第一次迭代返回的值用于此后的每次迭代。

I am attempting to call the function as follows: 我试图按以下方式调用该函数:

F = open("outFile.txt","w")
for i in range(6,100):  ## for each of these values
    for j in range(0,100):  ## call the function 100 times
        steps = getSteps(i)
        F.write(str(i)+","+str(steps)+"\n")

It should have a different value for each iteration, but I find each is the same. 对于每次迭代,它应该具有不同的值,但是我发现每一个都是相同的。 Could anyone tell me how to fix this? 谁能告诉我该如何解决?

The function is defined as below: 该函数定义如下:

def getSteps(maxSteps):
    global numberSteps, numberAttempts, found
    while (found == False):
        sourceJar = psj[random.randrange(0, len(psj))]
        if sourceJar in pdj:
            pdj.remove(sourceJar)
        destJar = pdj[random.randrange(0, len(pdj))]
        if sourceJar == 8 and destJar == 5:
            etf()
        elif sourceJar == 8 and destJar == 3:
            ett()
        elif sourceJar == 5 and destJar == 8:
            fte()
        elif sourceJar == 5 and destJar == 3:
            ftt()
        elif sourceJar == 3 and destJar == 8:
            tte()
        elif sourceJar == 3 and destJar == 5:
            ttf()
        found = checkFound()
        outString = "Pour the " + str(sourceJar) + " litre jar into the " + str(
            destJar) + " litre jar. The volumes for each jar are now "
        outString += "8-" + str(jar8volume) + ",5-" + str(jar5volume) + ",3-" + str(jar3volume)
        ##print outString
        updateLists()
        if (numberSteps >= maxSteps):
            numberAttempts += 1
            reset()
            ##print "Currently on iteration number " + str(numberAttempts)
        numberSteps += 1

    return numberAttempts + 1

There's a lot of code you're not showing. 您没有显示很多代码。 But presumably the problem is that you use global variables inside getSteps ; 但是大概的问题是您在getSteps使用了全局变量; once found is set to True on the first invocation, it will remain true and none of the code inside the if block will execute on subsequent calls. 一旦found则在第一次调用时将其设置为True,它将保持为true,并且if块中的任何代码都不会在后续调用中执行。

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

相关问题 在循环的每次迭代中运行一个函数作为 python 中的一个新进程 - Running a function in each iteration of a loop as a new process in python 如何在for循环的每次迭代中获取2组键值对? - How to obtain 2 sets of key,value pair in each iteration of for-loop? for循环的每个迭代中的Python新变量 - Python new variable in each iteration of for loop Python random.choice()与if函数一起循环-如何为每次迭代分配值 - Python random.choice() in loop with if-function - how to assign value to each iteration 将 DataFrame 的每次迭代“for 循环”结果保存为 Python 中的新数组 - Saving Each Iteration of DataFrame "for loop" results as a new array in Python 在python中for循环的每次迭代之后为数据创建新列 - Create new column for data after each iteration of a for loop in python Python:如何在for循环的每次迭代中创建新变量? - Python: How to create new variable in each iteration of a for loop? Python 日志记录 - 如何为循环中的每次迭代添加新的日志文件? - Python logging - How to add a new log file for each iteration in a loop? Python 记录 - 每次循环迭代的新日志文件 - Python logging - new log file each loop iteration 从循环中的其他函数开始循环的新迭代[python] - Start a new iteration of a loop from a different function in the loop [python]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM