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
Jonathan Beard
1,681 PointsDidn't get the right return values for some paths(absolute.py challenge 2/2)
I have made several different types of functions to try and solve this issue, but I guess I am missing something. I searched the community and did what was recommended, but still unable to get past this one issue! below is my code
import os
def absolute(root, path):
if not os.path.isabs(path):
root = '/'
return root + path
else:
return path
7 Answers
Steven Parker
243,656 PointsYou can, but you'll get this one. I just noticed one other thing, the instructions say the function "takes two arguments, a path string and a root string". It's important the the arguments are specified in that order.
Steven Parker
243,656 Points Hi, I got your request.
There's no link button on this question so I can check the requirements or test my correction, but I'd guess that if "root" is being passed in as an argument then you probably should not overwrite it with a new string.
If that's not it, please provide a link the the course page.
Jonathan Beard
1,681 PointsBelow is the link. https://teamtreehouse.com/library/python-for-file-systems/navigation/absolutely
I'm sure you're correct but I'm not sure how to append it to reflect the correct path. I tried the below code as well.
Challenge: Now I need you to write a function named absolute that takes two arguments, a path string and a root string. If the path is not already absolute, return the path with the root prepended to it. For example: absolute("projects/python_basics/", "/") would return "/projects/python_basics/" while absolute("/home/kenneth/django", "C:\") would return "/home/kenneth/django".
def absolute(root, path):
if not os.path.isabs(path):
return '/' + path
else:
return path
Jonathan Beard
1,681 PointsAlso tired this....
def absolute(root, path):
if not os.path.isabs(path):
return os.path.abspath(path)
else:
return path
Steven Parker
243,656 PointsI don't see a link in your answer. But I suspect you'll need to make use of that "root" argument that is passed in.
Jonathan Beard
1,681 PointsThat makes sense, but I'm not sure how to change the root variable from "C:\\" to root = '/' without just explicitly redefining the variable.
The code I posed actually works when I test it but I can assume there is something else I am missing.
Steven Parker
243,656 PointsBy "works" do you mean it passes the challenge? And if not, what if you use the root value passed in without reassigning it? The instructions say "If the path is not already absolute, return the path with the root prepended to it.".
I assume by "root" they mean the value that was passed in.
Jonathan Beard
1,681 PointsIt works in pycharm, but it will not pass the challenge.
the instructions state even if the c:\\ is passed to append with '/' which is why I redefined root = '/'.
If I just use root + path, in the case the root is c:\\ it will return C:\path/to/my/folder which is incorrect.
Steven Parker
243,656 PointsPerhaps the separator type isn't important to the challenge, they were pretty specific about returning "the path with the root prepended".
Did you try it?
Jonathan Beard
1,681 PointsI am really at a loss. I have tried several combinations to achieve what is being asked. Can I just skip a challenge?
Jonathan Beard
1,681 PointsOMG!!!
Yes what a simple oversight!! Thank you for that!!!
def absolute(path, root):
if not os.path.isabs(path):
return root + path
else:
return path
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsChanged comment to answer. Marked best answer.