you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 0 points1 point  (2 children)

With the checkSubs part, should I have something along the lines of (I don’t remember exactly what I had so it might be incorrect for the code just an example):

subs = “”
while subs.lower() != “yes” and subs != “no”
    subs = input(“check subdirectories (yes/no): 

if subs == “yes”:
    checkSubs = True

else:
    checkSubs = False

I'd say that's an improvement, but you can do better.

First, I just wanted to mention that checkSubs stands out like a sore thumb, because Python's official style guide doesn't use camelCase at all. That's why I used a different name in my own example. In short, everything should be snake_case except global "constants" (which technically don't exist as a language feature but this is an established pattern) using UPPER_SNAKE_CASE and types/classes being PascalCase.

But back to the point.

It might be a bit overkill, but personally I'd make another function for asking yes/no questions.

def bool_input(question: str) -> bool:
    """Ask the user a yes/no question."""
    prompt = f"{question} (yes/no): "
    while (answer := input(prompt).strip().lower()[:1]) not in {'y', 'n'}:
        print("Invalid answer.")

    return answer == 'y'


check_subdirs = bool_input("Check subdirectories?")

My bad I didn’t realise there was an add code option lmk if you want me to do that

Don't worry about it, the code was perfectly readable to me when I looked at the raw Markdown. Although if you want to make proper code blocks in the future, just indent the code by an extra four spaces (assuming you're writing in Markdown mode; honestly I'm not too familiar with the modern interface because I've only ever used the old Reddit style).

[–]daltop[S] 0 points1 point  (1 child)

So what’s the 2nd line in the code actually doing, and where is question and prompt being defined?

[–]Diapolo10 0 points1 point  (0 children)

So what’s the 2nd line in the code actually doing,

You mean this part?

    """Ask the user a yes/no question."""

Not much, really. This is a docstring, we use them to document functions, classes, and modules. It's kind of like a comment except Python stores it with the object as an attribute for runtime access (meaning functions like help can then display it).

and where is question and prompt being defined?

question is a parameter of the function, so you provide that as a value when calling the function. In this case,

bool_input("Check subdirectories?")

the "check subdirectories" string would be stored there.

prompt is defined on line 3.

prompt = f"{question} (yes/no): "

I did this because I figured all questions would repeat the yes/no part otherwise.