Course Content

Python Exercise 4: Astrologer's Stars

In today’s exercise, what we have to do is, we have to print a pattern similar to that of  a right-angle triangle, such as:

*
**
***
****

You have to follow certain instructions, which are as follows:

  • You have to take an integer type variable, and the input of the variable will define the length of the triangle.
  • You have to declare another Boolean variable.
  • When the value of Boolean is 1 i.e. True, the pattern will be printed as shown above.
  • But if the value of Boolean is 0 or false, then the triangle will be printed upside down.

If you are following my playlist and watching the Python tutorials in series, then it is a must for you to solve this problem and share your answer by commenting.

This will enhance and increase your skills, along with boosting my morale.

Your participation will be very much appreciated. Keep supporting and stay up to date with codewithharry.

Code file as described in the video

# Exercise 4
# Pattern Printing
# Input = Integer n
# Boolean = True or False
#
# True n=5
# *
# **
# ***
# ****
#
# False n=5
# ****
# ***
# **
# *
  

Comments(10)

charzo 1 year ago
print("Please enter the number of row(s) you want to print ")
given_row=int(input())
print("Enter 'Yes' for Top to Bottom and 'No' for Bottom to top style")
print_style=input().lower() #If we typecast any string to Boolean , it will be always "True", so typecasting an input() to boolean is difficult I found.
if print_style=='yes' :
    print_style=1
elif print_style=='no':
    print_style = 0
else:
    print("Invalid Input")

def top_to_bottom(row_num):
    row_flag=1

    while(row_flag<=row_num):
        print_flag = 1
        while(print_flag<=row_flag):
            print(" * ",end="")
            print_flag=print_flag+1
        row_flag=row_flag+1
        print("\n")


def bottom_to_top(row_num):
    row_flag=1


    while(row_flag<=row_num):
        print_flag = 5
        while(print_flag>=row_flag):
            print(" * ",end="")
            print_flag=print_flag-1
        row_flag=row_flag+1
        print("\n")


if(print_style):
    top_to_bottom(given_row)
else:
    bottom_to_top(given_row)


charzo 1 year ago
Ignore it 
charzo 1 year ago
I am trying to paste the code here , but it is getting messed up!!
lonewolf 2 months ago
same here bro 500 is word limit
Manav_Seng 9 months, 1 week ago
Name: Manav Sengupta

Solution:

#Code with harry Exercise 4-- Astrologer's stars 

try:
    while(True):
        N = int(input("Enter the no. of rows of stars you want"))
        bools = bool(int(input("Enter 1 or 0 for (Upright) or (Inverted) respectively")))
        if(bools):
            i=1
            r=1
            while (i<=N):
                print(i * "*")
                i = i + r
            temp = int(input("Enter 1 to Continue else enter anything else"))
            if (temp == 1):
                continue
            else:
                break
        else:
            i=N
            r=-1
            while (i>=1):
                print(i * "*")
                i = i + r
            temp = int(input("Enter 1 to Continue else enter anything else"))
            if (temp == 1):
                continue
            else:
                break
except:
    print("Sorry Unexpected Error!")

print(147*"*"+"\n*"+145*" "+"*\n*"+9*" "+"Thanks for checking out my code, and a ton of thanks to CodewithHarry Channel for all the Quality Content for Free"+22*" "+"*\n*"+145*" "+"*\n"+147*"*") # Gratitude
Manav_Seng 9 months, 1 week ago
Name: Manav Sengupta

Solution:

#Code with harry Exercise 4-- Astrologer's stars 

try:
    while(True):
        N = int(input("Enter the no. of rows of stars you want"))
        bools = bool(int(input("Enter 1 or 0 for (Upright) or (Inverted) respectively")))
        if(bools):
            i=1
            r=1
            while (i<=N):
                print(i * "*")
                i = i + r
            temp = int(input("Enter 1 to Continue else enter anything else"))
            if (temp == 1):
                continue
            else:
                break
        else:
            i=N
            r=-1
            while (i>=1):
                print(i * "*")
                i = i + r
            temp = int(input("Enter 1 to Continue else enter anything else"))
            if (temp == 1):
                continue
            else:
                break
except:
    print("Sorry Unexpected Error!")

print(147*"*"+"\n*"+145*" "+"*\n*"+9*" "+"Thanks for checking out my code, and a ton of thanks to CodewithHarry Channel for all the Quality Content for Free"+22*" "+"*\n*"+145*" "+"*\n"+147*"*") # Gratitude
natanian 2 months ago
print("printing starts")
n=int(input("enter the length of the string"))
val=int(input("enter a bool value \n 1 for true and 0 for false"))
if bool(val):
    i=0
    while i<n :
        j=0
        while j<i+1:
            print("*",end="")
            j=j+1
        print("\n",end="")
        i=i+1
else :
    i=n
    while i>0:
        j=i
        while j>0:
            print("*",end="")
            j=j-1
        print("\n",end="")
        i=i-1
lonewolf 2 months ago
#Author : lone_wolf
#last part (coz problem in uploading on site due to word limit or else)
        i-=1
n=input("enter no of row you want in astrologer star>>>")
k=int(input("enter a value '1' for ascending order and '0' for decending order>>"))
def check(k):
    return(bool(k==1))
if(check(k)):
    ascending_str(n)
    print("program end here!!!")
else:
    decending_str(n)
    print("program end here!!!")

       
rahul4837 1 month, 4 weeks ago
num = int(input("Enter the Number to print the Number of Stars : "))
A = bool(int(input("Enter 0 or 1 (0 {For Up side down Triangle}) \n "
                   "(1 {for Right Angle Triangle }) : ")))
if A == True:
    for i in range(1, num + 1):
        print("* " * i)
else:
    for i in range(num):
        i = num - i
        print("* " * i)
pushpak 1 week, 4 days ago
#printing star pattern
i=1
print("Please enter a number")
get = int(input())
print("Enter a number\nfor True :1 \nfor False :0")
Bool = int(input())

if Bool==1:
    while i<=get:
        print("*"*i)
        i = i + 1
        continue
elif Bool==0:
    while i<=get:
        print("*"*get)
        get=get - 1
        continue
else:
    print("sorry given wrong input")

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here