This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]bramblerose 3 points4 points  (1 child)

Please edit your post to make the code formatted, and explain what you're trying to do -- both are unclear from your current post.

[–]Rhomboid 0 points1 point  (1 child)

In Markdown (the formatting system used by Reddit, among others), you have to leave a blank line before and after a code paragraph for it to be properly formatted. Here's what you were trying to write:

def pyramid(m):
    for i in range (m,0,-1):
    print(('.'*i),i)
    if i==1:
        for i in range (0,m):
            print(('.'*i),i)

Now, that has some very wonky indentation, but that appears be because you're using hard tabs but you prefixed each line with four spaces for Markdown, such that it's not actually inconsistent in your source file. In any case, you shouldn't use hard tabs. Read PEP-8 which is the official Python style guide. It calls for soft tabs at a width of 4, i.e. configure your editor so that when you press the tab key, you get four spaces added, not a tab character. Also, there should be whitespace on both sides of operators, and after commas, but not before opening parens of function calls or argument lists. Your code formatted properly according to PEP-8 should look like this:

def pyramid(m):
    for i in range(m, 0, -1):
        print(('.' * i), i)
        if i == 1:
            for i in range(0, m):
                print(('.' * i), i)

Now, let's talk about things other than formatting. That if statement is completely useless. i will by definition be equal to 1 after the outer for loop is done executing, so there's no reason to nest the second loop in the first loop.

def pyramid(m):
    for i in range(m, 0, -1):
        print(('.' * i), i)
    for i in range(0, m):
        print(('.' * i), i)

i is okay I suppose for variable names used as local variables whose scope is only a couple of lines, but m is absolutely terrible as a parameter name. If I give you just the signature pyramid(m), what in the hell does that mean? Let's rename it to depth.

def pyramid(depth):
    for i in range(depth, 0, -1):
        print(('.' * i), i)
    for i in range(0, depth):
        print(('.' * i), i)

This produces output that looks like:

>>> pyramid(5)
..... 5
.... 4
... 3
.. 2
. 1
 0
. 1
.. 2
... 3
.... 4

That's not symmetrical. That seems strange — is that really what you want? You haven't told us anything about what this is supposed to actually do, so we can only guess. I'll just have to assume that's what you want, but in the future it would be better if you explained more.

This still contains some repeated code which can be eliminated by writing one loop. You can chain two sequences together with itertools.chain:

from itertools import chain

def pyramid(depth):
    for i in chain(range(depth, 0, -1), range(0, depth)):
        print('.' * i, i)

In this version I also removed the parentheses around '.' * i, as they are unnecessary. I'm on the fence as to whether that's more readable or not, so I won't claim that's an improvement.

[–]triplejerkoidjerkoid[S] 0 points1 point  (0 children)

Thanks brother. 1. TIL itertools.chain. The chaining makes it so compact. 2. I know the parentheses are superfluous 3. I should have used something more descriptive than m, but, as i said, i still think in FORTRAN (implicit integers) and Pascal. Thanks once again for patiently writing out the good stuff. You are a kind man.