Having null issues <solved>


#1

I just started coding nine days ago, and I thought I was getting used to it, but I’m still having fundamental issues. Below is a copy of the code that I wrote, it’s giving me a null error on the distance = hero.distanceTo(item) line. Can anyone explain this to me because I am having a very hard time figuring this out?

The artillery uses coins as a target.

You’ll be the rangefinder for the artillery.

Write the function.

def coinDistance():
# Find the nearest coin,
item = hero.findNearestItem()
# If there is a coin, return the distance to it.
distance = hero.distanceTo(item)
if item:
return distance
# Else, return 0 (zero).
else:
return 0
pass


#2

If there are no items in game yet this function returns null instead of an array of items. You can check for nulls by using an if-then-else statement before calling the distance = code segment.


#3

Ok, I got it. If I am calling the distance to an item that hasn’t appeared yet, it will be equal to null. Thank you for your help.


#4

@Nadario_Seays you are welcome.

Also, when you edit the title, there should actually be a category called Solved that you can choose. This helps categorize solutions into a single area for others who might have similar issues.

-HW


#5

I looked but didn’t find a solved category.


#6

@Nadario_Seays you are right, how odd. there is a “solved” category that is available in the forums if you select the category listing. I wonder if this is only something that can be chosen by the forum moderators?

Sorry about that, I changed your title back to adding the <solved> to is as this helps the rest of us skip the post if we know its resolved.

Thank you for trying to mark it as solved.


#7

No worries my friend, no worries!!