How to write a single program in python to print different '*' triangles?

0 votes
Asterisk pyramids or triangles
Jun 7 in Python by anonymous
58 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 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 Abha
• 15,680 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 Nisa
• 1,090 points

Related Questions In Python

0 votes
1 answer

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

answered Jun 1, 2018 in Python by george
• 200 points
1,361 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,710 points
55 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,500 points

edited Dec 6, 2018 by Nymeria 634 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 in Python by Anirudh
• 2,060 points
27 views
0 votes
1 answer
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 12 in Python by Shaam
17 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 1 day ago in Python by Neel
• 1,390 points
10 views
0 votes
1 answer

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

def add(a,b):      return a + b #when i call ...READ MORE

answered May 20 in Python by Mohammad
• 1,500 points
21 views
0 votes
1 answer

How to add a new Python interpreter in PyCharm?

Refer to the below screenshots: Then set a ...READ MORE

answered May 30 in Python by Shabnam
• 760 points
97 views