Printing a pyramid pattern inverse order in python - python

I am trying to print out this:
**********
*********
********
*******
******
*****
****
***
**
*
But somehow I get it in the inverse order like upside down
This is my code. I am very confused since the things that I can use are very restrained. I must not use any String method, for loops, slicing-index, or "*" * n, something like that. All I can do is using while and if-else cases.
This is my code, I delve deep into it with trying not to make it useless.
outer = 1
while outer <= 10:
inner = outer
pos = 10
while pos >= 1:
if pos > inner:
print(" ", end=" ")
pos = pos - 1
else:
print("*", end=" ")
inner = inner - 1
pos = pos - 1
print(" ")
outer = outer + 1
My output is
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
Thanks.

Here's a more simplified way to do it:
n = 10
while n > 0:
i = 10 - n
m = n
g = 1
while m <= 10:
print(" ", end="")
m += 1
while g < m - i:
print("*", end="")
g += 1
print()
n -= 1
Output:
**********
*********
********
*******
******
*****
****
***
**
*

I made another version that I think will work
def pyramid(n):
i = 0
while i < n:
k = 0
while k < (i):
print(" ", end="")
k += 1
j = i
while j < n:
print("*", end="")
j +=1
print()
i += 1
pyramid(10)

Related

Can i avoid to print new line between 2 for loops in Python

I'm trying to display a diamond shape depending on the input. Code almost worked done except 'empty new line'.
But I didn't eliminate the empty line between 2 loops. How can I fix it? Is there something that escaped my attention?
def print_shape(n):
for i in range(0,n):
print('*' * (n-i), end='')
print(' ' * (i*2), end='')
print('*' * (n-i), end='\n')
for k in range((n), (2 * n + 1)):
print('*' * (k - n), end='')
print(' ' * ((4 * n) - (2 * k)), end='')
print('*' * (k - n), end='\n')
n = int(input('Enter a number: '))
print_shape(n)
Enter a number: 5
**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
**********
Need to do this one:
**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
**********
Yes, that's very easy. Just start from n+1 instead of n in the bottom loop (it's the one printing the unwanted newline) like so
def print_shape(n):
for i in range(0,n):
print('*' * (n-i), end='')
print(' ' * (i*2), end='')
print('*' * (n-i), end='\n')
for k in range(n + 1, (2 * n + 1)): # <--- here
print('*' * (k - n), end='')
print(' ' * ((4 * n) - (2 * k)), end='')
print('*' * (k - n), end='\n')
n = int(input('Enter a number: '))
print_shape(n)
Try this
def print_shape(n):
for i in range(0,n):
print('*' * (n-i), end='')
print(' ' * (i*2), end='')
if i == n-1:
print('*' * (n-i), end='')
else:
print('*' * (n-i), end='\n')
for k in range((n), (2 * n + 1)):
print('*' * (k - n), end='')
print(' ' * ((4 * n) - (2 * k)), end='')
print('*' * (k - n), end='\n')
print_shape(5)

How do I remove this extra kite from the diamond in this code?

