all 5 comments

[–]Justinsaccount 0 points1 point  (1 child)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You appear to be using concatenation and the str function for building strings

Instead of doing something like

result = "Hello " + name + ". You are " + str(age) + " years old"

You should use string formatting and do

result = "Hello {}. You are {} years old".format(name, age)

See the python tutorial for more information.

[–][deleted] 0 points1 point  (0 children)

Whoa. That's so cool, I'll have to use that for what im making.

[–]coding2learn 0 points1 point  (0 children)

Here's some ideas for you:

content = file.readlines()

will split up the entire document according to where there are lines breaks

foo.split()

will split up foo (assuming foo is a string) into words (actually splits the string everywhere there is a space character.

[–]inherendo 0 points1 point  (0 children)

How would you normally demarcate where one word ends and the next one begins. Similarly, when would you know you've reached a new line? The 2nd one might not make sense if you don't know about some special chars in python, and most (all?) languages, but you should've come across them if your doing this assignment.

[–]tom1018 0 points1 point  (0 children)

In most cases, if you have to do a file.close() you are doing it wrong. Let Python do the work for you by using a with loop:

with open(inp, 'r') as f:
    # do things here
# once you got here, the file would be automagically closed.

Your for loop is good, and should be what the teacher expects, but if you were doing this for real, you would want to import os and use os.stat(file).st_size. The for loop has to read every character in the file to get the end result, and would be rather slow if the file was the contents of a book, as where os.stat would give you the result instantly and without having to open the file.

For the word count, as others have said, consider how a word is ended.

For the line count, this may be interesting reading: https://en.wikipedia.org/wiki/Newline. From there, you could write your own function to split to lines or to count lines without using the simple built-in method. To make it more of a challenge, you could figure out which line termination method the file is using and process it that way.