Write A Python Script To Generate The Following Pattern Up To N Lines

Let's take a number from the user as an input to the Python program and print the patterns.  the patterns  is  is half pyramid pattern

This python script contains only file lines of code

 

Write A Python Script To Generate The Following Pattern Up To N Lines

 


Write A Python Script To Generate The Following Pattern Up To N Lines

Take a variable named as number and using the input function call ask a user to enter a number and then convert it into an integer using the type conversion as shown below.

number = int(input("Enter a Number : "))




Take a for loop and iterate from 1 to the number and get the index value as x, take another look and iterate  the 1 to check and get the index value as y. in the inner for loop print the pattern that you are required (*  pattern).  in the outer for loop print new line.

for x in range(1, number+1):
   for y in range(1, x+1):
       print("*", end =" ")
   print("")



Complete python code
==========================

number = int(input("Enter a Number : "))
for x in range(1, number+1):
   for y in range(1, x+1):
       print("*", end =" ")
   print("")






Output
=================
Enter a Number : 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *




Let's use the same logic  to generate the pyramid pattern of numbers. Instead of printing the star you have to print the value of the index , that is the value of Y in the inner  for loop.

number = int(input("Enter a Number : "))
for x in range(1, number+1):
   for y in range(1, x+1):
       print(y, end =" ")
   print("")






Output
==========================
Enter a Number : 6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6





Conclusion
======================
Execute the above Python program by providing a random integer value as an input.

Comment down below if you have any suggestions to improve the above Python program





Post a Comment

0 Comments