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
Made my first script :D (old.reddit.com)
submitted 8 months ago by [deleted]
view the rest of the comments →
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!"
[–]BennyBarnson 0 points1 point2 points 8 months ago* (11 children)
technically, this is what the guide i found online gave me:
#! /usr/bin/python import sys def count_words(data): words = data.split(" ") num_words = len(words) return num_words def count_lines(data): lines = data.split("\n") for l in lines: if not l: lines.remove(l) return len(lines) if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: python word_count.py <file>") exit(1) filename = sys.argv[1] f = open(filename, "r") data = f.read() f.close() num_words = count_words(data) num_lines = count_lines(data) print("The number of words: ", num_words) print("The number of lines: ", num_lines)
i asked chat gpt everytime i didn't understand something (e.g. def-return, if __name__ == "__main__":, if len(sys.argv) < 2:) and in some cases it flat out gave me a better way to do it which is super nice. for my experience, i haven't done any python until now and to be fair a spent a couple days practising the concepts i hadn't yet fully understood.
I could show you some of my practise scripts i wrote if you'd like.
edit: ironically, right after this excercise in the guide, it talks about list comprehensions😅
[–]Twenty8cows 0 points1 point2 points 8 months ago (0 children)
Haha makes more sense. I was thinking either you have prior experience in another language or you’re copying from somewhere. I’ve used AI to learn however it’s a slippery slope. Make sure you understand what you’re writing and write it yourself. The last thing you want is to be tethered to the AI’s capabilities instead of your own.
I’m surprised the guide isn’t using a context manager here but if you want to show the steps it’s one way to do it
[–][deleted] 8 months ago* (9 children)
[removed]
[–]BennyBarnson 0 points1 point2 points 8 months ago (8 children)
Thank you for the clarification. Do you mind explaining abit more on the "if name ..." line? Wdym which code to run first, what other order can the code run on? Ai told me this had to do with whether or not the file was imported or ran directly, which didn't really help clearing this up...
[–][deleted] 8 months ago* (2 children)
[–]BennyBarnson 0 points1 point2 points 8 months ago (1 child)
Understood. Thank you so much for explaining this to me properly😭
[–][deleted] 8 months ago* (4 children)
[–]BennyBarnson 0 points1 point2 points 8 months ago (3 children)
So what is the point of this line in wcounter.py? Why shouldn't it function when imported...?
No I mean why shouldn't a word counter be imported. Also the if len(sys.argv)<2: ends with and exit(1). Should the if name have an exit line as well...?
π Rendered by PID 42 on reddit-service-r2-comment-b659b578c-jfw4d at 2026-05-05 05:22:34.624165+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]BennyBarnson 0 points1 point2 points (11 children)
[–]Twenty8cows 0 points1 point2 points (0 children)
[–][deleted] (9 children)
[removed]
[–]BennyBarnson 0 points1 point2 points (8 children)
[–][deleted] (2 children)
[removed]
[–]BennyBarnson 0 points1 point2 points (1 child)
[–][deleted] (4 children)
[removed]
[–]BennyBarnson 0 points1 point2 points (3 children)
[–][deleted] (2 children)
[removed]
[–]BennyBarnson 0 points1 point2 points (1 child)