Search This Blog

Tuesday, March 25, 2014

Print a hollow diamond - with numbers


if __name__ == "__main__" :
    try:
        act_size = int(input("Enter the size of the diamond : "))
        size=1
        size_max = 2*act_size - 1
        level=1
        i_size = 2 * act_size
        for j in range(0,i_size):
            pos=1         
            for i in range(i_size):
                if pos > act_size - level+1 and pos <=act_size+level-1 and level<=act_size :
                    print(" ", end='')
                elif pos <= act_size and size < 2*act_size and level<=act_size :
                    print(size, end='')
                    size += 2
                elif pos > act_size and pos <= i_size and level<=act_size :
                    size -= 2
                    print(size, end='')
                if level > act_size and pos <= level - act_size:                 
                    print(size_max,end='')
                    size_max += 2                   
                elif level > act_size and pos > i_size - level + act_size :
                    size_max -= 2
                    print(size_max,end='')                   
                elif level > act_size:
                    print(" ",end='')
               
                pos += 1
            size_max = 2*(i_size-level)-1
            size = (2*level) + 1
            level += 1
            print("")
               
    except :
        print("ERROR: Only numbers are accepted")



 

No comments:

Post a Comment