[Tutor] print() invalid syntax error

stuart kurutac scoobydoostu at hotmail.com
Tue Dec 23 19:27:10 CET 2014


Hello all,

I'm working my way through the Michael Dawson book "Python Programming for the Absolute Beginner" and I've come across an invalid syntax error on 2 occassoins now. For the life of me I can't see what's wrong. I've checked and re-checked I've typed in the code from the book correctly, I've ran the lines in IDLE and one works but the other doesn't. The first program is:

#Pizza Slicer
#Demonstrates string slicing

word = "pizza"

print(
"""
  Slicing 'Cheat Sheet'

 0   1   2   3   4   5
 +---+---+---+---+---+
 | P | i | z | z | a |
 +---+---+---+---+---+
-5  -4  -3  -2  -1

"""
)

print("Enter the beginning and ending index for your slice of 'pizza'.")
print("Press the enter key at 'Start' to exit.")
start = None
while start != "":
    start = (input("\nStart: "))

    if start:
        start = int(start)


        finish = (int(input("Finish: "))


        print("word[", start, ":", finish, "] is", end=" ")
        print(word[start:finish])

input("\n\nPress the enter key to exit.")


When I run this through the terminal in linux using python3 I get the following:

  File "pizza_slicer.py", line 32
    print("word[", start, ":", finish, "] is", end=" ")
        ^
SyntaxError: invalid syntax

If I type the while loop into IDLE it doesn't produce the error but it doesn't return anything full stop.

Now I get teh same sytax error for another program, the lines in question are below (I can include all the code if required):

#display one item through an index
index = (int(input("\nEnter the index number for an item in inventory: "))
print("At index", index, "is", inventory[index])

Once again, if Ii run this through the terminal I get the following:

  File "hero's_inventory2.py", line 26
    print("At index", index, "is", inventory[index])
        ^
SyntaxError: invalid syntax


Is this something simple such as a difference with python version syntax? I tried running with version 3.2.3 and the native version (2.7.3) of python and got the same error message at the same point so I didn't think it could be this.

I know it's probably something simple I just can't see it. Can anyone help? If you need any more info just let me know.
 		 	   		  


More information about the Tutor mailing list