Python Forum
Syntax Error: Outside a function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax Error: Outside a function?
#1
ordered_list = [2, 4, 6, 8, 10]

guess = input('Guess a number: ')

for element in ordered_list:

if element == guess:
return True
else:
return False

Hi! Does anyone know why the code produces a syntax error which says "return true outside a function?" What does outside a function means and how do I solve the error?
Reply
#2
return is a builtin method which returns a value from a function. The function is executed and returns a value, here's an example:
def add(x, y):
    return x + y
It takes x and y, then returns their value added together. So they cannot be used outside functions.
print(add(3, 6))
Output:
9
Reply
#3
You cannot use "return" unless outside a function. You have no functions so you cannot use return.

You could write a function like this:
ordered_list = [2, 4, 6, 8, 10]

def guess_in_list(guess, ordered_list):
    for element in ordered_list:
        if element == guess:
            return True
        else:
            return False

guess = input('Guess a number: ')
That wouldn't work at all for many reasons. The first being that input returns a string and your ordered list contains numbers (integers). The second reason being that it only looks at the first element in the list.
ordered_list = [2, 4, 6, 8, 10]

def guess_in_list(guess, ordered_list):
    for element in ordered_list:
        if element == guess:
            return True
    return False

guess = int(input('Guess a number: '))
print(guess_in_list(guess, ordered_list))
That works, but it ignores a useful list operation. You should always work on increasing you familiarity with a programming language. The more you know the better your code.
ordered_list = [2, 4, 6, 8, 10]

def guess_in_list(guess, ordered_list):
    return guess in ordered_list

guess = int(input('Guess a number: '))
print(guess_in_list(guess, ordered_list))
And now that we are down to a one line function there is no reason to have a function.
ordered_list = [2, 4, 6, 8, 10]

guess = int(input('Guess a number: '))
print(guess in ordered_list)
As long as we are at it, why use a variable name like "ordered_list". Variable names should describe the thing they reference, not the thing's type.
even_numbers = [2, 4, 6, 8, 10]

guess = int(input('Guess a number: '))
if guess in even_numbers:
    print(guess, 'is an even number in the range 2 to 10')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,015 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 335 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,454 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,137 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,251 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,194 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 848 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  [Solved] unkown (to me) function def parm "name1:name2" syntax. MvGulik 5 988 Nov-11-2022, 11:21 AM
Last Post: MvGulik
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,778 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,291 May-18-2022, 06:50 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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