Python: Hoarding Gold

here is my code:

Collect 25 gold, and then tell Naria the total.

Use break to stop collecting when totalGold >= 25.

totalGold = 0
while True:
coin = hero.findNearestItem()
if coin and totalGold >= 25:
# Pick up the coin.
hero.moveXY(coin.pos.x, coin.pos.y)
# Add the coin’s value to totalGold.
# Get its value with: coin.value
totalGold += coin.value
pass
if totalGold >= 25:
# This breaks out of the loop to run code at the bottom.
# The loop ends, code after the loop will run.
break

Done collecting gold!

hero.moveXY(58, 33)

Go to Naria and say how much gold you collected.

hero.say(totalGold)

Welcome‌ ‌welcome‌ ‌to‌ ‌the‌ ‌forum!‌ ‌It‌ ‌is‌ ‌a‌ ‌lovely‌ ‌place‌ ‌where‌ ‌you‌ ‌can‌ ‌share‌ ‌all‌ ‌kinds‌ ‌of‌ stuff‌ ‌(appropriate‌ ‌of‌ ‌course),‌ ‌share‌ ‌bugs,‌ ‌and‌ ‌even‌ ‌get‌ ‌assistance‌ ‌for‌ ‌code!‌ ‌We‌ ‌suggest‌ ‌that‌ ‌you‌ ‌review‌ ‌this‌‌ topic‌ ‌which‌ ‌shows‌ ‌all‌ ‌essentials‌ ‌of‌ ‌this‌ ‌board!‌ ‌And‌ ‌we‌ ‌suggest‌ ‌you‌ ‌review‌ this topic‌ ‌which‌ ‌shows‌ ‌how‌ ‌to‌ ‌post‌ ‌your‌ ‌code‌ ‌correctly!‌ ‌Thanks!!‌ ‌ :partying_face: :partying_face:

Please post your code correctly :slight_smile:
Lydia

1 Like

to post your code, press a button that looks like

</>
1 Like

ok like this?

Collect 25 gold, and then tell Naria the total.

Use break to stop collecting when totalGold >= 25.

totalGold = 0
while True:
coin = hero.findNearestItem()
if coin and totalGold >= 25:
# Pick up the coin.
hero.moveXY(coin.pos.x, coin.pos.y)
# Add the coin’s value to totalGold.
# Get its value with: coin.value
totalGold += coin.value
pass
if totalGold >= 25:
# This breaks out of the loop to run code at the bottom.
# The loop ends, code after the loop will run.
break

Done collecting gold!

hero.moveXY(58, 33)

Go to Naria and say how much gold you collected.

hero.say(totalGold)

type or paste code here
1 Like

You did it partially, you need to put the code here:

1 Like

Try putting the break inside of here

1 Like

yes it is like that already

2 Likes

Sorry it is hard for me to read unformatted code

1 Like

Found the problem instead of this

do this

totalGold = totalGold + coin.value

Your code didn’t work since you didn’t define totalGold. I hope this helps.

1 Like

@darklord999, shouldn’t totalGold be less than 25? If your gold is 0, the code won’t run at all. Also, keeping totalGold += coin.value is fine, it still adds the coin’s value.

2 Likes

He is right it can’t be greater than 30 but he has to get 25 but if you have 24 and the nearest coin gives you 2 gold you get stuck and you code won’t run. But if you put that greater sign it is saying it okay to get 1 more coin to get 25 and less than 30.

2 Likes

Hey @darklord999!. I’m not going to provide any answers because it looks like multiple users have got it from here. But I would like to welcome you to the forum! Like @Lydia_Song said make sure to check out our rules located in her first post in the hyperlink.

Cya around!

:wolf::wolf::wolf:

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.