'NoneType' 对象不可调用。 Python

'NoneType' Object is not callable. Python

我尝试为 Python 中的 raspberry pi 编写一个小游戏(例如 "Not-Not" 游戏)。但我总是得到错误:'Nonetype' Object is not callable,这实际上没有意义?在欢迎屏幕之后,游戏实际上应该以倒计时开始,然后随机颜色应该能够在显示器上看到。如果你按下正确的按钮,它应该会给你更高的分数并继续,直到它达到 1000 分。如果你按下错误的按钮,你将得到负 100 分。如果分数达到 0,游戏应该结束。

我也是 python 的新手。感谢您的回答 :)。

这是错误:

Traceback (most recent call last):
  File "game.py", line 316, in <module>
    Menu()
  File "game.py", line 114, in Menu
    Start_Game()
  File "game.py", line 139, in Start_Game
    Random_Sequency()
  File "game.py", line 118, in Random_Sequency
    random.choice(Sequenzen)()
TypeError: 'NoneType' object is not callable

这是代码:

import RPi.GPIO as GPIO
import time
import lcddriver 
import random

lcd = lcddriver.lcd()

lcd.lcd_clear()

#Welcome

lcd.lcd_display_string("   Welcome to", 1)
lcd.lcd_display_string("   Not-Not :)", 2) 

#End Welcome 

InputPin = 35 #Rot
InputPin2 = 32 #Blau
InputPin3 = 37 #Gruen
InputPin4 = 38 #Gelb 

OutputPin = 11 #Rot
OutputPin2 = 12 #Blau
OutputPin3 = 13 #Gruen
OutputPin4 = 15 #Gelb 

BuzzerPin = 40 #Buzzer 

WaitTime = 0.5 #Wartezeit 

#SETUP

GPIO.setmode(GPIO.BOARD)