I have written a python program for printing a diamond. It is working properly except that it is printing an extra kite after printing a diamond. May someone please help me to remove this bug? I can't find it and please give a fix from this code please.
CODE:
limitRows = int(input("Enter the maximum number of rows: "))
maxRows = limitRows + (limitRows - 1)
while currentRow <= limitRows:
spaces = limitRows - currentRow
while spaces > 0:
print(" ", end='')
spaces -= 1
stars = currentRow
while stars > 0:
print("*", end=' ')
stars -= 1
print()
currentRow += 1
while currentRow <= maxRows:
leftRows = maxRows - currentRow + 1
while leftRows > 0:
spaces = limitRows - leftRows
while spaces > 0:
print(" ", end='')
spaces -= 1
stars = leftRows
while stars > 0:
print("*", end=' ')
stars -= 1
print()
leftRows -= 1
currentRow += 1
OUTPUT(Case 1):
D:\Python\diamond\venv\Scripts\python.exe D:/Python/diamond/main.py
Enter the maximum number of rows: 4
*
* *
* * *
* * * *
* * *
* *
*
* *
*
*
Process finished with exit code 0
OUTPUT(Case 2):
D:\Python\diamond\venv\Scripts\python.exe D:/Python/diamond/main.py
Enter the maximum number of rows: 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
* * *
* *
*
* *
*
*
Process finished with exit code 0
You have an extra while loop in the second half of your code.
Try this
limitRows = int(input("Enter the maximum number of rows: "))
maxRows = limitRows + (limitRows - 1)
currentRow = 0
while currentRow <= limitRows:
spaces = limitRows - currentRow
while spaces > 0:
print(" ", end='')
spaces -= 1
stars = currentRow
while stars > 0:
print("*", end=' ')
stars -= 1
print()
currentRow += 1
while currentRow <= maxRows:
leftRows = maxRows - currentRow + 1
spaces = limitRows - leftRows # Removed unnecessary while loop here
while spaces > 0:
print(" ", end='')
spaces -= 1
stars = leftRows
while stars > 0:
print("*", end=' ')
stars -= 1
print()
leftRows -= 1
currentRow += 1
You can make this less complex by using just one loop as follows:
def make_row(n):
return ' '.join(['*'] * n)
rows = input('Number of rows: ')
if (nrows := int(rows)) > 0:
diamond = [make_row(nrows)]
j = len(diamond[0])
for i in range(nrows-1, 0, -1):
j -= 1
diamond.append(make_row(i).rjust(j))
print(*diamond[::-1], *diamond[1:], sep='\n')
Example:
Number of rows: 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

How to print a rectangle shape thru multiplication signs in python?

long = int(input("longueur : "))
larg = int(input("largeur : "))
for i in range(larg):
if i == 0 or i == larg - 1:
for j in range(long):
print("* ", end="")
print()
else:
for j in range(long):
if j == 0 or j == long - 1:
print("* ", end="")
else:
print(end=" ")
print()
I was trying to achieve a rectangle shape through symbols .
I started learning Python recently and I'm searching for a faster/cleaner way to do this .
the output requested is:
* * * *
* *
* * * *
Here is a clean and simple approach...
length = int(input("length: "))
width = int(input("width: "))
for i in range(length):
print("* " * width)
Well, you could use multiply with your multiplication signs:
rows = 3
columns = 5
print(('* ' * columns + '\n') * rows, end = '')
Output:
* * * * *
* * * * *
* * * * *
If you want to print a hollow rectangle, it's a little more complicated as you need different rows in the middle. For example:
for r in range(rows):
if r == 0 or r == rows - 1:
print('* ' * columns)
else:
print('* ' + ' ' * (columns - 2) + '* ')
Output:
* * * * *
* *
* * * * *
You need to use a loop to iterate through each row. If the row is first or last then fill it with column * '*'. If the row is the middle one then add an '*', fill with appropriate spaces and finally add an '*'.
Here's how you can do it:
long = int(input("longueur : "))
larg = int(input("largeur : "))
for i in range(long):
if (i == 0) or (i == long - 1): # Checking if it is first or last row
print('* ' * larg) # Printing entire row without gap
else: # For middle rows
print('* ' + ' ' *(larg - 2) + '*') # Printing middle rows with gap in between.
Output:
longueur : 3
largeur : 4
* * * *
* *
* * * *
longueur : 4
largeur : 7
* * * * * * *
* *
* *
* * * * * * *
You can try the good old basic for loop
rows = 3
columns = 3
for i in range(rows):
for j in range(columns):
print('*', end=' ')
print()
will give
* * *
* * *
* * *
as per your updated question for hollow
for i in range(1, rows + 1):
for j in range(1, columns + 1):
if i == 1 or i == rows or j == 1 or j == columns:
print("*", end=" ")
else:
print(" ", end=" ")
print()
will give
* * *
* *
* * *

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

Asterisks Triangle in Python

