【发布时间】:2017-04-12 17:37:14
【问题描述】:
定义此函数时出现语法错误。
def questionfilehandler("filename.txt"):
with open("filename.txt", "r") as file:
print(file.read)
return input()
file.close()
我查了语法,一切似乎都是正确的。
This is the error message I got
And this is the code with the error highlighted by IDLE.
感谢所有阅读并尝试回答此问题的人。非常感谢您的时间 =)。
【问题讨论】:
-
你没有正确缩进。
-
您显示的代码是实际代码的直接复制粘贴吗?如图所示的缩进?
-
对不起,在网站上写错了。缩进在实际程序中是正确的。我现在已经修好了。
-
你的函数参数不能写成字符串。
-
要知道的另一件事是,当使用上下文处理程序
with open(my_file ...时,您不需要在最后调用my_file.close()。它由with语句自动处理!