How to print diamond using for loops with '*'??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to print diamond using for loops with '*'???

i.e. * * * * * * * * *

27th Dec 2016, 6:59 PM
Himanshu Vishwakarma
6 Answers
+ 3
n = int(input()) for i in range(1,n+1): print((n - i)*' ' + ((2*i) - 1)*'*') for i in range(2,n+1): print((i-1)*' ' +((n + n)-((2*i)-1))*'*')
28th Dec 2016, 11:52 PM
Zahra Zaker Dehnavi
Zahra Zaker Dehnavi - avatar
+ 2
You have to figure out at what intervals you want them to be printed. As a result, you should see or find some sort of pattern and make conditions out of it. An example: if index % 3 == 0 print("*"). This code says every three print that out. Of course it'll be different for each row. You will need 2 for loops. One nested in the other because you need to create a grid and think of it as printing it in coordinates. I will stop here becuase I can go on and on and you might have questions already. So read it through, think it out a bit before you ask any questions. You'll be surprised with what you can figure out on your own with some thinking!
28th Dec 2016, 12:38 AM
Hyrum
Hyrum - avatar
+ 1
thnx to u too zahra
29th Dec 2016, 5:38 PM
Himanshu Vishwakarma
0
i tried a lot but i was unable to figure it out but still thanks
28th Dec 2016, 1:25 PM
Himanshu Vishwakarma
0
i figured it out...thanks a lot !!!!!
28th Dec 2016, 2:26 PM
Himanshu Vishwakarma
0
Glad to hear!
28th Dec 2016, 8:51 PM
Hyrum
Hyrum - avatar