Помогите разобраться. Написал код, понимаю что нужно написать функцию takeItem, но как это и где сделать не понимаю. Пробовал писать по разному - не получается. На сколько я понимаю тело функции изменять нельзя. Первую функцию так же нельзя менять. Во второй нужен один аргумент. Так в какую часть тогда вставлять?
# Собери монеты и сбеги, пока бурл не нашёл тебя.
# This function allows your hero take an item.
def takeItem(item):
hero.moveXY(item.pos.x, item.pos.y)
# Напиши функцию `checkTakeRun` с одним параметром.
# If the item exists, use "takeItem" function to take it.
# Двигайся на исходную точку (зелёная отметка), вне зависимости от наличия предмета.
def checkTakeRun(coin):
hero.moveXY(40, 12)
# Не меняй этот код.
while True:
hero.moveXY(16, 56)
coin = hero.findNearestItem()
checkTakeRun(coin)
hero.moveXY(64, 56)
coin = hero.findNearestItem()
checkTakeRun(coin)
type or paste code here
Итак, у вас уже есть функция takeItem(). Вам необходимо включить takeItem() в функцию checkTakeRun(). Вам нужно найти элемент, взять его, если он есть, а затем вернуться в функцию checkTakeRun()
Okay, so you already have the function takeItem(). You need to incorporate the takeItem() into the checkTakeRun() function. You need to find an item, take the item if there is an item and then move back in the checkTakeRun() function
Итак, вам нужно сначала определить элемент. Например, item = hero.findNearestItem И если есть предмет, вы должны использовать функцию takeItem (item), чтобы взять предмет и бегать к (40,12).
Если предмета нет, просто бегите к (40,12).
So you have to first define item. Like item = hero.findNearestItem And if there is an item, you would use takeItem(item) function to take the item, and run to (40,12).
If there isn’t an item, just run to (40,12).
# Собери монеты и сбеги, пока бурл не нашёл тебя.
# This function allows your hero take an item.
def takeItem(item):
hero.moveXY(item.pos.x, item.pos.y)
# Напиши функцию `checkTakeRun` с одним параметром.
# If the item exists, use "takeItem" function to take it.
# Двигайся на исходную точку (зелёная отметка), вне зависимости от наличия предмета.
def checkTakeRun(coin):
hero.moveXY(40, 12)
coin = hero.findNearestItem()
if coin:
takeItem(coin)
else:
hero.moveXY(40, 12)
# Не меняй этот код.
while True:
hero.moveXY(16, 56)
coin = hero.findNearestItem()
checkTakeRun(coin)
hero.moveXY(64, 56)
coin = hero.findNearestItem()
checkTakeRun(coin)
type or paste code here
Проблема в том, что вы не определяете переменную “item”. Вы должны сделать item = hero.findNearestItem после def takeItem (item)Вы также должны добавить if item: hero.moveXY (item.pos.x, item.pos.y)
# Собери монеты и сбеги, пока бурл не нашёл тебя.
# This function allows your hero take an item.
def takeItem(item):
#вставить item = hero.findNearestItem Вот
#if item:
hero.moveXY(item.pos.x, item.pos.y) #сделать отступ в этой строке
The problem is that you are not defining the variable “item”. You must do item = hero.findNearestItem after def takeItem(item) You should also add if item: hero.moveXY(item.pos.x, item.pos.y)
До сих пор не могу решить задачу. Не понимаю почему не работает?
# Собери монеты и сбеги, пока бурл не нашёл тебя.
# This function allows your hero take an item.
def takeItem(item):
hero.moveXY(item.pos.x, item.pos.y)
# Напиши функцию `checkTakeRun` с одним параметром.
# If the item exists, use "takeItem" function to take it.
# Двигайся на исходную точку (зелёная отметка), вне зависимости от наличия предмета.
def checkTakeRun(item):
hero.moveXY(40, 12)
item = hero.findNearestItem()
if item:
hero.moveXY(40, 12)
else:
hero.moveXY(40, 12)
# Не меняй этот код.
while True:
hero.moveXY(16, 56)
coin = hero.findNearestItem()
checkTakeRun(coin)
hero.moveXY(64, 56)
coin = hero.findNearestItem()
checkTakeRun(coin)
type or paste code here
Не работает. Я что то не так делаю. Третий день не могу решить, уже голова не соображает.
# Собери монеты и сбеги, пока бурл не нашёл тебя.
# This function allows your hero take an item.
def takeItem(item):
hero.moveXY(item.pos.x, item.pos.y)
# Напиши функцию `checkTakeRun` с одним параметром.
# If the item exists, use "takeItem" function to take it.
# Двигайся на исходную точку (зелёная отметка), вне зависимости от наличия предмета.
def checkTakeRun(item):
hero.moveXY(40, 12)
item = hero.findNearestItem()
if item:
takeItem(item)
hero.moveXY(40, 12)
else:
hero.moveXY(40, 12)
# Не меняй этот код.
while True:
hero.moveXY(16, 56)
coin = hero.findNearestItem()
checkTakeRun(coin)
hero.moveXY(64, 56)
coin = hero.findNearestItem()
checkTakeRun(coin)type or paste code here
Спасибо большое! Меня интересует почему я не понял с первого раза? В сети нашел комментарий что эта задача ошибочно поставлена в таком порядке, ее нужно ставить позже. Так ли это? Еще меня интересует вот эта строка def takeItem(item): hero.moveXY(item.pos.x, item.pos.y)Preformatted text Почему тут нет цифр? Почему нет конкретики? Меня это вводит в заблуждение.
Рад, что вы прошли уровень!
Чтобы ответить на ваш вопрос, в нем нет цифр, потому что он достаточно конкретен, чтобы указать вашему герою, куда идти. Но поскольку элемент может быть где угодно, положение элемента должно измениться. Было бы очень сложно предсказать, где будет находиться позиция. Так что проще сначала идентифицировать элемент и просто использовать moveXY для перехода к позиции элемента.
Glad you solve the level!
To answer your question, it has no numbers because it is specific enough to tell your hero where to go. But because an item can be anywhere, the item’s position has to change. It would be very difficult to predict where the item’s positions are going to be. So it is easier just to identify item first and just use moveXY to move to the item’s pos.
Lydia