![]() In a second reading, that must have been what you meant anyway.) Would have produced a clear error message. (A little disgression on names: IMHO this error arose from an inaccurate naming of a variable. So len(words_par) returns the number of characters instead of the number of words. When the condition evaluates to False, the split-method is not called and words_par remains a string (and not a list of strings as expected). So you could shorten the line to if words_par.startswith("P1") & words_par.endswith("P1"): "P1" or "P2" or "P3" evaluates to "P1" (non-empty strings are "truthy" values). More problems here: if words_par.startswith("P1" or "P2" or "P3") & words_par.endswith("P1" or "P2" or "P3"): # 'words_par' is initialized in the for-statement ![]() words_par = 0 # You can leave out this line, In the second part, something seems to go wrong. I'd shorten it a bit: with open('C:/data.txt', 'r') as textfile: The first part about counting all words is quite ok. Maybe I didn't understand the requirements completely, but I'll do my best. Print 'Words in last paragraph', para_type, ':', len(paragraph) Print 'Words in paragraph', para_type, ':', len(paragraph) You could just do print 'Words in paragraph', para_type, ':', len(paragraph) So instead of print 'Words in paragraph', para_type, ':', para_count The variable para_count is not needed, since the words are being appended to the paragraph variable. I actually just noticed some redundant code in the example. Print 'Words in last paragraph', para_type, ':', para_count Print 'Words in paragraph', para_type, ':', para_count ![]() Also note I made no comments, so feel free to hassle me to explain things for you. I tried to keep it simple and avoid using things like list comprehension, since you say you are just starting to learn, so it is not optimal, but hopefully will be clear. I wrote a version of what I think you are after as a example according to what I think you want. On the next line is always going to counting the number of characters in the line, since the if statement is always going to evaluate to False and the word_par never got split into a list of words.Īlso the else clause on the for loop will always be executed no matter whether the sequence is empty or not. If you do this your code will operate a little more efficiently. The first will evaluate both expressions no matter what the result of the first, where as the second will only evaluate the second expression if the first is True. While we are at it, unless you are wanting to do bitwise comparisons avoid doing something & something Will always be words_par.startswith("P1")Īlways evaluates to the first one which is True, which is the first string in this case. That should really be words_par.split()Īlso words_par.startswith("P1" or "P2" or "P3") Were to ever return True, would always be splitting the last line in your file, due to the last time it was assigned to was in the first part of your program where you did a full count of the number of words in the file. This, if the expression words_par.startswith("P1" or "P2" or "P3") & words_par.endswith("P1" or "P2" or "P3") Under a condition which will never be meet, it is turned into a list with the line.split()Įxpression. The words_par is at first a string containing the line from the file. Where you fall down is here words_par = 0 The first part is ok where you get the total words and print the result. Print words_par.replace('P1', '') #doesn't display it but still counts Words_all = words_all + len(line.split()) I ended up with this code: text = open (r'C:/data.txt', 'r') Somehow I ended up with something that counts the number of characters instead :/ "P1", ending with another participant "P2") and exclude these words from my word count. ![]() count the number of words in a specific paragraph, starting with a specific phrase (e.g. count the total number of words (got it), I'm trying to create a program that would: I've just started to learn Python so my question might be a bit silly. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |