Can't get output to rotate 180 degrees - python

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?

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

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

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

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)

Related

Write a program that prompts the user to enter an integer number from 1 to 9 and displays two pyramids

I have the code for the 1st and the second pyramid, I just don't know how to put it together like how the question is asking. The first code below is for pyramid 1 and second is for the 2nd pyramid.
`
rows = int(input("Enter number of rows: "))
k = 0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(end=" ")
while k!=(2*i-1):
print("* ", end="")
k += 1
k = 0
print()
`
`
rows = int(input("Enter number of rows: "))
k = 0
count=0
count1=0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(" ", end="")
count+=1
while k!=((2*i)-1):
if count<=rows-1:
print(i+k, end=" ")
count+=1
else:
count1+=1
print(i+k-(2*count1), end=" ")
k += 1
count1 = count = k = 0
print()
`
You just need to run the second loop after the first loop. Also your code for the second pyramid is incorrect so I changed that.
rows = int(input("Enter number of rows: "))
k = 0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(end=" ")
while k!=(2*i-1):
print("* ", end="")
k += 1
k = 0
print()
k = 0
count=0
count1=0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(" ", end="")
count+=1
for k in range(i,0,-1):
print(k, end=' ')
for k in range(2,i+1):
print(k, end=' ')
count = 0
print()
Since the algorithm for constructing both pyramids is similar, it is better to make a function
def pyramid(n_rows, s_type='*'):
max_width = n_rows * 2 - 1
for i in range(0, n_rows):
stars = i * 2 + 1
side = (max_width - stars) // 2
if s_type == '*':
print(f"{' ' * side}{'* ' * stars}")
else:
side_nums = ' '.join(f'{x+1}' for x in range(0,i+1))
print(f"{' ' * side}{side_nums[1:][::-1]}{side_nums}")
rows = int(input("Enter number of rows: "))
pyramid(rows)
pyramid(rows, s_type='0')
You only need to input the row count once. You may also find using str.rjust easier for formatting.
rows = int(input('Enter a number from 1 to 9: '))
assert rows > 0 and rows < 10
output = [None] * (rows*2)
stars = '*' * (rows * 2 - 1)
for i in range(rows):
output[i] = stars[:i*2+1].rjust(rows+i)
s = ''.join(map(str, range(1, i+2))) + ''.join(map(str, range(i, 0, -1)))
output[i+rows] = s.rjust(rows+i)
print(*output, sep='\n')
Output:
Enter a number from 1 to 9: 6
*
***
*****
*******
*********
***********
1
121
12321
1234321
123454321
12345654321

I am a beginner in python. Can someone help me to get a star pattern like this?

I have tried these steps but I don't know what to do after trying them.
num = int(input("Enter an integer number greater than or equal to 1: "))
if num > 1:
for x in range(0, num):
for y in range(0, x+1):
print("*", end = " ")
print("")
for x in range(num,1,-1):
for y in range(0,x-1):
print("*", end=" ")
print("")
The pattern I would like to get is
*
**
***
**
*
**
*
*
Hope this does the job. The outer for loop is supposed to decrease the height of a hill by one at every iteration and the inner two for loops print the hill for every height.
num = int(input("Enter an integer number greater than or equal to 1: "))
for height in range (num, 0, -1):
for i in range (1, height):
print ('*'*i)
for j in range (height, 0, -1):
print ('*'*j)

Create a 2d array that prints out a snowflake PYTHON

