Python Forum

Full Version: Learning python SyntaxError: 'return' outside function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here is my code:

import string 
letters_guessed = ['x', 'i', 'k', 'p', 'r', 's', 'z', 'h', 'b'] 
alpha = string.ascii_lowercase
available_letters = alpha
def get_available_letters(letters_guessed):
    '''
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string (of letters), comprised of letters that represents which letters have not
      yet been guessed.
    '''
for char in (alpha):
    print(char)
    if char in (letters_guessed):
        available_letters = available_letters.replace(char,'')
        print(available_letters)
This code does what it should. It gives a printout of the alphabet minus letters guessed. ANYTIME I try to add a return statement ANYWHERE in this code I get either SyntaxError: 'return' outside function or IndentationError: unexpected indent. I have tried indenting everything in every manner I can manage. Please help.
Please proper tags when posting code.
The lines from for char in (alpha): onwards are not indented into the function get_available_letters
Also, when debug error - always post the full traceback (in error tags) alongside the actual code that produce it (in python tags).
(Aug-03-2020, 02:49 AM)Grale1953 Wrote: [ -> ]This code does what it should.
This code (as is now) will produce error