Design a Python script that uses a for loop to print a pattern of numbers or characters. For instance, create ascript that prints a pyramid pattern using asterisks (*) based on a user-input height. (15 Marks)
Question
Design a Python script that uses a for loop to print a pattern of numbers or characters. For instance, create ascript that prints a pyramid pattern using asterisks (*) based on a user-input height. (15 Marks)
Solution
Sure, here is a simple Python script that prints a pyramid pattern using asterisks (*) based on a user-input height.
# Ask for user input
height = int(input("Enter the height of the pyramid: "))
# Loop through each row
for i in range(height):
# Print leading spaces
for j in range(height - i - 1):
print(" ", end="")
# Print asterisks
for k in range(2 * i + 1):
print("*", end="")
# Move to the next line
print()
Here's how it works:
- The script first asks the user to input the height of the pyramid.
- It then enters a loop that runs for each row of the pyramid.
- In each row, it first prints the leading spaces. The number of spaces is equal to the height of the pyramid minus the current row number minus one.
- After printing the spaces, it prints the asterisks. The number of asterisks is equal to twice the current row number plus one.
- Finally, it moves to the next line and repeats the process for the next row.
Similar Questions
Write a C program to print the Pyramid with numbersWrite a program to print a pyramid of numbers separated by spaces for the given number of rows.At the time of execution, the program should print the message on the console as:Enter number of rows : For example, if the user gives the input as:Enter number of rows : 5then the program should print the result as:Enter number of rows : 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Ethan is organizing a creative coding challenge for his friends. The challenge is to create a unique pattern using asterisks (*) based on a given input size n. Write a program to help Ethan by taking an integer n as input and generating the pattern given below.Input: 5Output:Input format :The input consists of an integer n.Output format :The output prints the required pattern.Code constraints :1 ≤ n ≤ 15Sample test cases :Input 1 :5Output 1 :* * * * * * * * * * * * * * * * * Input 2 :10Output 2 :* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Write a program to generate and print the given star pattern.Input:The input consists of a single integer N, where 1 ≤ N ≤ 10.Output:Print the star pattern as described.For N=3, the pattern will be * *** ***** *** *
Develop a C program to print the following pattern of N rows using whileloop (Read N value from the Keyboard)For Example: N value : 512 34 5 67 8 9 1011 12 13 14
Write a program to print the following output: [3] 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.