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

Didn'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
Steven Parker
243,656 Points

You 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
Steven Parker
243,656 Points

:mailbox_with_mail: 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.

Below 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

Also tired this....

def absolute(root, path):
    if not os.path.isabs(path):
        return os.path.abspath(path)
    else:
        return path
Steven Parker
Steven Parker
243,656 Points

I 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.

That 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
Steven Parker
243,656 Points

By "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.

It 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
Steven Parker
243,656 Points

Perhaps 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?

I am really at a loss. I have tried several combinations to achieve what is being asked. Can I just skip a challenge?

OMG!!!

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