[Tutor] Return Statement error

Joel Goldstick joel.goldstick at gmail.com
Sun Oct 12 19:02:55 CEST 2014


On Sun, Oct 12, 2014 at 10:38 AM, William Becerra <wbecerra1 at gmail.com> 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
> I want my application to compare  x and y and return either 1, -1 or 0.
> I'm using  IDLE
>
> Here is my code:
> 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 "Please insert a Valid character"
>
>
> When I press F5 to run my code a new window called syntax error appears
> The window says the following
> There's an error in your program:
> ***'return' outside function (C:/Users/William/Desktop/Python Files/Function
> compare.py, line6)
>
> What am I doing Wrong?

return is only allowed inside a function -- not in main line code  You
can wrap your code into a function and then just call it to see what
it returns
> I noticed that if i substitute all the return keywords with print the code
> runs correctly.
> However I want to use return as I am trying to learn how it works.
> Thank You.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
http://joelgoldstick.com


More information about the Tutor mailing list