Could someone please help me with this level? My code:
# You need exactly 104 gold.
less = "Nimis"
more = "Non satis"
requiredGold = 104
# This function calculates the sum of all coin values.
def sumCoinValues(coins):
coinIndex = 0
totalValue = 0
# Iterate all coins.
while coinIndex < len(coins):
totalValue += coins[coinIndex].value
coinIndex += 1
return totalValue
def collectAllCoins():
item = hero.findNearest(hero.findItems())
while item:
hero.moveXY(item.pos.x, item.pos.y)
item = hero.findNearest(hero.findItems())
while True:
items = hero.findItems()
# Get the total value of coins.
goldAmount = sumCoinValues(items)
# If there are coins, then goldAmount isn't zero.
if goldAmount != 0:
# If goldAmount is less than requiredGold
if goldAmount <= requiredGold:
# Then say "Non satis".
hero.say("Non satis")
# If goldAmount is greater than requiredGold
if goldAmount >= requiredGold:
# Then say "Nimis".
hero.say("Nimis")
# If goldAmount is exactly equal to requiredGold
if goldAmount == requiredGold:
# Then collect all coins:
coin = hero.findItems()
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
pass
# Then collect all coins
Your code here makes no effort to collect all coins. You have a defined function collectAllCoins, perhaps use that?
(And sidenote- you’ve been extraordinarily impatient when it comes to answers. More messages never help, and it’s unreasonable to always expect a reply in under a day, let alone a couple of minutes.)
Okay. First of all, the “collectAllCoins()” function didn’t work. My guy collected all the coins, but it was more than 104, and I didn’t complete the level. Second of all, I wasn’t being impatient. I was just saying that, since it was the weekend, you guys didn’t have to reply to me.
# You need exactly 104 gold.
less = "Nimis"
more = "Non satis"
requiredGold = 104
# This function calculates the sum of all coin values.
def sumCoinValues(coins):
coinIndex = 0
totalValue = 0
# Iterate all coins.
while coinIndex < len(coins):
totalValue += coins[coinIndex].value
coinIndex += 1
return totalValue
def collectAllCoins():
item = hero.findNearest(hero.findItems())
while item:
hero.moveXY(item.pos.x, item.pos.y)
item = hero.findNearest(hero.findItems())
while True:
items = hero.findItems()
# Get the total value of coins.
goldAmount = sumCoinValues(items)
# If there are coins, then goldAmount isn't zero.
if goldAmount != 0:
# If goldAmount is less than requiredGold
if goldAmount <= requiredGold:
# Then say "Non satis".
hero.say("Non satis")
# If goldAmount is greater than requiredGold
if goldAmount >= requiredGold:
# Then say "Nimis".
hero.say("Nimis")
# If goldAmount is exactly equal to requiredGold
if goldAmount == requiredGold:
# Then collect all coins:
collectAllCoins()
pass
You are collecting the coins at the wrong time because because of these two statements:
and
You should not be checking if they are equal to. They need to be either greater than or less than. If you check if they are greater than or equal to (or less than or equal to), then you will collect the coins at the wrong time. Those two statements should be: