使用Raspberry Pi 3B +,以及Python新手。我知道我使用全局变量,并且想知道我应该使用什么。对不起,如果我没有把它缩小,但我根本不知道错误是什么,所以我无法重新创建它...至少回答错误是什么,如果你知道我怎么能请修理它。
代码在这里:
from gpiozero import LED, Button
from time import sleep
from random import uniform
from sys import exit
from signal import pause
print('Objective: Push the button as fast as you can when the LED goes off.')
print('First to five wins.')
left_name = input('Left player name is? ')
right_name = input('Right player name is? ')
led = LED(4)
leftscore = 0
rightscore = 0
addnum = 1
winnum = 5
right_button = Button(15)
left_button = Button(14)
def scoreleft(ls):
ls = ls + addnum
print(left_name + ' has ' + str(ls) + ' points!')
if ls == winnum:
print(left_name + ' won the whole game!')
exit()
else:
print('No one has won yet...')
return ls
def pressedleft(button):
if button.pin.number == 14:
global leftscore
print(left_name + ' won this round!')
scoreleft(leftscore)
led.off()
def scoreright(rs):
rs = rs + addnum
print(right_name + ' has ' + str(rs) + ' points!')
if rightscore == winnum:
print(right_name + ' won the whole game!')
exit()
else:
print('No one has won yet...')
return rs
def pressedright(button):
if button.pin.number == 15:
global rightscore
print(right_name + ' won this round!')
scoreright(rightscore)
led.off()
while True:
led.off()
sleep(uniform(5, 10))
led.on()
right_button.when_pressed = pressedright(right_button)
left_button.when_pressed = pressedleft(left_button)
sleep(3)
Shell错误:(从未触摸过按钮。没有错误,只是文本。它不应该说每次都按下并且每次都有相同的分数。)
Objective: Push the button as fast as you can when the LED goes off.
First to five wins.
Left player name is? Left player
Right player name is? Right player
Right player won this round!
Right player has 1 points!
No one has won yet...
Left player won this round!
Left player has 1 points!
No one has won yet...
Right player won this round!
Right player has 1 points!
No one has won yet...
Left player won this round!
Left player has 1 points!
No one has won yet...
请使用答案功能而不是评论,这样我就可以给你声誉。感谢您阅读本文,我们将非常感谢您的回答。你很酷。