Python 在def循环中将变量增加1的位置?

Python 在def循环中将变量增加1的位置?,python,selenium,Python,Selenium,我有这段代码: if storagetypecheck1 == "Virtual": def storagecheck(): d = 1 dforid = str(1-d) while d <= int(numcount): storageidprefix = "specimen" + "[" + dforid + "]" driver.

我有这段代码:

if storagetypecheck1 == "Virtual":
        def storagecheck():
            d = 1
            dforid = str(1-d)
            while d <= int(numcount): 
                storageidprefix = "specimen" + "[" + dforid + "]"
                driver.find_element_by_id(storageidprefix + ".storageContainerForSpecimen").click()
                pag.press('a')
                pag.press('enter')
                time.sleep(1)
                d = d + 1
                storagecheck()
        storagecheck()
如果storagetypecheck1==“虚拟”:
def storagecheck():
d=1
dforid=str(1-d)
而d
storagecheck()
递归地调用自身。每次它调用自身时,都会执行行
d=1
,因此会重置d的值。您需要将
d
置于
d
的函数定义之外才能继续递增,如下所示:

if storagetypecheck1 == "Virtual":
        d = 1
        def storagecheck():
            global d
            dforid = str(1-d)
            while d <= int(numcount): 
                storageidprefix = "specimen" + "[" + dforid + "]"
                driver.find_element_by_id(storageidprefix + ".storageContainerForSpecimen").click()
                pag.press('a')
                pag.press('enter')
                time.sleep(1)
                d = d + 1
                storagecheck()
        storagecheck()
如果storagetypecheck1==“虚拟”:
d=1
def storagecheck():
全球d
dforid=str(1-d)

欢迎来到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。您在循环中每次都递增
d
。但是你每次通过循环都会递归地调用整个函数,新函数从它自己的
d=1
开始,它递归地调用自己,新函数从它自己的
d=1
开始,它递归地…我不确定你在这里想做什么,为什么你一开始就递归地称呼自己。如果你能解释的话,也许我们可以解释如何修复它。如果没有理由这么做,解决办法可能很简单,就是不做那一行。