Python记分板全局变量noob
我收到一个错误,变量RedScore未定义。最初,我使用一个用于原始输入的键盘创建了它,并使一切正常工作,直到我尝试使用瞬时开关来控制一切 我收到的错误是在结尾的while语句附近。这并不一定意味着程序的其余部分正在运行。我只知道下面有个错误 有人能找出我的错误吗Python记分板全局变量noob,python,Python,我收到一个错误,变量RedScore未定义。最初,我使用一个用于原始输入的键盘创建了它,并使一切正常工作,直到我尝试使用瞬时开关来控制一切 我收到的错误是在结尾的while语句附近。这并不一定意味着程序的其余部分正在运行。我只知道下面有个错误 有人能找出我的错误吗 import RPi.GPIO as GPIO import time from time import sleep import Adafruit_CharLCD as LCD # Raspberry Pi configurati
import RPi.GPIO as GPIO
import time
from time import sleep
import Adafruit_CharLCD as LCD
# Raspberry Pi configuration:
lcd_rs = 27 # Change this to pin 21 on older revision Raspberry Pi's
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
lcd_red = 4
lcd_green = 17
lcd_blue = 7 # Pin 7 is CE1
# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2
# Initialize the LCD using the pins above.
lcd = LCD.Adafruit_RGBCharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,
lcd_d7, lcd_columns, lcd_rows, lcd_red, lcd_green, lcd_blue)
lcd.clear()
lcd.set_color(0.0,0.0,0.0)
GPIO.setmode(GPIO.BCM)
bluebutton = 5
blueresetbutton = 6
resetbutton = 13
redresetbutton = 19
redbutton = 26
global BlueScore
global RedScore
GPIO.setup(bluebutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(blueresetbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(resetbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(redresetbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(redbutton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def bluebutton(channel):
global BlueScore
BlueScore = BlueScore + 1
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
def blueresetbutton(channel):
global BlueScore
BlueScore = 11
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
def resetbutton(channel):
global RedScore
global BlueScore
RedScore = BlueScore = 0
lcd.clear()
lcd.set_color(1.0, 1.0, 0.0)
lcd.message('NEW GAME!!!\nRed: '+str(RedScore)+'Blue:'+str(BlueScore))
def redresetbutton(channel):
global RedScore
RedScore = 11
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
def redbutton(channel):
global RedScore
RedScore = Redscore + 1
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('Red: '+str(RedScore)+' Blue:'+str(BlueScore))
GPIO.add_event_detect(5, GPIO.FALLING, callback=bluebutton, bouncetime=300)
GPIO.add_event_detect(6, GPIO.FALLING, callback=blueresetbutton, bouncetime=300)
GPIO.add_event_detect(13, GPIO.FALLING, callback=resetbutton, bouncetime=300)
GPIO.add_event_detect(19, GPIO.FALLING, callback=redresetbutton, bouncetime=300)
GPIO.add_event_detect(26, GPIO.FALLING, callback=redbutton, bouncetime=300)
while RedScore <= 21 and BlueScore <= 21:
if RedScore == 21:
lcd.clear()
lcd.set_color(1.0, 0.0, 0.0)
lcd.message('RED WINS!!!')
sleep(2)
RedScore = BlueScore = 0
lcd.clear()
lcd.set_color(1.0, 1.0, 0.0)
lcd.message('NEW GAME!!!\nRed: '+str(RedScore)+' Blue:'+str(BlueScore))
continue
elif BlueScore == 21:
lcd.clear()
lcd.set_color(0.0, 0.0, 1.0)
lcd.message('BLUE WINS!!!')
sleep(2)
BlueScore = RedScore = 0
lcd.clear()
lcd.set_color(1.0, 1.0, 0.0)
lcd.message('NEW GAME!!!\nRed: '+str(RedScore)+' Blue:'+str(BlueScore))
continue
else:
sleep(1)
将RPi.GPIO导入为GPIO
导入时间
从时间上导入睡眠
进口Adafruit_CharLCD作为LCD
#树莓Pi配置:
lcd_rs=27#在旧版覆盆子Pi上将此更改为插脚21
lcd_en=22
lcd_d4=25
lcd_d5=24
lcd_d6=23
lcd_d7=18
lcd_红色=4
lcd_绿色=17
lcd_蓝色=7#引脚7为CE1
#定义16x2 LCD的LCD列和行大小。
lcd_列=16
lcd_行=2
#使用上面的引脚初始化LCD。
lcd=lcd.Adafruit_RGBCharLCD(lcd_rs,lcd_en,lcd_d4,lcd_d5,lcd_d6,
lcd_d7、lcd_列、lcd_行、lcd_红、lcd_绿、lcd_蓝)
lcd.clear()
lcd.set_颜色(0.0,0.0,0.0)
GPIO.setmode(GPIO.BCM)
蓝色按钮=5
blueresetbutton=6
重置按钮=13
redresetbutton=19
红色按钮=26
全球蓝核
全球红分
GPIO.setup(蓝色按钮,GPIO.IN,上下拉=上下拉)
GPIO.setup(blueresetbutton,GPIO.IN,pull\u up\u down=GPIO.PUD\u up)
GPIO.setup(重置按钮,GPIO.IN,上拉下拉=GPIO.PUD上拉)
GPIO.setup(redresetbutton,GPIO.IN,pull\u up\u down=GPIO.PUD\u up)
GPIO.setup(红色按钮,GPIO.IN,上拉下拉=GPIO.PUD上拉)
def蓝色按钮(通道):
全球蓝核
蓝芯=蓝芯+1
lcd.clear()
lcd.set_颜色(1.0,0.0,0.0)
消息('Red:'+str(RedScore)+'Blue:'+str(BlueScore))
def BlueReset按钮(通道):
全球蓝核
蓝核=11
lcd.clear()
lcd.set_颜色(1.0,0.0,0.0)
消息('Red:'+str(RedScore)+'Blue:'+str(BlueScore))
def重置按钮(通道):
全球红分
全球蓝核
RedScore=BlueScore=0
lcd.clear()
lcd.设置颜色(1.0、1.0、0.0)
lcd.message('新游戏!!!\nRed:'+str(红分)+'Blue:'+str(蓝核))
def红色重置按钮(通道):
全球红分
红分数=11
lcd.clear()
lcd.set_颜色(1.0,0.0,0.0)
消息('Red:'+str(RedScore)+'Blue:'+str(BlueScore))
def红色按钮(通道):
全球红分
RedScore=RedScore+1
lcd.clear()
lcd.set_颜色(1.0,0.0,0.0)
消息('Red:'+str(RedScore)+'Blue:'+str(BlueScore))
GPIO.add_event_detect(5,GPIO.FALLING,callback=bluebutton,bouncetime=300)
GPIO.add\u event\u detect(6,GPIO.FALLING,callback=blueresetbutton,bouncetime=300)
GPIO.add\u event\u detect(13,GPIO.FALLING,callback=resetbutton,bouncetime=300)
GPIO.add\u event\u detect(19,GPIO.FALLING,callback=redresetbutton,bouncetime=300)
GPIO.add\u event\u detect(26,GPIO.FALLING,callback=redbutton,bouncetime=300)
while RedScore在while循环之前为RedScore指定一个值。例如
RedScore = 0
它需要全局的还是只需要一个值?BlueScore也一样?是的,BlueScore也需要一个值。如果RedScore和BlueScore没有为表达式指定任何值,则while循环无法计算表达式。你已经将他们视为全球人,因此没有更多的事情要做。