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 Collections (2016, retired 2019) Slices Slice Functions

Arun Patel
Arun Patel
1,180 Points

Added additional code to enhance the task. Why is the task failing?

I have additional logic to enhance the task, take user input and then slice the string to get the first 4 letters. Is the logic not correct? Can't this be validated by Workspaces?

slices.py
def main():
    userinput = str(input("Enter a word >" ))
    list2 = first_4(userinput)
    print(list2)


def first_4(str1):
    str2 = list(str1)

    if len(str2) >= 4:
        list1 = str2[:4]
    else:
        print("Please enter a word with minimum 4 letters")
        userinput1 = input("Enter Yes to continue or No to Exit >")
        if userinput1.lower() == "yes":
            main()
        else:
            print("Thanks")
        return
    return list1

main()

2 Answers

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Hi Arun Patel

When attempting to complete challenges, it is best to only write the code necessary to pass. There are predefined test cases that will be using your code to make sure it is meeting the requirements of the challenge. The challenge wont let you override the behavior of how the test is going to perform its checks against your code. if you try to force run your own code it will just give you an error because you are trying to do more than it asked and are giving it unexpected results.

Arun Patel
Arun Patel
1,180 Points

Thanks Chris. Will keep this in mind for future tasks.