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 trialAndy McDonald
Python Development Techdegree Graduate 13,801 Pointstrying 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.
import os
def absolute(path, root):
while path.startswith('/') is True:
return path
else:
return (f"/{path}")
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsHey Andy McDonald, you have the correct ideas, but some semantics are incorrect.
- the proper comparison with
True
is ==. Usingis
means checking something is exactlyTrue
as inbool int 1
. Moreover, since the methodstartswith()
returns abool
, the comparison toTrue
is unnecessary. The βtruthyβ value of the method result is sufficient - use
if
instead ofwhile
since the comparison is done once and not repeatedly until notTrue
- in the
else
return value,{root}
needs to prefix thepath
. No / is needed sinceroot
ends in β/β.
Post back if you need more help. Good luck!!!