[Tutor] Return Statement error

Steven D'Aprano steve at pearwood.info
Mon Oct 13 01:01:53 CEST 2014


On Sun, Oct 12, 2014 at 04:38:54PM +0200, William Becerra wrote:
> Hello, I'm new to programming. I'm using Python 2.7.8 and Windows 8 OS
> I was making an application to see if I understand how the return statement
> works

The `return` statement can only be used inside a function. That means 
you have to start off with a `def` line, and indent your code.

Have you learned about functions yet? If not, perhaps you might prefer 
to forget about `return` until you do. Otherwise, if you take the code 
you wrote, indent it, and put a function declaration at the top, you 
should be able to use `return` successfully:

def compare():
    print "Please write a value for x"
    x = raw_input()
    print "Please write a value for y"
    y = raw_input()
    if x  > y:
        return 1
    elif x < y:
        return -1
    elif x == y:
        return 0
    else:
        return "this will never happen"


Then, once you have defined your function, you can call it:

result = compare()  # don't forget the parentheses ()
print "And the result is", result



-- 
Steven


More information about the Tutor mailing list