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

南楼画角 提交于 2019-11-26 09:38:45

问题


Why is Python giving me a syntax error at the simple print statement on line 9?

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\",\"\")

The version of Python is:

Python 3.2.2 (default, Sep  4 2011, 09:07:29) [MSC v.1500 64 bit (AMD64)] on win
32

回答1:


In Python 3, print is a function, you need to call it like print("hello world").




回答2:


Use print("use this bracket -sample text")

In Python 3 print "Hello world" gives invalid syntax error.

To display string content in Python3 have to use this ("Hello world") brackets.



来源:https://stackoverflow.com/questions/7584489/python-print-statement-syntax-error-invalid-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!