[Tutor] SyntaxError Message
Alan Gauld
alan.gauld at btinternet.com
Tue Jul 8 15:40:12 CEST 2014
On 08/07/14 02:41, Pamela Wightley wrote:
> Hi All,
>
> I have no programming experience and am trying to teaching myself
> python. Am trying to replicate the code below but I get the error
> message below highlighted in yellow:
Unfortunately I can't see anything in yellow, probably due to enmail
losing the formatting.
But the code below has several errors and looks like its part
of a bigger program. It also looks completely unrelated to the
errors you show us further down.
> choice = input("Choose your option: ")
You seem to be using Python 2 in which case using input() is
frowned upon. Its better to use raw_input() and convert the
resultant string to a number using int() or float() as needed.
[ In Python 3 input() has been rwe oved and raw_input renamed to input()...]
>
> if choice == 1:
indentation(spacing) is very importanbt in Python.
You should only indent the code inside a code block that follows a
structural statement such as if/for/while/def/class etc.
Indenting the if statement after the input() will raise an error.
> add1 = input("Add this: ")
But this should e indented because its part of the if block.
>
> add2 = input("to this: ")
>
> print add1, "+", add2, "=", add1 + add2
>
> elif choice == 2:
>
> sub2 = input("Subtract this: ")
>
> sub1 = input("from this: ")
>
> print sub1, "-", sub2, "=", sub1 - sub2
>
> elif choice == 3:
>
> mul1 = input("Multiply this: ")
>
> mul2 = input("with this: ")
>
> print mul1, "*", mul2, "=", mul1 * mul2
>
> elif choice == 4:
>
> div1 = input("Divide this: ")
>
> div2 = input("by this: ")
>
> print div1, "/", div2, "=", div1 / div2
>
> elif choice == 5:
>
> loop = 0
>
> Any assistance appreciated.
>
> ERROR MESSAGE
>
>>>> return input (1)
>
> File "<stdin>", line 1
>
> SyntaxError: 'return' outside function
>
This seems completely unrelated to the choice code?
You can only use return inside a function (ie a block
starting def....)
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list