Python Forum
Learning python SyntaxError: 'return' outside function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning python SyntaxError: 'return' outside function
#1
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.
Reply
#2
Please proper tags when posting code.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
The lines from for char in (alpha): onwards are not indented into the function get_available_letters
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 562 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,176 Aug-06-2023, 02:29 PM
Last Post: deanhystad
Sad I'm stuck with the reinforcement learning function in my Code OskiLori 2 1,515 May-20-2023, 10:01 PM
Last Post: OskiLori
  function return boolean based on GPIO pin reading caslor 2 1,131 Feb-04-2023, 12:30 PM
Last Post: caslor
  return vs. print in nested function example Mark17 4 1,674 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  How to invoke a function with return statement in list comprehension? maiya 4 2,753 Jul-17-2021, 04:30 PM
Last Post: maiya
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,753 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  Function - Return multiple values tester_V 10 4,321 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Question on None function in a machine learning algorithm Livingstone1337 1 2,331 Mar-17-2021, 10:12 PM
Last Post: supuflounder
  Get return value from a threaded function Reverend_Jim 3 16,811 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020