需要一个整数?

时间:2018-03-18 18:04:07

标签: python python-3.x

我可以通过我的Wtime功能让我的代码工作,但在为waitTime设置Wtime之后,它并没有翻译成我的main {1}}功能。当我按下按钮让我的LED工作时,我得到:

  time.sleep(waitTime)
TypeError: an integer is required (got type function)

这是我的代码:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#leds
rled = 16
yled = 6

#btns
btnOnVal = 12
btnOffVal = 18

#ldr
ltSense = 21


GPIO.setup(rled, GPIO.OUT)
GPIO.setup(yled, GPIO.OUT)

GPIO.setup(btnOnVal, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(btnOffVal, GPIO.IN, pull_up_down = GPIO.PUD_UP)

GPIO.setup(ltSense, GPIO.IN, pull_up_down = GPIO.PUD_UP)

def Wtime():
    try:
        waitTime = float(input("Please enter a wait time for flashing lights(1 - 10 secs):"))

        while (waitTime < 0 or waitTime > 10):
            waitTime = float(input("Please enter a wait time for flashing lights(1 - 10 secs):"))
    except:
        print("Not Integer Value")

    return waitTime

def rblink(waitTime):
    GPIO.output(rled, GPIO.HIGH)
    time.sleep(waitTime)
    GPIO.output(rled, GPIO.LOW)
    time.sleep(waitTime)

def yblink(waitTime):
    GPIO.output(yled, GPIO.HIGH)
    time.sleep(waitTime)
    GPIO.output(yled, GPIO.LOW)
    time.sleep(waitTime)

def main(waitTime):
    while (GPIO.input(ltSense) == 1):
        if (GPIO.input(btnOnVal) == False):
            for i in range(0,10,1):
                rblink(waitTime)

        if (GPIO.input(btnOffVal) == False):
            for i in range(0,10,1):
               yblink(waitTime)

#function call
Wtime()
main(yblink)

如何解决此错误?

1 个答案:

答案 0 :(得分:3)

您需要将wTime()的结果传递给main()

main(Wtime())

此外,理想情况下,您应该使用小写字符启动函数名称,因为标准是类的大写字符。