How to write a single program in python to print different triangles

0 votes
Asterisk pyramids or triangles
Jun 7, 2019 in Python by anonymous
9,246 views

4 answers to this question.

+1 vote
Best answer

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, 2019 by Likhith

selected Jun 11, 2019 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 9, 2019 by Abha
• 28,140 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, 2019 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 11, 2019 by Nisa
• 1,090 points

Related Questions In Python

+1 vote
2 answers

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

class Solution:     def firstAlphabet(self, s):             self.s=s              k=''              k=k+s[0]              for i in range(len(s)):                     if ...READ MORE

answered Oct 28, 2020 in Python by Anurag
12,388 views
0 votes
1 answer

How to Print a List in Python

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

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
1,067 views
0 votes
1 answer

How to write inline if statement for print in Python?

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

answered Dec 4, 2018 in Python by Nymeria
• 3,560 points

edited Dec 6, 2018 by Nymeria 16,750 views
0 votes
0 answers

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

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

Jan 14, 2019 in Python by Anirudh
• 2,090 points
668 views
0 votes
1 answer

How to write data to a file in Python?

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

answered May 13, 2019 in Python by Shaam
975 views
0 votes
1 answer
0 votes
1 answer

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

answered Jul 15, 2019 in Python by Neel
• 3,020 points
829 views
0 votes
2 answers

How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
23,843 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP