-1

在我的代码中,它应该是一个被操纵的魔术 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))

它没有显示错误消息,它只显示“是!” 每次我摇晃。顺便说一句,“是的!” 不在答案数组中,只有“是”,我总是看到!

4

2 回答 2

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

否则,调试您的程序。在不同的执行路径中添加打印语句,看看是什么导致每个if语句成为True.

于 2018-03-18T13:23:03.360 回答
0

谢谢深空!经过一些修饰,这是我的最终代码。对不起,我没有说清楚,但这是一个可以操纵的魔术 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))
于 2018-03-27T00:33:42.810 回答