I am trying to create a piece of code that will accept a odd number as a input and create a snowflake using this graph of n * n
Enter Integer: 5
* . * . *
. * * * .
* * * * *
. * * * .
* . * . *
Im pretty sure Im on the the right track
n = int(input("Enter odd input: "))
while n % 2 == 0:
print("Invalid Input. Input must be odd")
n = int(input("Enter odd input: "))
snowflake = [["."] * n for i in range(n)]
middle = int((n-1) / 2)
for i in range(n):
snowflake[i][2] = "*"
snowflake[2][i] = "*"
snowflake[i][i] = "*"
diagnol = 5-i
snowflake[i][diagnol] = "*"
for i in snowflake:
for j in i:
print(j, end=' ')
print()
print()
But I keep getting this error
snowflake[i][diagnol] = "*"
IndexError: list assignment index out of range
Can someone help edit my code or give me a tip?(This is a homework assignment)
I decided not to fix your algorithm, but as a working example to provide my own algorithm:
Try it online!
n = 9
a = [['.'] * n for i in range(n)]
for i in range(n):
a[n // 2][i], a[i][n // 2], a[i][i], a[i][n - 1 - i] = ['*'] * 4
print('\n'.join([''.join(a[i]) for i in range(n)]))
Output:
*...*...*
.*..*..*.
..*.*.*..
...***...
*********
...***...
..*.*.*..
.*..*..*.
*...*...*
After debugging it I found that 5 was too big and that some of the code would only work if the input was 5
n = int(input("Enter odd input: "))
while n % 2 == 0:
print("Invalid Input. Input must be odd")
n = int(input("Enter odd input: "))
snowflake = [["."] * n for i in range(n)]
middle = int((n-1) / 2)
for i in range(n):
snowflake[i][middle] = "*"
snowflake[middle][i] = "*"
snowflake[i][i] = "*"
diagnol = n -1 -i
snowflake[i][diagnol] = "*"
for i in snowflake:
for j in i:
print(j, end=' ')
print()
print()

No output when using nested for loops inside if statements

I'm trying to print out a series of patterns based off of the user input. However, when I add an if statement or while loop to it, I get no output even if I selected the correct number. The patterns work if I don't add the loops to it. I'm new to python and don't understand why it isn't printing anything out.
num_draw = input("Please enter the number of the design you would like[1-6] or -1 to quit: ")
#while num_draw != -1:
if num_draw == 1:
for i in range(0, 5):
for j in range(0, i+1):
print("* ",end="")
print()
elif num_draw == 2:
#2
size = 5
isize = size - 2
print ('*' * size)
for i in range(isize):
print ('*' + ' ' * isize + '*')
print ('*' * size)
elif num_draw == 3:
for i in range(5):
for j in range(5):
print(" *"[(j + i + 1)%2], end=' ')
print()
elif num_draw == 4:
for i in range(0, 5):
for j in range(0, i+1):
print("* ",end="")
print()
elif num_draw == 5:
for i in range(0, 5):
for j in range(5, i, -1):
print("# ", end="")
print()
elif num_draw == 6:
k = 8
for i in range(0, 5):
for j in range(0, k):
print(end=" ")
k = k - 2
for j in range(0, i+1):
print("* ", end="")
print()
You were almost correct. The problem was that the keyboard input is always of type str (string, by default) and you are comparing it to numbers of type int (integer). For ex. if you enter 1, then num_draw='1' and so you are checking if '1' == 1: which is False. Similarly, none of your if or elif is True because you are comparing str type with int type.
To make your code work, convert your input type to int as follows:
num_draw = int(input("Please enter the number of the design you would like[1-6] or -1 to quit: "))
Output
Please enter the number of the design you would like[1-6] or -1 to quit:
1
*
* *
* * *
* * * *
* * * * *
As mentioned your input needs to be an int to make proper comparisons. As for your while loop while num_draw != -1: is correct but you need to move your input prompt within the loop so user can reelect an option. setting num_draw = 'x' is just to create a qualifying condition to start the loop
num_draw = 'x'
while num_draw != -1:
num_draw = int(input("Please enter the number of the design you would like[1-6] or -1 to quit: "))

Console print a scaling square with roatated square in the middle

I really am not sure how to explain my question in a title; a graphic will be best.
I've been given this problem as something to play around with in class, so far only one person has managed to achieve a solution, and it's a very complex one.
While I've spoken to him (The best we can manage, his english isn't great), I'd like to come up with my own solution, but I need some pointers, or at least new ideas..
The problem is this:
n=5
0 3 5 5 3 0
3 5 5 3
5 5
5 5
3 5 5 3
0 3 5 5 3 0
With 'n' being an input value.
So far I've got this;
#!/usr/bin/env python3
while True:
n = int(input("Enter a size : "))
z = "+"
for i in range(n*2): # ROWS
for j in range(n*2): # COLUMNS
if i == 0 or j == 0 or i == n*2 - 1 or j == n*2 - 1: # OUTLINE
print(z, end=" ")
elif j < n-i: # TOP LEFT
print(z, end=" ")
elif j >= n+i or i >= n+j: # TOP RIGHT + BOTTOM LEFT
print(z, end=" ")
elif j >= n*2-i+n-1 and i >= n*2-j+n-1: # BOTTOM RIGHT
print(z, end=" ")
else:
print(" ", end=" ")
print()
Whitch outputs this;
Enter a size : 5
+ + + + + + + + + +
+ + + + + + + +
+ + + + + +
+ + + +
+ +
+ +
+ + + +
+ + + + + +
+ + + + + + + +
+ + + + + + + + + +
The next step is to replace "z" with an equation for the places in the box I guess. But I've no idea where to start (And my math is a little rusty)
Taking a guess, I suppose this is what you mean:
def square(n):
def row(i, n):
l = [str(x) if x <= n else ' ' for x in range(i, i+n)]
return l + l[::-1]
top = [row(i, n) for i in range(1, n+1)]
return '\n'.join(' '.join(r) for r in (top + top[::-1]))
while True:
n = int(input("> "))
print(square(n))
I solved it, and not in a way I was expecting, tbh. I'd call it a dirty hack.
#!/usr/bin/env python
__author__ = "Luke Jones"
__copyright__ = "Copyleft, do what you want with it"
__license__ = "GPL"
__version__ = "0.0.1"
while True:
startV = int(input("Enter a size : "))
array1=[]
array2=[]
for i in range(1,startV+1,2):
array1.append(i)
array2 = list(array1)
array2.reverse()
n = len(array1)
for i in range(n*2): # ROWS
for j in range(n*2): # COLUMNS
if j < n-i: # TOP LEFT
print(array1[j+i], end=" ")
elif j >= n+i: # TOP RIGHT
print(array2[j-n-i], end=" ")
elif i >= n+j: # BOTTOM LEFT
print(array2[i-n-j], end=" ")
elif i >= n*2-j+n-1 and not i >= n*2-j+n:
#print("X",end=" ")
for q in range(n*2-j):
print(array2[q], end=" ")
else:
print(" ", end=" ")
print()

Categories