STEP 12: Let's use the user's input to set the width of the pen.
From drag out Pen Width. Drop it indented inside the a_key() event, under the line where you have asked the user for width.
Change the argument in .pen_width() from 4 to width.
Run your code and press the a key. Enter a number, then use the arrow keys to move the sprite!
To navigate the page using the TAB key, first press ESC to exit the code editor.
stage.set_background_color("black")
# sprite = codesters.Point(x, y)
sprite = codesters.Point(50, 100)
sprite.set_color("green")
sprite.pen_down()
def right_key():
sprite.move_right(20)
# add other actions...
stage.event_key("right", right_key)
def left_key():
sprite.move_left(20)
# add other actions...
stage.event_key("left", left_key)
def up_key():
sprite.move_up(20)
# add other actions...
stage.event_key("up", up_key)
def down_key():
sprite.move_down(20)
# add other actions...
stage.event_key("down", down_key)
my_list = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
def space_bar():
my_var = random.choice(my_list)
sprite.set_color(my_var)
# add other actions...
stage.event_key("space", space_bar)
def a_key():
pass # delete after adding indented code
width = int(sprite.ask("Enter a number from 1 to 10."))
# add other actions...
stage.event_key("a", a_key)
t = codesters.Teacher()
widths = t.get_parameters_for_function('pen_width')
width_line = t.find_text('pen_width')
try:
tval1a = int(width_line[0][0])
tval1b = t.get_indent_at_line(tval1a)
except:
tval1a = -1
tval1b = -1
try:
tval2 = widths[0][0]
except:
tval2 = "DNE"
t1 = TestObjective()
t1.add_success(tval2 == 'width' and tval1b == 4, "Great job!")
t1.add_failure(tval2 == "DNE", "Did you drag Pen Width into your a_key() event?")
t1.add_failure('"' in tval2 or "'" in tval2, "Oops! Did you put width in quotes?")
t1.add_failure(tval2 != 'width', "Did you change the argument in .pen_width()?")
t1.add_failure(tval1b > 4, "It looks like you indented too far. Make sure your Ask for Integer command is only 4 spaces in.")
t1.add_failure(tval1b < 4, "Make sure your Ask for Integer command is indented 4 spaces in your event block.")
tester = TestManager()
tester.add_test_list([t1])
tester.run_tests()
tester.display_first_feedback()
Are you already running a Codesters project in another tab or window?
Micro:bit can only connect to one web page at a time.
Try stopping other Codesters projects or closing
other tabs or windows that may be using your Micro:bit.
If that doesn't fix the problem try disconnecting your Micro:bit,
reloading this page, and reconnecting your Micro:bit.