我在 python 中的代码有什么问题?

What is wrong with my code in python?

在我的代码中,它应该是一个被操纵的魔法 8 球。按 A 应显示 "Yes!",按 B 应显示 "No." 但是每次,它都显示 "Yes!" 而没有按下任何按钮。

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))

它没有显示任何错误消息,每次我摇动时它只是显示 "Yes!"。顺便说一下,"Yes!" 不在答案数组中,只有 "Yes",而且我总是看到 !.

没有更多上下文,只能假设问题出在哪里。 确保 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.

谢谢深空! 经过一些润色,这是我的最终代码。 对不起,我没有说清楚,但这是一个可以操纵的神奇 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))