Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Dungeon Game Hit points

Sandy Leon
Sandy Leon
2,246 Points

Is there something I'm missing?

Hello everyone, I am once again stuck on a coding challenge. According to the examples provided my code seems to do what is being asked, but I am still getting the problem wrong. Thanks in advance.

movement.py
# EXAMPLES:
# move((1, 1, 10), (-1, 0)) => (0, 1, 10)
# move((0, 1, 10), (-1, 0)) => (0, 1, 5)
# move((0, 9, 5), (0, 1)) => (0, 9, 0)


def move(player, direction):
    x, y, hp = player
    x1,y1 = direction
    if hp > 0:
        x += x1
        y += y1
        if x == -1:
            x = 0
            hp -= 5

        if y == -1:
            y = 0
            hp -= 5

        if x == 10:
             x = 9
            hp -= 5

        if y == 10:
            y = 9
            hp -= 5        

    return x, y, hp

4 Answers

Sandy Leon
Sandy Leon
2,246 Points

Hey Alex, thank you for taking the time to look at my problem. I tried changing the first if statement to 'if hp >= 0' and even 'if True' but those still don't work, any suggestions?

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Sandy,

Try removing the if hp ... statement completely and then dedent all the rest of the the code that was inside that if block by four spaces. That's what I did after copying and pasting your code into the challenge and it passed.

Cheers

Alex

Sandy Leon
Sandy Leon
2,246 Points

I tried doing exactly that but it keeps saying it's wrong, this is exactly what I have. Thank you again in advance.

def move(player, direction): x, y, hp = player x1,y1 = direction
x += x1 y += y1 if x == -1: x = 0 hp -= 5

if y == -1:
    y = 0
    hp -= 5

if x == 10:
     x = 9
    hp -= 5

if y == 10:
    y = 9
    hp -= 5        

return x, y, hp
Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Sandy,

Can you edit your code example to show it properly formatted with markdown? That will help to reveal any subtle bugs, especially with regard to indentation. I can see one such bug in the portion of code that is in a code block:

  • in your x == 10 block your x assignment and hp augmented assignment have different indentation. This will cause Python to crash with an IndentationError.

Cheers

Alex

Sandy Leon
Sandy Leon
2,246 Points

Whoops, sorry about that.

# EXAMPLES:
# move((1, 1, 10), (-1, 0)) => (0, 1, 10)
# move((0, 1, 10), (-1, 0)) => (0, 1, 5)
# move((0, 9, 5), (0, 1)) => (0, 9, 0)


def move(player, direction):
    x, y, hp = player
    x1,y1 = direction
    x += x1
    y += y1   
    if x == -1:
        x = 0
        hp -= 5
    if y == -1:
        y = 0
        hp -= 5
    if x == 10:
        x = 9
        hp -= 5
    if y == 10:
        y = 9
        hp -= 5        

    return x, y, hp
Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Sandy,

You didn't mention if your code is now passing. I just copy-pasted on my end and it did pass the challenge.

Happy coding,

Cheers

Alex

Sandy Leon
Sandy Leon
2,246 Points

I tried that same code yesterday and it didn't work, but I just copy and pasted what was seemingly the exact same code that I had in the workspace and now it's working, I don't know how, but I'll take it. Thanks for the help, Alex.