Indentation in Python [duplicate]

0

I would like to know where I could put blanks 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)
    
asked by anonymous 16.01.2018 / 16:25

1 answer

2

You have more to worry about in code than line spacing.

Code is about having functions - and if applicable 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 call if it is in "correct style": it does not present the most basic element for code reuse and actual organization of information which is the use of functions. If you have one, 3 or 20 blank lines where it should not make no difference.

The PEP 8, which is just a suggestion box for style code, although code formatted strictly according to it is rather more beautiful to look at, it is especially draconian blank lines. p>

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

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

And that's basically it. Since your code sample does not even have a function, you can not "show more practice". Now, I insist that if it is not yet your own in programming everything in functions, that's where you should focus if you want to perfect your programming style.

    
16.01.2018 / 18:22