[英]'NoneType' Object is not callable. Python
我试图用Python为树莓派编写一个小游戏(例如“ 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,因此会出现错误。
您实际上想要做的是仅列出函数本身的列表:
Sequenzen = [Blue, Red, Green, Yellow]
顺便说一句,这些函数可能应该返回一些东西:即更新的得分,而不是使用全局变量。
还要注意,这些功能非常相似,可以简化为一个带有参数的功能,以指示按下了哪种颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.