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 Basics (2015) Letter Game App Random Item

Jun Kheng Ong
Jun Kheng Ong
580 Points

Able to print results but not return, due to syntax error: 'return' outside function.


I've managed to get the code to print the answers required for this challenge, but when I changed it to return it gives the SyntaxError: 'return' outside function

Please see my work attached and help (probably a very newbie mistake!)

Thanks, jk

import random

while True:
    start = input("Press enter/return to start, or enter Q to quit: ")
    if start.lower() == 'q':

    # enter a word
    random_item = input("Key in a single word: ")

    if random_item.isalpha():
        print("You choice of word is {}".format(random_item))

    elif not random_item.isalpha():
        print("A single word is made up of alphabets, not with numbers, spaces, or special characters!@#$%^&*:")

    y = len(random_item)
    yy = y-1
    z = random_item
    x = (random.randint(0,yy))
    final = (z[x])

1 Answer

28,558 Points

The error message is actually pretty straightforward in this case, return is a keyword used for returning information from a function, as such it cannot be used outside a function. You have not actually made any function in your solution. I would recommend that you go back and rewatch the Fully Functional video where the concept of creating functions are explained and demonstrated.

Also the function issue aside your code does far more than what the challenge asks for, when doing these challenges it's always best to keep the code as simple as possible. The code should fulfill the exact criteria of the challenge instructions but should not go beyond that. You don't have to do any looping, printing, validation or anything like that to solve this challenge.

Also you are not meant to get the item using the input command, but from a parameter in the function. That will likely make more sense after rewatching the video I link to above.

Jun Kheng Ong
Jun Kheng Ong
580 Points

Thanks andren for taking the time to put the details in! Sorry for the overdoing part, it was added as I was trying to see what's happening and if I'm pulling out the correct input.

28,558 Points

You don't need to feel bad about overdoing it, I only included the paragraph about that since these challenges will often mark your code as wrong if you go overboard. When actually making real programs on the other hand going overboard is often a good thing, especially when it comes to validation and the like.