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
Andy 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,468 PointsHey Andy McDonald, you have the correct ideas, but some semantics are incorrect.
- the proper comparison with
Trueis ==. Usingismeans checking something is exactlyTrueas inbool int 1. Moreover, since the methodstartswith()returns abool, the comparison toTrueis unnecessary. The “truthy” value of the method result is sufficient - use
ifinstead ofwhilesince the comparison is done once and not repeatedly until notTrue - in the
elsereturn value,{root}needs to prefix thepath. No / is needed sincerootends in “/“.
Post back if you need more help. Good luck!!!