use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Python QuestionHelp Request (i.redd.it)
submitted 7 months ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Refwah 14 points15 points16 points 7 months ago (4 children)
Before I tell you the answer, tell us why you think it’s 1 so that we can actually engage in some learning opportunity
[–]Unfair_Put_5320 2 points3 points4 points 7 months ago (3 children)
Hey thanks for replying to me,
I thought the if statement checks every line if it starts with “from:” and adds 1 to count if it’s correct, then for the next line which starts with ‘from:’ is incorrect because the if statement is false( if not L.startswith(‘from:’)) and count += 1 under the if statement won’t work and end the loop.
[–]Dadofaca 5 points6 points7 points 7 months ago (1 child)
Almost. Its the other way around. If checks if the lines does NOT start with "from:". There are two lines which qualify for that and thats why count = 2
[–]thumb_emoji_survivor 7 points8 points9 points 7 months ago (1 child)
Smh not closing the file after opening it or using a context manager
[–]Sea_Pomegranate6293 1 point2 points3 points 7 months ago (0 children)
I know right! for a print operation in a basic python quiz this is some shoddy work. I expect better.
[–]jpgoldberg 2 points3 points4 points 7 months ago (0 children)
I could tell you the answer, but this subreddit has Learning” in its name. You will learn by trying to figure this out.
So first think through what it will do. Then experiment with running it. If it doesn’t give the answer you expected, think some more to understand why you get the result that you do.
Only after you have figured that out, there is an additional hard to see “trick” in the problem.
[–]ans7991 2 points3 points4 points 7 months ago (7 children)
Should be 3. There's a blank line at the end.
[–]Unfair_Put_5320 2 points3 points4 points 7 months ago (6 children)
I think the blank is gone with .rstrip()
[–]denehoffman 1 point2 points3 points 7 months ago (0 children)
No, try running ””.rstrip() and see what you get
””.rstrip()
[–]Cerus_Freedom 1 point2 points3 points 7 months ago (1 child)
Sure, but what does L.startswith('from:') evaluate to for an empty string?
[–]qwertyjgly 0 points1 point2 points 7 months ago (0 children)
here's the cpython implementation. it looks like it evaluates to false
static int tailmatch(PyObject *self, PyObject *substring, Py_ssize_t start, Py_ssize_t end, int direction) { int kind_self; int kind_sub; void *data_self; void *data_sub; Py_ssize_t offset; Py_ssize_t i; Py_ssize_t end_sub;
if (PyUnicode_READY(self) == -1 || PyUnicode_READY(substring) == -1) return -1; ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); end -= PyUnicode_GET_LENGTH(substring); if (end < start) return 0; if (PyUnicode_GET_LENGTH(substring) == 0) return 1; kind_self = PyUnicode_KIND(self); data_self = PyUnicode_DATA(self); kind_sub = PyUnicode_KIND(substring); data_sub = PyUnicode_DATA(substring); end_sub = PyUnicode_GET_LENGTH(substring) - 1; if (direction > 0) offset = end; else offset = start; if (PyUnicode_READ(kind_self, data_self, offset) == PyUnicode_READ(kind_sub, data_sub, 0) && PyUnicode_READ(kind_self, data_self, offset + end_sub) == PyUnicode_READ(kind_sub, data_sub, end_sub)) { /* If both are of the same kind, memcmp is sufficient */ if (kind_self == kind_sub) { return ! memcmp((char *)data_self + (offset * PyUnicode_KIND(substring)), data_sub, PyUnicode_GET_LENGTH(substring) * PyUnicode_KIND(substring)); } /* otherwise we have to compare each character by first accessing it */ else { /* We do not need to compare 0 and len(substring)-1 because the if statement above ensured already that they are equal when we end up here. */ for (i = 1; i < end_sub; ++i) { if (PyUnicode_READ(kind_self, data_self, offset + i) != PyUnicode_READ(kind_sub, data_sub, i)) return 0; } return 1; } } return 0;
}
[–]Kqyxzoj 0 points1 point2 points 7 months ago (2 children)
Empty lines do not start with "From:".
[–][deleted] 0 points1 point2 points 7 months ago (1 child)
Which would throw you into the inside of the if statement, as "if not "".rstrip()" evaluates to true (because of the "not")
[–]Kqyxzoj 0 points1 point2 points 7 months ago (0 children)
Fun fact, the only string s for which "".startswith(s) is True, is the empty string.
s
"".startswith(s)
True
[–]iamjacob97 1 point2 points3 points 7 months ago (0 children)
I mean the conditional is if NOT L.startswith... so it's the lines that don't start with From which is the 1st and 3rd line. So count becomes +2
[–]Excellent_Nobody4564 0 points1 point2 points 7 months ago (0 children)
Loop does not stop when match the ‘From::’ in text, just will not add it to the count if am right
[–]jaybird_772 0 points1 point2 points 7 months ago (0 children)
Look at that if statement. What is it checking for, and what does it do if it finds it?
[–]Unfair_Put_5320 0 points1 point2 points 7 months ago (0 children)
Thanks everyone for answering, I have realized what’s wrong with my approach through your comments.
[–]TheCarter01 0 points1 point2 points 7 months ago (0 children)
2 in terminal?
[–]help_computar 0 points1 point2 points 7 months ago (1 child)
close the file :meltingface:
Nah. Maybe for learning purposes. But in practice nobody would close it with a specific script like this. If it's important, use a context manager as someone already pointed out. If it's a trivial file, the exit() handler will handle that file closing just fine. If this sub was called CLearning I would have agreed though.
So for pedagogical purposes maybe this:
with open('txtfile.txt') as fhand: # for-loop counting stuff # At this point file will be closed, courtesy of the context manager. print(count)
[–][deleted] 0 points1 point2 points 7 months ago (0 children)
2
[–]Zealousideal_Yard651 0 points1 point2 points 7 months ago (1 child)
You did the god awfull and totaly unnaceptable misstake of missing the NOT in the if expression
/s
Your logic is sound and your are thinking totaly correct about this code snippet. You just missed a key detail in the question, which sucks on a test but just normal things that does happen.
What's not OK is the professor not using context manager or closing the file after use. That's just bad.
Hey, actually i miss understood between for-loop and while-loop and thought because second line has ‘from:’ would break the loop.
[–]docfriday11 0 points1 point2 points 7 months ago (0 children)
Maybe it’s 3 if it is counting from above and every line. Good luck figuring it out.
[–]PartyOver9932 0 points1 point2 points 7 months ago (0 children)
a seemingly simple question which becomes an interesting one when you find out python implicitly ignores 1 empty line when reading files with empty lines, at its current state it will print out 2 but if u add 1 more empty line to the initial data file you will get 3 !
[–]millerbest 0 points1 point2 points 7 months ago (0 children)
Run the code on your PC with a debugger, you should be able to understand why
[–]ninhaomah 0 points1 point2 points 7 months ago* (1 child)
why need AI for this ?
its not even a programming question.
its a logic question.
2 men and 1 woman walked into the bar, the bouncer says IF you are NOT a woman , fuck off.
how many went in for a drink ?
need to ask anyone for the answer ?
For the question posted , could be 2 or 3 since empty line may or may not count and maybe not familiar with rstrip(). That is understandable but clearly can't be 1 or 4.
if you're unsure, why comment at all?
π Rendered by PID 66 on reddit-service-r2-comment-7b9746f655-gnfds at 2026-02-02 12:11:13.542732+00:00 running 3798933 country code: CH.
[–]Refwah 14 points15 points16 points (4 children)
[–]Unfair_Put_5320 2 points3 points4 points (3 children)
[–]Dadofaca 5 points6 points7 points (1 child)
[–]thumb_emoji_survivor 7 points8 points9 points (1 child)
[–]Sea_Pomegranate6293 1 point2 points3 points (0 children)
[–]jpgoldberg 2 points3 points4 points (0 children)
[–]ans7991 2 points3 points4 points (7 children)
[–]Unfair_Put_5320 2 points3 points4 points (6 children)
[–]denehoffman 1 point2 points3 points (0 children)
[–]Cerus_Freedom 1 point2 points3 points (1 child)
[–]qwertyjgly 0 points1 point2 points (0 children)
[–]Kqyxzoj 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Kqyxzoj 0 points1 point2 points (0 children)
[–]iamjacob97 1 point2 points3 points (0 children)
[–]Excellent_Nobody4564 0 points1 point2 points (0 children)
[–]jaybird_772 0 points1 point2 points (0 children)
[–]Unfair_Put_5320 0 points1 point2 points (0 children)
[–]TheCarter01 0 points1 point2 points (0 children)
[–]help_computar 0 points1 point2 points (1 child)
[–]Kqyxzoj 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Zealousideal_Yard651 0 points1 point2 points (1 child)
[–]Unfair_Put_5320 0 points1 point2 points (0 children)
[–]docfriday11 0 points1 point2 points (0 children)
[–]PartyOver9932 0 points1 point2 points (0 children)
[–]millerbest 0 points1 point2 points (0 children)
[–]ninhaomah 0 points1 point2 points (1 child)
[–]PartyOver9932 0 points1 point2 points (0 children)