6
\$\begingroup\$

So basically, a user is prompted to input a number of stairs and a staircase when a guy on it is generated. I know this is sloppy and my variable names are rough, I'm actively working to change them now. I thought I would go ahead and post the working code though.

How can I improve on this? What can I do better to do more with less?

Note: I'm very new to Python and general programming.

def render(num, num3):
    num2 = num - 1
    white_space7 = ""

    white_space = ""
    for x in range(num):
        white_space = white_space + "       "
    c = num
    white_space5 = makeSpace(Num - num2)
    if num3 != 0:
        white_space7 = makeFloor(num3)
        print(white_space + "      *" + white_space5 + "*")
        print(white_space + "   o  *" + white_space5 + "*")
        print(white_space + "  /|\ *" + white_space5 + "*")
        print(white_space + "  / \ *" + white_space5 + "*")
        print(white_space + "*******" + white_space7 + "*")
    else:
        print(white_space + "      *" + white_space5 + "*")
        print(white_space + "   o  *" + white_space5 + "*")
        print(white_space + "  /|\ *" + white_space5 + "*")
        print(white_space + "  / \ *" + white_space5 + "*")
        print(white_space + "*******" + white_space7 + white_space5 + "*")

def makeSpace(num):
    white_space4 = ""
    for x in range(num):
        white_space4 = white_space4 + "       "
    return white_space4

def makeFloor(num):
    white_space6 = ""
    num4 = num + 1
    for x in range(num4):
        white_space6 = white_space6 + "*******"
    return white_space6



Num = raw_input("Number of Stairs:" )
Num = int(Num)

white_space2 = ""
for x in range(Num + 1):
    white_space2 = white_space2 + "       "
print(white_space2 + "*******")
y = Num

white_space2 = ""


for x in range(Num):
    if (x < Num - 1):
        render(y,0)
        y -= 1
    else:
        render(y,Num - 1)
        y -= 1
\$\endgroup\$
1
  • \$\begingroup\$ Have you heard of string formatting? You could use {} to place the string at its place \$\endgroup\$
    – llrs
    Commented Oct 19, 2015 at 20:38

1 Answer 1

5
\$\begingroup\$

Conciseness

Python overloads operators, that is * works differently for numbers or numbers and strings. You can use it to shorten some functions of yours:

def makeSpace(num):
    return " " * 7 * num

def makeFloor(num):
    return "*" * 7 * (num + 1)

Global typo

white_space5 = makeSpace(Num - num2)
                         ^

That n should be lowercase as uppercase it refers to a global and referring to a global variable from a function is very bad.

Use your functions

You can use makeSpace once more in render:

def render(num, num3):
    num2 = num - 1
    white_space7 = ""

    white_space = makeSpace(num)
    white_space5 = makeSpace(num - num2)
    # ...

The same goes for the top level code (that should be put in a main function for modularity)

def main():

    Num = raw_input("Number of Stairs:" )
    Num = int(Num)

    white_space2 = makeSpace(Num + 1)
    print(white_space2 + "*******")
    # ...

Variable names

Naming the variables and arguments num or num3 or Num is a very bad idea as it does not tell me about the meaning of what is contained inside. I suggest, Num -> staircase_height as an example but all the names can and should be improved.

Avoid reassigning the same variable

You re-assign white_space7 and that gave me headaches when refactoring, reassigning is generally avoided in Python.

Simplify branching

The printing part in render:

print(white_space + "      *" + white_space5 + "*")
print(white_space + "   o  *" + white_space5 + "*")
print(white_space + "  /|\ *" + white_space5 + "*")
print(white_space + "  / \ *" + white_space5 + "*")
if num3 != 0:
    print(white_space + "*******" + white_space7 + "*")
else:
    print(white_space + "*******" + white_space5 + "*")

Extract common code out of branches.

and

for x in range(staircase_height - 1):
    render(staircase_height - x, 0)
render(staircase_height - (x + 1), staircase_height - 1)

Use less variables

You can use a simple subtraction in the loop to easily avoid the y variable:

for x in range(staircase_height):
    if (x < staircase_height - 1):
        render(staircase_height - x,0)
    else:
        render(staircase_height - x,staircase_height - 1)

and:

print(makeSpace(staircase_height + 1) + ("*" * 7))

Further simplify render

Simply right_pad = makeSpace(1), not the long winded expression that always evaluated to one.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.