I want to make a function to print triangle like the following picture. User can insert the row number of the triangle. The total lenght of first row is must an odd.
I try to use below code :
def triangle(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
triangle(n)
Here is the expected output image :
and here is my actual output image :
but I can't remove the star middle star. And it's not Upside - Down Triangle
You could try a different way.
def triangle(n) :
for i in range(1,n+1) :
for j in range(1,i) :
print (" ",end="")
for j in range(1,(n * 2 - (2 * i - 1))
+1) :
if (i == 1 or j == 1 or
j == (n * 2 - (2 * i - 1))) :
print ("*", end="")
else :
print(" ", end="")
print ("")
n = 5
triangle(n)
Not sure how cool implementation is this but it gives results:
def triangle(n):
print(' '.join(['*']*(n+2)))
s = int((n/2)+1)
for i in range(s):
star_list = [' ']*(n+2)
star_list[-i-2] = ' *'
star_list[i+1] = '*'
print(''.join(star_list))
n = 5
triangle(n)
Output:
* * * * * * *
* *
* *
*
for n = 7:
* * * * * * * * *
* *
* *
* *
*
I would try a recursive solution where you call the printTriangle() function. This way, it will print the point first and move it's way down the call stack.
Related
I would like to write a python program to print the above shape( I am new to python)
but I have write the program of single diamond and now I have a problem to solve this,
would u guide to find the algorithm?
* *
*** ***
**********
*** ***
* *
this is the single diamond:
def Diamond(rows):
n = 0
for i in range(1, rows + 1):
for j in range (1, (rows - i) + 1):
print(end = " ")
while n != (2 * i - 1):
print("*", end = "")
n = n + 1
n = 0
print()
k = 1
n = 1
for i in range(1, rows):
for j in range (1, k + 1):
print(end = " ")
k = k + 1
while n <= (2 * (rows - i) - 1):
print("*", end = "")
n = n + 1
n = 1
print()
rows = int(input())
Diamond(rows)
I was bored, here you go.
In [36]: def print_diamonds(width, ds):
...: r = width//2
...: for i in range(-r, r+1):
...: print((' '*(abs(i)) + '*'*((r-abs(i))*2+1) + ' '*(abs(i)))*ds)
...:
In [37]: print_diamonds(5, 2)
* *
*** ***
**********
*** ***
* *
Your question is vague but here is a function for a single diamond per line. I'm not sure what do you expect. Be mor explicite.
vect = ('*', '***', '*****', '***', '*')
def method():
for i in range(0,5):
print(abs((2-i))*" ",vect[i])
I am trying to draw the following pattern in Python:
# #
## ##
### ###
########
I can get the two right triangles separately but cannot figure out how to make them into one. Would anone be able to help me?
My code for the left triangle is:
rows = 4
for i in range(0, rows):
for j in range(0, i+1):
print('#', end='')
print()
My code for the right triangle is:
for i in range(0,rows):
for j in range(0, rows-i):
print(' ',end='')
for k in range(0, i+1):
print('#',end='')
print()
I'm trying to combine them somehow but haven't been successful.
Here's one way to go about it. '#' * x prints a increasing number of '#'s and space [(2*x):] slices the eight spaces in the string space.
space = ' '
for x in range (1, 5) :
print ('#' * x + space [(2*x):] + '#' * x)
And here is a version without slicing.
y = 6
for x in range (1, 5) :
print ('#' * x, end = '')
if y > 0 : print (' ' * y, end = '')
print ('#' * x)
y = y - 2
This is a pattern program
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
the code for this program will be:
for i in range(0,7):
for j in range(0,i+1):
print("*",end=" ")
print("\r")
for m in range(5,-1,-1):
for n in range(0,m+1):
print("*",end=" ")
print("\r")
try using string formatting with spacers like so:
>>>print("{:<4}{:>4}".format('#','#'))
# #
>>>print("{:x^7}".format('#'))
xxx#xxx
#f-strings
x = '#'
>>>print(f'{x:>5}')
#
The formatting spacers add padding, to the left right, or both sides of a string
I ended up doing:
col=8
x=2
y=col-1
for i in range(1, col//2+1):
for j in range(1, col+1):
if(j>=x and j<=y):
print(' ', end='')
else:
print('#', end='')
x=x+1
y=y-1
print()
Here assuming no.of lines l = 4
The min space starts with s = 2.
No.of hashes to print based on lines end = l * 2.
l = 4
space = 2
end = l * 2
for i in range(1, l + 1):
print('#'*i, end='')
print(' '*(end - space), end='')
print('#'*i)
space = space + 2
I want to write a program that prints this output:
*
* *
* *
* *
*
But it prints this instead:
*
* *
* * *
* * * *
* * * * *
Suppose n = 5 is the input from the user, then the first star should be in the centre, and the ending star as well, so I can divide the input number by 2 to get the position of the first and the last star.
For the rest of the stars I am not understanding how to make it so that if one star is above then the next star should not be below it.
def Empty_triangle(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):
# printing stars
print("* ", end="")
# ending line after each row
print("\r")
# Driver Code
n = 5
Empty_triangle(n)
How about this sort of thing?
print("\n".join([" "*(n-2-i)+"*"+" "*(2*i-1)+("*"if i>0 else"") for i in list(range(n-1))+[0]]))
Which, for example, for n=5, gives this output:
*
* *
* *
* *
*
Is this the kind of thing you had in mind?
Here's a less code-golfish version
def Empty_triangle(n):
for i in list(range(n-1))+[0]: #i.e. [0,1,2,3,4,0] so the last line is the same as the first
line = ""
leadingSpaces = n-2-i
line += " "*leadingSpaces
line += "*"
if i != 0:
middleSpaces = 2*i-1
line += " "*middleSpaces
line += "*"
print(line)
# Driver Code
n = 5
Empty_triangle(n)
In Python, I'd like to print a diamond shape of asterisks *:
with $ at the top half of the diamond (upper pyramid) where there isn't a *, and
with & at the bottom half of the diamond (lower pyramid) where there isn't a *.
So far, I only know how to make a pyramid that is right side up:
def pyramid(n):
for i in range(n):
row = '*'*(2*i+1)
print(row.center(2*n))
For example, if the function called was print shape(7), then it would print [this image].
Any ideas?
def shape(n):
for i in range(2*n+ 1):
if (i < n):
print "$" * (n - i) + "*" * 2 * i + "$" * (n - i)
elif i == n:
print "*" * 2 * n
elif i > n:
print "&" * (i - n) + "*" * 2 * (2* n - i) + "&" * (i - n)
I'm trying to figure out how to turn my whole square into a hollow one. The few things I've tried so far haven't been very successful as I end up getting presented with a rather distorted triangle!
This is the code I have to form my square currently ..
size = 5
for i in range(size):
print ('*' * size)
When run, this is the result ..
*****
*****
*****
*****
*****
Do I need to run if or while statements when size is greater than 3 to specify a condition?
I think this is what you want to do:
m, n = 10, 10
for i in range(m):
for j in range(n):
print('*' if i in [0, n-1] or j in [0, m-1] else ' ', end='')
print()
Output:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
You can also draw a triangle this way:
m, n = 10, 10
for i in range(m):
for j in range(n):
print('*' if i in [j, m-1] or j == 0 else ' ', end='')
print()
Output:
*
**
* *
* *
* *
* *
* *
* *
* *
**********
You can print out a single '*', followed by size-2 spaces, then a single '*'. This will give you the "hollow" portion. The first and last lines need the full length:
size = 5
inner_size = size - 2
print ('*' * size)
for i in range(inner_size):
print ('*' + ' ' * inner_size + '*')
print ('*' * size)
Here is my python code for drawing a square by entered size N.
n = int(input())
print('*' * n)
for i in range(n-2):
print ('*' + ' ' * (n-2) + '*')
print('*' * n)
Basically the first and the last print('*' * n) are drawing the top and the bottom lines and the for cycle prints the body.
Output example: N=3
***
* *
***
Output example: N=5
*****
* *
* *
* *
*****
Try this:
size = 5
for i in range(1,size+1):
if (i==1 or i==size):
print("*"*size)
else:
print("*"+" "*(size-2),end="")
print("*")
output:
*****
* *
* *
* *
*****
Heres my example:
print("Enter width")
width = int(input())
print("Enter height")
height = int(input())
for i in range(height):
if i in[0]:
print("* "*(width))
elif i in[(height-1)]:
print("* "*(width))
else:
print("*"+" "*(width-2)+" *")
input()
Output: Image link
Hope this helps everyone else, who wants to leave spaces between asteriks, while printing a rectangle and if there are any mistakes in my code let me know, because I'm a beginner myself.
size = int(input('Enter the size of square you want to print = '))
for i in range(size): # This defines the rows
for j in range(size): # This defines the columns
print('*' , end=' ') # Printing * and " end=' ' " is giving space after every * preventing from changing line
print() # Giving a command to change row after every column in a row is done
print() # Leaving one line
for k in range(size): # This defines the rows
print('* ' *size) # Defines how many times * should be multiplied
Here is my python 3.6 code for drawing a square using column size and row size inputs:
column = int(input("Enter column size : "))
row = int(input("Enter row size : "))
print('* ' * column)
print(('* ' + " " * (column-2)+ '*'+'\n')*(row -2) + ('* ' * column))
You can also draw a triangle using this simple way:
n = int(input("enter num"))
for i in range (n+1):
if 2>=i :
print (i * "*")
elif n == i:
print ("*" * i)
else:
print ("*"+" "*(i-2)+"*")
Nice python library that will do this...
termshape - https://github.com/zvibazak/termshape
from termshape import get_rectangle
print(get_rectangle(10, 5))
* * * * * * * * * *
* *
* *
* *
* * * * * * * * * *
In this code, there are 2 for loops and this loops will create edges. And "else statements" represents if our point is not on the edge code will print empty character(" "). "i == 0" shows upper edge and "i == length - 1" shows bottom edge. "j == 0" will be left edge and I think you can guess what is for "j == length - 1" and "print()".
def emptySquare(length,char):
for i in range(length):
for j in range(length):
if (i == 0 or i == length-1 or j == 0 or j == length-1):
print(char,end=" ")
else:
print(" ",end=" ")
print()