在我的代码中,它应该是一个被操纵的魔法8球。按A应显示“是!”,按B应显示“否” 但每次都显示“是的!”没有按任何按钮。
from microbit import *
import random
frameq = Image("00000:""00000:""00900:""00000:""00000")
framew = Image("00000:""09990:""09390:""09990:""00000")
framee = Image("99999:""93339:""93339:""93339:""99999")
framer = Image("33333:""33333:""33333:""33333:""33333")
framet = Image("22222:""22222:""22222:""22222:""22222")
framey = Image("11111:""11111:""11111:""11111:""11111")
frames = [frameq, framew, framee, framer, framet, framey]
answers = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it"
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
]
apress = False
bpress = False
while True:
if button_a.is_pressed:
bpress = False
apress = True
elif button_b.is_pressed:
apress = False
bpress = True
display.show("8")
if accelerometer.was_gesture("shake") and apress is True:
display.clear()
display.show(frames, loop=False, delay=250)
sleep(1000)
display.show("Yes!")
apress = False
elif accelerometer.was_gesture("shake") and bpress is True:
display.clear()
display.show(frames, loop=False, delay=250)
sleep(1000)
display.show("No.")
bpress = False
elif accelerometer.was_gesture("shake"):
display.clear()
display.show(frames, loop=False, delay=250)
sleep(1000)
display.scroll(random.choice(answers))
它没有显示错误消息,只显示“是!”每一次我都动摇。顺便说一句,“是的!”不在答案数组中,只有“是”,我总是看到!。
答案 0 :(得分:1)
如果没有更多的背景,人们只能假设问题是什么。
确保is_pressed
不是函数:
if button_a.is_pressed:
bpress = False
apress = True
elif button_b.is_pressed:
apress = False
bpress = True
如果is_pressed
是一个函数,那么button_a.is_pressed
将始终为True
因此apress
始终为True
因此您将总是打印'Yes!'
。
尝试将上述代码更改为
if button_a.is_pressed():
bpress = False
apress = True
elif button_b.is_pressed():
apress = False
bpress = True
否则,您编程调试。在不同的执行路径中添加print语句,看看每个if
语句是True
的原因。
答案 1 :(得分:0)
谢谢DeepSpace! 经过一些修饰,这是我的最终代码。 对不起,我没有说清楚,但这是一个可以操纵的神奇的8球。按A或B. 这是最终的代码:
from microbit import *
import random
frameq = Image("00000:""00000:""00900:""00000:""00000")
framew = Image("00000:""09990:""09390:""09990:""00000")
framee = Image("99999:""93339:""93339:""93339:""99999")
framer = Image("33333:""33333:""33333:""33333:""33333")
framet = Image("22222:""22222:""22222:""22222:""22222")
framey = Image("11111:""11111:""11111:""11111:""11111")
frames = [frameq, framew, framee, framer, framet, framey]
answers = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it"
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
]
apress = False
bpress = False
while True:
if button_a.is_pressed():
bpress = False
apress = True
elif button_b.is_pressed():
apress = False
bpress = True
display.show("8")
if accelerometer.was_gesture("shake"):
if apress:
display.clear()
display.show(frames, loop=False, delay=250)
sleep(1000)
display.show("Yes!")
apress = False
elif bpress:
display.clear()
display.show(frames, loop=False, delay=250)
sleep(1000)
display.show("No.")
bpress = False
else:
display.clear()
display.show(frames, loop=False, delay=250)
sleep(1000)
display.scroll(random.choice(answers))