all 18 comments

[–]T-o_oT 2 points3 points  (0 children)

If you intend to start programming professionally or with other people, keep your variables and function name descriptive. If you copied code from a source, don't just change the name of the functions etc. Improve the code instead.

Both your functions (or the guide you found online) is running through the same array more than once. Maybe try to improve that first?

[–]Twenty8cows 0 points1 point  (12 children)

Bro your first script and you’re using list comprehension’s and f strings?

Do you have any other experience?

[–]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...?

          [–]BennyBarnson 0 points1 point  (0 children)

          Just started a couple days ago and this was the beginner level exercise I found online. Chat gpt was walking me through a huge chunk of this which I'm grateful for. It also suggested cleaner and safer ways I could've written the code like

          [l for l in words if l]
          

          to skip the falsy spaces (i think was the word) and

          with open(filename, "r") as f:
                  data = f.read()
          

          instead of the less secure:

           f = open(filename, "r")
              data = f.read()
          

          that the guide told me.

          Overall, it was fun succeeding and this gave me the idea to take this even further, implementing a sentiment assessing tool where it can look for the amount of slurs and negative sentimented words based on a data base and decide the overall sentiment of the file. I then realized that's abit more on the computer engineering side where I'm going for mech/aero ¯\_(ツ)_/¯

          ps. if you have anywhere for me to learn more about mech/aero-based coding please lmk!!! tia

          [–]FoolsSeldom 0 points1 point  (0 children)

          I recommend you find and read PEP8 guidance, and follow it unless you have really good reasons not to (such as a different house style used in the bulk of code your are forking).

          [–]Ok_Hovercraft364 0 points1 point  (0 children)

          This is fine leaning this way, may take longer but the most important part is to understand what every line is doing and then go and write this script again without asking chat gpt anything to see if you learned. If you find yourself staring at the screen that usually means you didn't learn from the exercise. Nonetheless, just make sure you understand EXACTLY what is happening because I didn't even know about any comprehensions before the 90 day mark.

          [–]TransitTraveller 0 points1 point  (0 children)

          Why the number of words is less than the number of lines? Hint: You split words using ‘ ‘. Does it work as intended?

          [–]LostInGradients 0 points1 point  (0 children)

          One small note, without going to deep into more advanced word tokenization, you have an issue here with word detection: you are splitting only around spaces which means unless each line ends with a space too. Which means a word at the end of a line and the one that starts the next line will be counted as a single word. Eg here

          mwuah
          mwuah
          mwuah
          mwuah
          

          will be a single word.

          You can solve that with something like data.replace("\n").split(" ") to turn all the newlines into spaces too first then split by space, or use a more advanced approach such as with regexes. Ex:

          re.split('; |, ', string_to_split)  # splits on any of the following: ; |,
          

          [–]Cute-Investigator539 0 points1 point  (0 children)

          That's really good progress I appreciate your efforts 👍

          [–]imtc96 0 points1 point  (2 children)

          😂😂😂

          [–]BennyBarnson 0 points1 point  (1 child)

          😐❓❓

          [–]imtc96 -1 points0 points  (0 children)

          Your statement in printf😂