定义此函数时出现语法错误.
def questionfilehandler("filename.txt"):
with open("filename.txt", "r") as file:
print(file.read)
return input()
file.close()
Run Code Online (Sandbox Code Playgroud)
我查了一下语法,这一切似乎都是正确的.
这是我收到的错误消息
这是IDLE强调错误的代码.
感谢所有阅读并试图回答此问题的人.非常感谢您的时间=).
假设您的缩进得到修复,这很明显......您不能直接将字符串作为函数参数调用.你需要一个变量:
def questionfilehandler(filename):
with open(filename, "r") as file:
print(file.read())
return input()
# file.close() - not needed
Run Code Online (Sandbox Code Playgroud)
然后......你可以用一个字符串作为参数来调用函数:
questionfilehandler("filename.txt")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
334 次 |
| 最近记录: |