Python print statement “Syntax Error: invalid syntax” [duplicate]

602    Asked by arun_3288 in Python , Asked on Apr 14, 2021

In line number 9 of python code, I am getting a syntax error for the below code:

import hashlib, sys
m = hashlib.md5()
hash = ""
hash_file = raw_input("What is the file name in which the hash resides?  ")
wordlist = raw_input("What is your wordlist?  (Enter the file name)  ")
try:
    hashdocument = open(hash_file,"r")
except IOError:
    print "Invalid file."    # Syntax error: invalid syntax
    raw_input()
    sys.exit()
else:
    hash = hashdocument.readline()
    hash = hash.replace("n","")
My version of Python is:
Python 3.2.2 (default, Sep  4 2011, 09:07:29) [MSC v.1500 64 bit (AMD64)] on win
32
In line number 9 of python code, I am getting a syntax error for the below code:
import hashlib, sys
m = hashlib.md5()
hash = ""
hash_file = raw_input("What is the file name in which the hash resides?  ")
wordlist = raw_input("What is your wordlist?  (Enter the file name)  ")
try:
    hashdocument = open(hash_file,"r")
except IOError:
    print "Invalid file."    # Syntax error: invalid syntax
    raw_input()
    sys.exit()
else:
    hash = hashdocument.readline()
    hash = hash.replace("n","")
My version of Python is:
Python 3.2.2 (default, Sep  4 2011, 09:07:29) [MSC v.1500 64 bit (AMD64)] on win
32


Answered by Arun Singh

To remove python print invalid syntax error in Python 3 version print id considered as a function so it needs to be inside the parathesis as shown below:

print("Hello World")

Note: While using python code

Make sure you are not using a Python keyword for a variable name.

Check that you have a colon at the end of the header of every compound statement, including for, while, if, and def statements.

Check that indentation is consistent.

Make sure that any strings in the code have matching quotation marks.



Your Answer

Interviews

Parent Categories