Indentation in Python

Asked

Viewed 779 times

0

I wonder where I could put blank spaces to separate parts of the code according to PEP8.

q = int(input('Quantos números sua sequência tem?(3 ou mais)'))
if q < 3:
    while True:
        q = int(input('Quantos números sua sequência tem?(3 ou mais)'))
        if q >= 3:
            break
seq = [int(input()) for c in range(0, q)]
x = y = 0
n = []
while y < q:
    if x not in seq: 
        while True: 
            x += 1 
            if x in seq:
                break
    else:
        if x in seq: 
            n.append(x)
            x += 1
            y += 1
    if y == q:
        break
print(n)
  • I think only the PEP8 it matters and it seems to be all right, I just bumped my eye.

  • I was doubtful if there was any deviation in the PEP, but as for what seems not to exist, I edited the question and focused on my second question.

  • 1

    But haven’t you asked that already? https://answall.com/q/249584/101

  • I still have some doubts that I think would only be solved in a more practical way, demonstrating in the code.

  • I don’t know, in this code it’s all right.

  • Even though I had already asked a question on the subject, I realized that after a while I still had some doubts that I could not clarify through the last question, so I asked this new question to supplement the other with a more practical and less theoretical example.

  • @Lucassouza What are these doubts, more specifically? Why this question here is about where spaces should be in the code, and this is already (very well) answered in the other question you asked.

Show 2 more comments

1 answer

1


you have more to worry about in the code than the spacing of lines.

Code is about having functions - and if it’s applied to the problem, having classes and methods - a "program" played without even a function is something that can be done and will work - but almost no one in the world will care if it’s in the "right style": it does not present the most basic element for reuse of code and real organization of information that is the use of functions. If you have one, 3 or 20 blank lines where you shouldn’t make a difference.

PEP 8, which is just a suggestion document style code, although code formatted strictly according to it gets yes more beautiful to look at, it is especially draconian blank lines.

Basically, you can leave a blank line wherever you want in your code. But only one. If you skip two lines you’ll be violating the style convention. And then, of course, good sense: you leave blank lines when a part of the code is dealing with a "theme" or doing something different. It can be, for example, one after the input, one before the while (but not the while inside the if - it is already visually separated from the code around by starting a block). And possivelemnte one after the while and before the last print, in that code. But it, again, is not representative of a program "real".

Functions and classes in the object body must have exactly two blank lines before and two after their declaration (methods within a class must be separated only by a blank line, however).

And that’s basically it. Because your example of code doesn’t even have a function, it doesn’t have a "more practical display". Now, I insist that if it’s not yet in your nature to program separating everything into functions, this is where you should focus if you want to perfect your programming style.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.