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.
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))