Find the number of characters in a file using Python

后端 未结 16 1426
春和景丽
春和景丽 2021-02-07 00:34

Here is the question:

I have a file with these words:

hey how are you
I am fine and you
Yes I am fine

And it is asked to find the numbe

16条回答
  •  有刺的猬
    2021-02-07 01:20

    You do have the correct answer - and your code is completely correct. The thing that I think it is doing is that there is an end of line character being passed through, which includes your character count by two (there isn't one on the last line, as there is no new line to go to). If you want to remove this, the simple fudge would be to do as Loaf suggested

    characters = characters - (lines - 1)
    

    See csl's answer for the second part...

提交回复
热议问题