#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Help needed displaying a stair step pattern


    Hi, This program works, but is slightly off. The output is coming out different than what I am trying to achieve. Any helpful suggestions? Thanks.

    #this program displays a stair-step pattern.


    num_steps = 6

    for r in range(num_steps):
    for c in range (r):
    print(' ', end='')
    print ('#')
    print ('#')

    Output from code above
    >>>
    #
    #
    _ #
    ___#
    ____#
    #
    ___#
    #
    ____#
    #__
    ____ #
    #
    >>>
    This is the pattern I am trying to get it to display. (where the underscores are should be black spaces.
    ##
    #_#
    # __#
    #____ #
    #______ #
    #_______ #
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    I'm not all that familiar with Python 3 (which it seems you're using) but I think the problem is in the first of your 2 "print("#")"s. That is, the default is to add a linefeed so you should have "print("#", end="")" or whatever is the proper syntax.

    Also, it helps if you use code tags and thus preserve the indentation (although in this case it's fairly transparent).

    By the way, in Python 2.x, I'd do it like this (where "n" is your numsteps):
    Code:
    >>> for i in xrange(n):
    ...   print "#"+i*"_"+"#"
    ##
    #_#
    #__#
    #___#
    #____#
    #_____#
    Last edited by rrashkin; February 14th, 2013 at 01:15 PM. Reason: on second thought

IMN logo majestic logo threadwatch logo seochat tools logo