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 for File Systems Navigation Absolutely

Andy McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy McDonald
Python Development Techdegree Graduate 13,801 Points

trying to create funtion that prepends / to a file path that is not absoloute

heres my code: import os

def absolute(path, root): while path.startswith('/') is True: return path else: return (f"/{path}")

the challenge is: https://teamtreehouse.com/library/python-for-file-systems/navigation/absolutely

I dont understand why my code isnt working. I tested it and everything seems to work correctly but the quiz will not allow me to move forward.

absolute.py
import os

def absolute(path, root):
    while path.startswith('/') is True:
        return path
    else:
        return (f"/{path}")

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hey Andy McDonald, you have the correct ideas, but some semantics are incorrect.

  • the proper comparison with True is ==. Using is means checking something is exactly True as in bool int 1. Moreover, since the method startswith() returns a bool, the comparison to True is unnecessary. The β€œtruthy” value of the method result is sufficient
  • use if instead of while since the comparison is done once and not repeatedly until not True
  • in the else return value, {root} needs to prefix the path. No / is needed since root ends in β€œ/β€œ.

Post back if you need more help. Good luck!!!