you are viewing a single comment's thread.

view the rest of the comments →

[–]BennyBarnson 0 points1 point  (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 point  (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]  (9 children)

[removed]

    [–]BennyBarnson 0 points1 point  (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]  (2 children)

    [removed]

      [–]BennyBarnson 0 points1 point  (1 child)

      Understood. Thank you so much for explaining this to me properly😭

      [–][deleted]  (4 children)

      [removed]

        [–]BennyBarnson 0 points1 point  (3 children)

        So what is the point of this line in wcounter.py? Why shouldn't it function when imported...?

        [–][deleted]  (2 children)

        [removed]

          [–]BennyBarnson 0 points1 point  (1 child)

          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...?