Back

Sanskar

Keen Learner and Exp... • 1d

Day 4 of learning python as a beginner. Topic: printing * pattern using while and for loop. I took the famous challenge of printing a * (star) pattern my goal was to print a triangle and diamond shape pattern. first I used int(input()) function to take input in an integer from the user then I used a for loop to create a loop which will print the pattern. As for loop excludes the last number therefor to avoid that I added row+1 this means that "add +1 to the user input" now for loop will include row (user's input). Then I have to add spaces from the margin in order to get a visually centered pattern (not the one which sticks to the left margin) and thus I used print(" " * (row-i), end ="") as I discovered, less stars = more spaces from the margin (typically in decreasing order like 4, 3, 2, 1) and thus row-i makes sure that the space is printed in decreasing order (ex- input 5 rows now row-i = 5-1=4 spaces printed (as loop stars from 1) ). end="" ensures that there is no new line entered by default. I used, print("*" * (2*i-1)) to print stars in odd numbers (1, 3, 5, etc). in line 25 I used, for i in range(row-1, 0, -1) here row-1 makes sure that the loop stars in descending order which will help in printing less spaces in first row and more spaces in last row (for diamond pattern). I didn't started loop with "row" only because I don't want to repeat the last line of triangle pattern (which is the middle line of diamond pattern) and -1 in the last emphasis that the printing starts backward (more stars first less stars in the end). I know I may have confused you a lot especially with my explaination fell free to ask any questions and suggest any alternative method so that I can improve the code. Also here's my code.

Reply
1

More like this

Recommendations from Medial

Arihant Jain

Crafting Impactful A... • 12d

🧠 Programming Challenge of the Day Can you solve this? You're given an integer array (with both positive and negative values). Each element in the array represents the number of stars (*) to be printed vertically in its column: 🔹 Positive values

See More
Reply
2
3

Sanskar

Keen Learner and Exp... • 2d

Day 3 of learning python as a beginner. Topic: Loops Loops are of two types: for loop and while loop. For loop is used when: 1. you know how many times you have to repeat, in my case I know I have to repeat it 10 times to get a full multiplication

See More
Reply
2
Image Description
Image Description

Aakash Dhawan

Hey I am on Medial • 7m

I have an Idea fir all of you . there are many type of t shirt printing business . But we want to modify it , I think we have to launch an application which will certain orders and their process My Idea .... In My opinion we want to start

See More
7 Replies
9
Image Description

Moradiya Jenisa

Be confident • 10m

Name : Instant print **InstantPrint** is an on-demand document printing and delivery app designed for students. It allows users to upload documents, select print options (black & white or color, single or double-sided, paper size), and receive printe

See More
1 Reply
1

Aakash Tiwary

Assistant Marketing ... • 4m

Working on project from past 1.5years, Online print biz. print in gifting, print in marketing posm, print for clg & scholl students. i know many companies are involved in this online industury. but i find some gap in quality. one whitespace i had fou

See More
Reply
3

Ashish Ahirwar

Aspiring entrepreneu... • 3m

education mafias are way too dangerous like wtf is going on the private universities and colleges they have created a system loop by which they're just printing money like paper!!!

Reply
10

Abhay Devda

Silverwale • 10m

I was starting a b2b silver jewellery row material and silverbar in all at one place

Reply
7
Image Description
Image Description

Nishanth Kulal

Let's talk about you... • 4m

Is the T-shirt printing business worth investing in?

3 Replies
5
Image Description

Gantavaya Minocha

My vision starts whe... • 3m

Looking for a Garment Printing Partner in Delhi We're seeking a Delhi-based garment printer who offers the following services at minimal pricing: DTF Printing Sublimation Printing Puff Printing Digital Printing If you or someone you know has t

See More
1 Reply
7
Image Description
Image Description

Shafe Shaikh

Hey I am on Medial • 1y

I need manufacturer for my PRINT ON DEMAND business please suggest me a best PRINT ON DEMAND manufacturer in india for T-shirt.

7 Replies
4

Download the medial app to read full posts, comements and news.