you need to put something after your âifâ statement on line 12.
currently, your code does this in this order :
- Finds the nearest enemy
- checks if that nearest enemy is a munchkin or a thrower
- if it is a munchkin or thrower, attack it
- finds the nearest item
- checks if that item is a coin or a gem
Notice how it looks a bit incomplete at the end? You are checking for an item and then doing nothing from there on, you need to follow up your âifâ statement on line 12 with something.
(Checks previous levels if you arenât sure on how to move to an itemâs X and Y coordinates)
# Ogres are attacking a nearby settlement!
# Be careful, though, for the ogres have sown the ground with poison.
# Gather coins and defeat the ogres, but avoid the burls and poison!
while True:
enemy = hero.findNearestEnemy()
if enemy.type == "munchkin" or enemy.type == "thrower":
hero.attack(enemy)
item = hero.findNearestItem()
# Check the item type to make sure the hero doesn't pick up poison!
# Look for types: 'gem' and 'coin'
if item.type == "coin" or item.type == "gem":
hero.moveXY(item.pos.x, item.pos.y)
is this better?
Iâm not sure, is it? What errors are you getting now?
while True:
item = hero.findNearestItem()
if item.type != "gem":
hero.moveXY(item.pos.x, item.pos.y)
# Find the nearest enemy.
# Attack it if it exists and its type isn't "burl".
enemy = hero.findNearestEnemy()
if enemy.type != "burl":
hero.attack(enemy)
pass
this is my code
did you mean to put â!â?
if item AND item.type != âgemâ:
also
if enemy AND enemy.type != âburlâ:
I tried doing that it didnât work for Salted Earth. Are you doing Python?
This personâs post was 2 years ago. Donât you think that George_Huchins_Hun would have asked for more help if he/she could? Please do not revive dead topics.
Please do not think up rules.
Hi I need help with my code. This is what I have so far,â
while True:
enemy = hero.findNearestEnemy()
if enemy.type == "munchkin" or enemy.type == "thrower":
hero.attack(enemy)
item = hero.findNearestItem()
# Check the item type to make sure the hero doesn't pick up poison!
itemPosition = item.pos
# Put the X and Y coordinates of the item into variables.
itemX = itemPosition.x
itemY = itemPosition.y
hero.moveXY(itemX, itemY)
gem = hero.findNearestItem()
coin = hero.findNearestItem()
# If the item's type is "gem" or "coin":
if item.type == coin or item.type == gem:
# Then move and pick it up:
hero.moveXY(itemX, itemY)
plz help me to improve my skills
You should remove the lines from 14 to 17 and the lines 22, 23. After that, it should work fine.
but then its just says coin not defined. And when I say poison or bottle its keep puting the labels on the coin. And how do you make the hero move away from the poison?
Compare this line:
if enemy.type == "munchkin" or enemy.type == "thrower":
with
if item.type == coin or item.type == gem:
The top one sounds as though itâs working - whatâs the difference?
The two statements in my last post arenât quite the same! Oneâs got " " in, the other one doesnâtâŚ
what do you mean? (20)
Try putting âcoinâ and âgemâ, rather than coin and gem!