[Tutor] SyntaxError while defining tuple : Guidance requested

Alan Gauld alan.gauld at freenet.co.uk
Sat Dec 31 15:00:03 CET 2005


Hi John,

> File "page114.py", line 12
>    inventory = ("Sword","Armor","Shield","Healing
> Potion")
>            ^
> SyntaxError: invalid syntax

The inventory= line should all be on a single line, Python doesn't like
you breaking strings across lines (unless they are triple quoted).

However, interestingly the error you get is not referring to that problem
but to an earlier one in raw_input(). Once you fix that the broken string
error will rear its head....

There is another stragnge feature in the code you posted:

> **********************************************************
> inventory()

I'm not sure whats going on here, it seems to be calling a function called
inventory which we can't see? Are you sure that this line shouldn't be

inventory = ()

ie assigning an empty tuple?

# treat the tuple as condition

> if not inventory:
>        print "U are empty Handed."

And here it tests to see if inventory exists, which is it is a function then
it will always be true so the error message will never be printed, but if
it is an empty tuple the error will always be printed. Either way this
seems to be a redundant test!....

> raw_input("\nPress the enter key to continue."

This needs a closing paren... the actual cause of your initial error

> #Create a tuple with some items
> inventory = ("Sword","Armor","Shield","Healing
> Potion")

And here we try to assign a tuple to inventory which will result in the
function being hidden. THis is what makes me think the first line should
be an assignment?

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list