Sax*_*guy -1 python python-3.x
在我讨厌之前,我还没有找到回答我问题的链接.我是Python 3的初学者.
我应该编写一个函数来打开我写的文件(data.txt),上面写着'嗨那里!' 使用换行符,假设给我10分.
我在下面编写的代码给了我第一个测试用例值10,但它没有隐藏的测试用例 - 这应该给我一个值81.我的代码有什么问题?
def file_size(lines):
"""docstring"""
with open('data.txt', 'r') as file:
lines = file.read()
return len(lines)
print(file_size('data.txt'))
# data.txt contains 'Hi there!' followed by a new line character.
ans = file_size('alongertextfile.txt')
print(ans)
Run Code Online (Sandbox Code Playgroud)
您需要打开其名称作为参数传递的文件:
def file_size(filename):
"""docstring"""
with open(filename, 'r') as file:
data = file.read()
return len(data)
Run Code Online (Sandbox Code Playgroud)