Question
Can help me complete this code, somehow I cannot let the user answer the maths questions using 'a' and 'b' buttons. from microbit import *
Can help me complete this code, somehow I cannot let the user answer the maths questions using 'a' and 'b' buttons.
from microbit import * import random import music
step = 0 stop_steps = 0 step_operator = random.choice(["move_step", "math_step"]) correct_step = None
tick = Image("00000:" "00009:" "00090:" "90900:" "09000")
cross = Image("90009:" "09090:" "00900:" "09090:" "90009")
display.scroll("Welcome Savior, Start to make your first step!" , 65) sleep(1000) display.show(Image( "09900:" "99999:" "99999:" "90090:" "00000")) sleep(500) display.show(Image( "00000:" "09900:" "99999:" "99999:" "90090")) sleep(1000)
while stop_steps < 11: if accelerometer.was_gesture('shake'): stop_steps = stop_steps + 1 if step_operator == 'move_step': step += 1 display.show(Image( "09900:" "99999:" "99999:" "90090:" "00000")) sleep(500) display.show(Image( "00000:" "09900:" "99999:" "99999:" "90090")) correct_step = (Image.HAPPY) music.play(music.JUMP_UP) elif step_operator == 'math_step': step -= 1 correct_step = (Image.SAD) music.play(music.JUMP_DOWN) # Generate random numbers and operator for math game answer = 0 num1 = random.randint(0, 9) num2 = random.randint(0, 9) math_operator = random.choice(["+", "-"]) input_answer = 0 correct_answer = None if math_operator == '+': correct_answer = num1 + num2 elif math_operator == '-': correct_answer = num1 - num2 # Display the math question on the screen display.scroll(str(num1) + math_operator + str(num2) + " = ?") sleep(1000) display.clear()
# Wait for user input user_answer = int(input("Enter your answer: "))
# User input if input(button_a.was_pressed()): input_answer += 1 display.show(str(user_answer)) if button_b.was_pressed(): # Check if the answer is correct if input_answer == correct_answer: display.show(Image( "00000:" "09900:" "99999:" "99999:" "90090")) display.show(tick) answer += 1 else: display.show(Image( "90090:" "99999:" "99999:" "09900:" "00000")) display.show(cross) display.scroll("Game Over!") display.scroll("Your score: " + str(answer)) sleep(1000) display.clear() display.show(step_operator) sleep(1000) display.scroll('End of the road, The Turle Save. Congratulations!', 65) music.play(music.ENTERTAINER)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started