Asterisk pyramids or triangles Jun 7 in Python 267 views

4 answers to this question.

+1 vote

If you want to use the same script to print different patterns when running at different times, what you can do is write functions to print different patterns and then randomly call these functions. Refer to the example below:

import random

def pattern1():
n = 5
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")

def pattern2():
n = 3
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")
def pattern3():
n = 7
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")

which=random.randint(1,3)

if which==1:
pattern1()
elif which==2:
pattern2()
else:
pattern3() answered Jun 11 by Likhith

selected Jun 11 by Omkar
+1 vote

Hi, you can use following code snippet to print '*' asterisk triangle pattern in python:

# number of spaces k

# number of rows n = 5

k = 2*n - 2

# outer loop to handle number of rows

for i in range(0, n):

# inner loop to handle number spaces

# values changing acc. to requirement

for j in range(0, k):

print(end=" ")

# decrementing k after each loop

k = k - 1

# inner loop to handle number of columns

# values changing acc. to outer loop

for j in range(0, i+1):

# printing stars

print("* ", end="")

# ending line after each row

print("\r") answered Jun 8 by
• 24,440 points
+1 vote

use nested if in response to a prompt eg

a=print("Enter 1 for pyramid or 2 for triangle")
if(a=1)
{
#enter commands for pyramid triangle
}
elif(a=2)
{
#enter commands for right angle triangle
} answered Jun 10 by brianni

Hi Brianni, you have a nice approach to print different patterns based on user input, but to store the user-input, you can't use print statement. It will simply print the statement for you, but wont store the keyboard input from user. To store keyboard input into a variable, either use raw_input() or input() methods. For eg.

a = input("What is your age? ")
Or
a = raw_input("What is your name?")
+1 vote

Here is a program to print an '*' pyramid

def pyfunc(r):
for x in range(r):
print(' '*(r-x-1)+'*'*(2*x+1))    #single whitespace present in the print statement string
pyfunc(4)

output:

*
***
*****
*******

In case you want to print triangles, to the same program you can just, in place of 1 whitespace character, insert another whitespace character or remove the present one to null string as shown below:

def pyfunc(r):
for x in range(r):
print('  '*(r-x-1)+'*'*(2*x+1))
pyfunc(4)

output:

*
***
*****
*******
def pyfunc(r):
for x in range(r):
print(''*(r-x-1)+'*'*(2*x+1))
pyfunc(4)

output:

*
***
*****
******* answered Jun 10 by
• 1,090 points

How to print first character of each word in upper case of a string in Python

You can use split() using space as ...READ MORE

How to Print a List in Python

ou are using Python 2.x syntax with ...READ MORE

How to write inline if statement for print in Python?

Inline if-else expression must always contain the else ...READ MORE

How to implement multiple try codes in a single block using Python?

Hi all, As per the title, I am ...READ MORE

How can I write code to find a palindrome in python without using string functions?

In Logic 1, try if i<int(length/2): instead of if i<int((length/2+1)): In ...READ MORE

How to write data to a file in Python?

Refer to the below code. data=’whatever your data ...READ MORE

How can you use a single insert statement into three tables from three different classes in python

you can use executescript() method : This is ...READ MORE

Is it possible to print a string and a variable in a single print statement in Python?

This can be done using simple string ...READ MORE