Memory error Python Staircase -
hi i'm writing code grok learning(website) test i've been testing code on mac(messed schools should using pc) , nothing seems appear 1 min web page reloads , appears in area code displays it's running form:
traceback (most recent call last): file "program.py", line 6, in <module> print(" " * number + "|") memoryerror
can understanding i've never seen before? (code below)
a=0 number = int(input("how many steps? ")) print("__") if number != 1: while < number: print(" " * number + "|") = + 1 print(" " * number + "_") number = number * 2 elif number == 1: print("__|")
the code making staircase using underscores , pipe characters user's input(screen shot of explanation included)
so assuming desired output input of 4:
how many steps? 4 __ |__ |__ |__ ___________|
you can this:
number = int(input("how many steps? ")) print("__") in range(1, number): offset = a*2 + - 1 print(" " * (offset) + '|' + ('__' if < number else '')) offset = number*2 + number - 1 print("_" * (offset) + '|')
basically can rid of number != 1
check @ beginning since should behave identically input. loop on number of steps. @ each step offset, twice step number (a), plus offset previous pipe chars. check if there step after one, can put underscores on same line. finally, on last step (which outside loop), put underscore instead of whitespace , final pipe char
Comments
Post a Comment