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
  Line 42 syntax error..Help!!1 patpython 4 259 Sep-01-2021, 03:22 PM
Last Post: deanhystad
  Tuple generator, and function/class syntax quazirfan 3 307 Aug-10-2021, 09:32 AM
Last Post: buran
  Unexplained Invalid syntax Error cybertooth 5 347 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  I have a syntax error! rohes_kaugummi 3 513 Jun-23-2021, 05:24 PM
Last Post: deanhystad
  import module with syntax error Skaperen 7 588 Jun-22-2021, 10:38 AM
Last Post: Skaperen
Heart near "(": syntax error Maryan 3 725 Jun-21-2021, 08:05 AM
Last Post: ibreeden
Exclamation getting a syntax error and i don't know why ilikedofs 10 1,065 May-13-2021, 08:52 PM
Last Post: deanhystad
  Even number code syntax error MrCeez 1 559 May-02-2021, 06:43 PM
Last Post: Larz60+
  error in your SQL syntax Xuli 10 974 May-02-2021, 12:27 PM
Last Post: Xuli
  Function syntax error Changosoyyo 2 688 Mar-07-2021, 10:42 PM
Last Post: Changosoyyo

Forum Jump:

User Panel Messages

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