WARNING: This program has a logic error. It doesn't cause an error message, but something isn't quite right.
modulo% calculates the remainder of division! This program splits up candy among a group of friends and calculates how much candy is leftover.
Click Run and enter a number between 1 and 10. Look at the stage. What's wrong with the output?
Fix the program by replacing the division operator / with the modulo operator %.
To navigate the page using the TAB key, first press ESC to exit the code editor.
# sprite = codesters.Text("text", x, y)
warning = codesters.Text("There must be 10 or less people!", 0, 200, "red")
warning.hide()
amount_of_candy = 10
amount_of_people = int(stage.ask("How many people are sharing? Enter a number between 1 and 10!"))
leftovers = amount_of_candy/amount_of_people
if leftovers != 0:
# sprite = codesters.Text("text", x, y)
sprite = codesters.Text("There are " + str(leftovers) + " candies left over!", 0, 150)
while amount_of_people > amount_of_candy:
warning.show()
stage.wait(1)
amount_of_people = int(stage.ask("How many people are sharing?"))
warning.hide()
t = codesters.Teacher()
div_op = t.find_text("/")
mod_op = t.find_text("%")
try:
tval1 = div_op[0][1].replace(" ", "")
except:
tval1 = "DNE"
try:
tval2 = mod_op[0][1].replace(" ", "")
except:
tval2 = "DNE"
if tval2 != "leftovers=amount_of_candy%amount_of_people":
warning.set_y(0)
true_leftovers = amount_of_candy % amount_of_people
warning.set_text("Hmm...I see there are really " + str(true_leftovers) + " candies left over!")
warning.show()
t1 = TestObjective()
t1.add_success(tval1 == "DNE" and tval2 == "leftovers=amount_of_candy%amount_of_people", "Great job!")
t1.add_failure(tval1 != "DNE", "Did you replace the / with %?")
t1.add_failure(tval1 == "DNE" and tval2 == "DNE", "Oops! Did you delete provided code?")
t1.add_failure(tval1 != "DNE" and tval2 != "leftovers=amount_of_candy%amount_of_people", "Did you change / to % correctly?")
tester = TestManager()
tester.add_test_list([t1])
tester.run_tests()
tester.display_first_feedback()
people = []
candy = []
candy_person_ratio = amount_of_candy//amount_of_people
for counter in range(amount_of_people):
sprite = codesters.Sprite("person" + str(random.randint(1, 16)), random.randint(-230, 230), 600)
sprite.set_size(.8)
sprite.set_bottom(-250)
people.append(sprite)
for counter in range(amount_of_candy):
sprite = codesters.Sprite("candy", random.randint(-230, 230), 600)
sprite.set_size(.8)
sprite.set_rotation(random.randint(-90, 90))
sprite.set_y(random.randint(0, 250))
candy.append(sprite)
# for index, sprite in enumerate(people):
# candy[index].set_speed(6)
# candy[index].glide_to(sprite.get_x(), sprite.get_y())
# stage.wait(.1)
# stage.remove_sprite(candy[index])
# stage.remove_sprite(sprite)
candy_index = 0
for index, sprite in enumerate(people):
for counter in range(candy_person_ratio):
candy[candy_index].set_speed(6)
candy[candy_index].glide_to(sprite.get_x(), sprite.get_y())
stage.wait(.1)
stage.remove_sprite(candy[candy_index])
candy_index += 1
stage.remove_sprite(sprite)
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.