Python在函数中传递变量

2024-06-16 08:49:19 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试将X从test()传递到rfid()以进行测试,但它不起作用:

这就是我所拥有的:

def test(x):
        testcard = 8974
        x=testcard
        return x


def rfid():
        #This will just see if the rfid chip has enough digits

        main= 8974

        # This looks to see if the rfid chip has enough digits to pass the test

        if main == x:
                print ("Card read check complete")
                cardlength= info[main + readlen]
                main += readlen + 1
                return True,info[main:main + cardlength]
        else:
                        main += readlen + 1
        return False,0,''


def main():
        rfid(x)

main()

Tags: thetestreturnifmaindefthisrfid
1条回答
网友
1楼 · 发布于 2024-06-16 08:49:19

首先,你必须解决这个问题:

def rfid(x):
    # ... rest of code

你看,如果一个函数需要一个参数,它必须被声明为这样。接下来,您必须从test()调用rfid()

def test():
        x = 8974
        return rfid(x)

main()中,调用test()。或者只调用test(),不需要声明和调用main()

def main():
        test()

main()

相关问题 更多 >