GPIO.setup(InputPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Input 1

GPIO.setup(InputPin2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Input 2
GPIO.setup(InputPin3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Input 3
GPIO.setup(InputPin4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Input 4

GPIO.setup(OutputPin, GPIO.OUT) #Output 1

GPIO.setup(OutputPin2, GPIO.OUT) #Ouput 2
GPIO.setup(OutputPin3, GPIO.OUT) #Output 3
GPIO.setup(OutputPin4, GPIO.OUT) #Output 4 

#SETUP ENDE

#ENGINE

SCORE = 0

#Hauptmenue

def Menu(): 
    #Hauptmenue hier hin.
    Start_Game()
    #Hauptmenue Ende 

def Random_Sequency():
    random.choice(Sequenzen)()

#Spiel
def Start_Game():
    global SCORE

    lcd.lcd_clear()

    while(SCORE >= 0): 
        while(SCORE < 1000):

            lcd.lcd_display_string("Game starts in:", 1)
            lcd.lcd_display_string("3 Seconds.", 2)
            time.sleep(1)
            lcd.lcd_display_string("2 Seconds..", 2)
            time.sleep(1)
            lcd.lcd_display_string("1 Second...", 2)
            time.sleep(1)

            lcd.lcd_display_string("PRESS:", 1)

            Random_Sequency()

        lcd.lcd_display_string("Congratulations:", 1)
        lcd.lcd_display_string("YOU WON!", 2)
        time.sleep(5)
        SCORE = 0

        Menu() 



    lcd.lcd_display_string("    GAME", 1)
    lcd.lcd_display_string(" OVER :(", 2)
    time.sleep(5)
    SCORE = 0

    Menu()

#SEQUENZEN

#Blau
def Blue():
    global SCORE
    lcd.lcd_display_string("BLUE", 2)
    if(GPIO.input(InputPin2) == 1):
        lcd.lcd_display_string("<><><><><><><><>", 1)
        lcd.lcd_display_string("      :)", 2)
        print("Knopf BLAU gedrueckt!")
        GPIO.output(OutputPin2, 0)
        print("Richtig!")
        time.sleep(WaitTime)
        GPIO.output(OutputPin2, 1)
        SCORE = SCORE + 50
        Random_Sequency()
    elif((GPIO.input(InputPin) == 1) or (GPIO.input(InputPin3) == 1) or (GPIO.input(InputPin4) == 1)):
        lcd.lcd_display_string("<><><><><><><><>", 1)
        lcd.lcd_display_string("      :(", 2)
        print("Falsch!")
        GPIO.output(OutputPin, 0)
        GPIO.output(OutputPin3, 0)
        GPIO.output(OutputPin4, 0)
        time.sleep(WaitTime)
        GPIO.output(OutputPin, 1)
        GPIO.output(OutputPin3, 1)
        GPIO.output(OutputPin4, 1)
        SCORE = SCORE - 100
        Random_Sequency()
#Blau Ende

#Rot
def Red():
    global SCORE
    lcd.lcd_display_string("RED", 2)
    if(GPIO.input(InputPin) == 1):
        lcd.lcd_display_string("<><><><><><><><>", 1)
        lcd.lcd_display_string("      :)", 2)
        print("Knopf ROT gedrueckt!")
        GPIO.output(OutputPin, 0)
        print("Richtig!")
        time.sleep(WaitTime)
        GPIO.output(OutputPin, 1)
        SCORE = SCORE + 50
        Random_Sequency()
    elif((GPIO.input(InputPin2) == 1) or (GPIO.input(InputPin3) == 1) or (GPIO.input(InputPin4) == 1)):
        lcd.lcd_display_string("<><><><><><><><>", 1)
        lcd.lcd_display_string("      :(", 2)
        print("Falsch!")
        GPIO.output(OutputPin2, 0)
        GPIO.output(OutputPin3, 0)
        GPIO.output(OutputPin4, 0)
        time.sleep(WaitTime)
        GPIO.output(OutputPin2, 1)
        GPIO.output(OutputPin3, 1)
        GPIO.output(OutputPin4, 1)
        SCORE = SCORE - 100
        Random_Sequency()
#Rot Ende

#Gruen
def Green():
    global SCORE
    lcd.lcd_display_string("GREEN", 2)
    if(GPIO.input(InputPin3) == 1):
        lcd.lcd_display_string("<><><><><><><><>", 1)
        lcd.lcd_display_string("      :)", 2)
        print("Knopf GRUEN gedrueckt!")
        GPIO.output(OutputPin3, 0)
        print("Richtig!")
        time.sleep(WaitTime)
        GPIO.output(OutputPin3, 1)
        SCORE = SCORE + 50
        Random_Sequency()
    elif((GPIO.input(InputPin) == 1) or (GPIO.input(InputPin2) == 1) or (GPIO.input(InputPin4) == 1)):
        lcd.lcd_display_string("<><><><><><><><>", 1)
        lcd.lcd_display_string("      :(", 2)
        print("Falsch!")
        GPIO.output(OutputPin, 0)
        GPIO.output(OutputPin2, 0)
        GPIO.output(OutputPin4, 0)
        time.sleep(WaitTime)
        GPIO.output(OutputPin, 1)
        GPIO.output(OutputPin2, 1)
        GPIO.output(OutputPin4, 1)
        SCORE = SCORE - 100
        Random_Sequency()
#Gruen Ende

#Gelb
def Yellow():
    global SCORE
    lcd.lcd_display_string("YELLOW", 2)
    if(GPIO.input(InputPin4) == 1):
        lcd.lcd_display_string("<><><><><><><><>", 1)
        lcd.lcd_display_string("      :)", 2)
        print("Knopf GELB gedrueckt!")
        GPIO.output(OutputPin4, 0)
        print("Richtig!")
        time.sleep(WaitTime)
        GPIO.output(OutputPin4, 1)
        SCORE = SCORE + 50
        Random_Sequency()
    elif(GPIO.input(InputPin) == 1 or GPIO.input(InputPin3) == 1 or GPIO.input(InputPin2) == 1):
        lcd.lcd_display_string("<><><><><><><><>", 1)
        lcd.lcd_display_string("      :(", 2)
        print("Falsch!")
        GPIO.output(OutputPin, 0)
        GPIO.output(OutputPin2, 0)
        GPIO.output(OutputPin3, 0)
        time.sleep(WaitTime)
        GPIO.output(OutputPin, 1)
        GPIO.output(OutputPin2, 1)
        GPIO.output(OutputPin3, 1)
        SCORE = SCORE - 100
        Random_Sequency()

Sequenzen = [Blue(), Red(), Green(), Yellow()]

#Inititation des Spieles 
try:
    while True:
        Menu()
except KeyboardInterrupt:
    GPIO.cleanup() 
#Initiation des Spieles Ende

#ENGINE ENDE

麻烦的是,当你这样做的时候:

Sequenzen = [Blue(), Red(), Green(), Yellow()]

您当时正在调用这些函数,因此该列表仅包含它们的 结果 。正如已经指出的那样,这些函数中的 none return 任何东西,所以它们的结果都是 None,因此你的错误。

您真正想要做的是仅列出函数本身:

Sequenzen = [Blue, Red, Green, Yellow]

顺便说一句,函数可能 应该 return 一些东西:即更新的分数,而不是使用全局变量。

另请注意,这些函数都非常相似,可以简化为一个函数,该函数采用一个参数来指示按下了哪种颜色。