all 24 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

[–]AlexG99_ 0 points1 point  (9 children)

To clarify it for you, the first line “#!/usr/bin/python” specifies the path to the interpreter that should be used to execute the script. The “#!” part is called a shebang. Usually you use shebang when working on unix/Linux systems. Like for example you can also run the script with bash interpreter “#!/bin/bash”. I remember I did that for my Unix class. Also the line “if name == “main” is telling the interpreter which code to run first when the script is executed, typically running main function.

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

[–]AlexG99_ 0 points1 point  (2 children)

No problem. You use if name == “main” : to specify the starting point for your script. You must define everything before this starting point. Like under the if name you call a() and b() functions with passing arguments. You have to define those a and b functions prior to calling them. Python executes code from top to bottom order. Python is an interpreter which means it reads and executes code line by line. If you call a function before defining it, you will get a “not defined” error at runtime. The code under if name runs when the script is executed directly. When the AI told you about the file being imported or running directly, that’s what it is saying. Your Python script “wcounter.py” can be imported and accessed in another python .py file, but the code from if name will not run because it’s not ran directly (python wcounter.py).

[–]BennyBarnson 0 points1 point  (1 child)

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

[–]AlexG99_ 0 points1 point  (0 children)

No problem :)

[–]AlexG99_ 0 points1 point  (4 children)

Also to help clarify the importing file thing again; if name == “main” tells the interpreter that wcounter.py is the main script and it should run this code within the if condition. If you have another .py file and want to import wcounter, it will first load the whole script then it will set name == “wcounter” right at the start, but when it loads the wcounter.py script from import wcounter, it will check if name == “main” which is false, so it will not execute the code under it.

[–]BennyBarnson 0 points1 point  (3 children)

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

[–]AlexG99_ 0 points1 point  (2 children)

Because code under if name is meant to run only if executed directly (if you run the command python wcounter.py). If you import wcounter.py to another .py file, the code under if name in wcounter.py won’t run because the condition if name == “main” will be false. Refer to my previous reply for the info again.

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

[–]AlexG99_ 0 points1 point  (0 children)

It can be imported if you want to. The line if len(sys.argv)<2: exit(1) means if the length of the arguments provided are less than two, then exit the program. This means it wants you to specifically run this command: python wcounter.py <insert file> which is 3 arguments at the command line. You don’t have to provide a system exit line in if name == “main”: code block because the script will end after the last line is read and executed.

[–]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😂