I am trying to create a program that allows me to make a "pyramid" or "triangle" using asterisks, in the Python program. I've already started the code, but can't seem to figure it out.
Here's the code I've managed to figure out:
def triangle():
totalRows = int(eval(input("How big? ")))
for currentRows in range(1,totalRows+1):
for currentCol in range (1, currentRows+1):
print("*", end = " ")
triangle()
The end result is supposed to mirror this!
How big? 1
*
------------------------------------------------------
How big? 2
*
* *
------------------------------------------------------
How big? 3
*
* *
* * *
------------------------------------------------------
Slight modification to RParadox's solution to match requirement:
for i in range(totalRows + 1):
numWhite = totalRows - i
print ' ' * numWhite + '* ' * i
n = 10
for i in range(n-1):
numwhite = n-i
print ' '*numwhite + '*'*i + '*'*i
**
****
******
********
**********
************
**************
****************
def build_pyr(depth):
rows = [ (depth-i)*' ' + i*2*'*' + '*' for i in range(depth-1) ]
for i in rows:
print i
This works, however, it adds 2n + 1 asterisks at each depth. Just remove the + 1 asterisks from the list comprehension and add an initial asterisk to the row list before hand.
kiko="*"
empty=" "
def star(n):
for i in range(n):
print((n-i-1)*empty+(i+i+1)*kiko)
star(5)
def xmastree(maxwidth):
for i in xrange(1,maxwidth,2):
print '{0}'.format('*'*i).center(maxwidth)
or:
def xmastree2(maxrows):
for i in xrange(1,maxrows*2,2):
print '{0}'.format('*'*i).center(maxrows*2)
hmm, still no response, maybe not generic enough ? ok try this ;-) :
def triangle(pattern, n):
maxwidth = n*len(pattern)*2
for i in xrange(1,n*2+1,2):
print '{0}'.format(pattern*i).center(maxwidth)
>>> triangle(' ^', 5)
^
^ ^ ^
^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^
Woah, guys, you are going at it from a really complicated point of view! why don'y you just use this program? :
asterisknum = int(raw_input('How many asterisks? Input here: '))
base = 0
if asterisknum % 2 == 0:
print ('Added 1 to even number')
asterisknum = asterisknum + 1
while asterisknum != -1 :
print (' ' * base + '*' * asterisknum)
base = base + 1
asterisknum = asterisknum - 2
raw_input('Press <Enter> to exit')
I just made this program to work once, but I used the extremely simple parts of python that everybody should know. It could be tweaked to work again in the same program, or whatever...
def triangle(height):
maxstars = 1+ 2*(height -1)
spaces = int(maxstars/2)
for i in range(0,height):
print(" " * spaces +"*" * (1+ 2*i))
spaces = spaces -1
number = int(input("Please enter height of triangle: "))
triangle(number)
Imagine you have "blocks" of stars and space. Now add them on a canvas.
This is Python.
It glues objects together in this particular case
This code prints a diamond, the first loop is the upper half, the second loop is the lower half.
Note that i had to make new variables(objects) for the second loop.
Hope this helps guys :)
star = "*"
space = "." # dots are for presentation purposes
rows = 10
star_counter = 1
space_counter = rows
for i in range(rows):
print((space * (space_counter - 1 )) + (star * star_counter) +
(star * (star_counter - 1)) + (space * (space_counter - 1)) )
star_counter += 1
space_counter -= 1
star_counter_new = (rows - 1) # one row less for the lower triangle
# the upper triangle takes the base row
# that's why rows - 1
space_counter_new = 1
for j in range(rows - 1): # same goes here
print((space * (space_counter_new)) + (star * (star_counter_new)) +
(star * (star_counter_new - 1)) + (space * (space_counter_new )))
space_counter_new += 1
star_counter_new -= 1
it can be done in just few steps:
def main():
rows = input(" enter the number of rows : ")
for i in range (rows):
print ' '*(rows-i-1) + '*'*(2*i+1)
main()
def pypart2(n):
k = 2*n-2
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k=k-1
for j in range(0, i+1):
print("* ", end="")
print("\r")
n = 5
pypart2(n)

Categories