IndifferentFento
IndifferentFento

Reputation: 9

Can't get output to rotate 180 degrees

I need to make a nested triangle of hash symbols. But I can't get it to rotate 180 degrees like I need it to, I am also not getting the 12 rows like I need to.

This is my code.

n = int(input("Enter a number: "))
for i in range (0, n):
    for j in range(0, i + 1):
        print("#", end='')
    print("")
for i in range (n, 0, -1):
    for j in range(0, i -1):
        print("#", end='')
    print("")

The Input value is 6.

Enter a number:6
     #
    ##
   ###
  ####
 #####
######
######
 #####
  ####
   ###
    ##
     #

But I keep getting this:

Enter a number: 6
#
##
###
####
#####
######
#####
####
###
##
#

How do I fix this?

Upvotes: 0

Views: 163

Answers (4)

ErdoganOnal
ErdoganOnal

Reputation: 880

Also this works fine

num = 6
sing = "#"
for i in range(1, num * 2):
    if i > num:
        spaces = " " * (i - num)
        signs = sign * (2 * num - i)
        line = "{0}{1}".format(spaces, signs)
    elif i == num:
        spaces = " " * (num - i)
        signs = sign * i
        line = "{0}{1}\n{0}{1}".format(spaces, signs)
    else:
        spaces = " " * (num - i)
        signs = sign * i
        line = "{0}{1}".format(spaces, signs)

    print(line)

Upvotes: 0

blhsing
blhsing

Reputation: 106445

You can use the str.rjust method to align a string to the right:

n = int(input("Enter a number: "))
for i in range(2 * n):
    print(('#' * (n - int(abs(n - i - 0.5)))).rjust(n))

Demo: https://ideone.com/27AM7a

Upvotes: 1

Djaouad
Djaouad

Reputation: 22766

You can use this (manually calculating number of spaces and hashtags):

n = int(input("Enter a number: "))
for i in range (1, n + 1):
    print(" "*(n - i) + "#"*i)
for i in range (n, 0, -1):
    print(" "*(n - i) + "#"*i)

Or use rjust:

n = int(input("Enter a number: "))
for i in range (1, n + 1):
    print(("#"*i).rjust(n))
for i in range (n, 0, -1):
    print(("#"*i).rjust(n))

Upvotes: 0

Billy
Billy

Reputation: 1

A bit late

def absp1(x):
    if (x < 0):
        return -x - 1
    return x

n = int(input("Enter a number: "))
for i in range(-n, n):
    for j in range(absp1(i)):
        print(' ', end='')
    for k in range(n-absp1(i)):
        print('#',end='')
    print()

Upvotes: 0

Related Questions