2 Replies - 423 Views - Last Post: 18 February 2014 - 08:17 AM Rate Topic: -----

#1 Karia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-February 14

Triangle

Posted 18 February 2014 - 05:12 AM

Help me to make a program to make a triangle like given below: with for loop...

*
**
***
****
*****
****
***
**
*


I tried like this...



for i in range(1,6):
    for j in range(1,i):
        print "*",
    print


the out put is
*
* *
* * *
* * * *




please help me....thanks in advance...

This post has been edited by Atli: 18 February 2014 - 05:34 AM
Reason for edit:: Use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: Triangle

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Triangle

Posted 18 February 2014 - 05:59 AM

You skipped the most important step of the programming process: understand the logic yourself! Before you even touch a keyboard, you need to first understand how to solve the problem; what logic is required to accomplish the task. - Have you figured out how to do that yet?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,705
  • Joined: 16-October 07

Re: Triangle

Posted 18 February 2014 - 08:17 AM

Your problem is really just a row for number problem. That is, for a given row, what is the number of stars.

What you have may be rewritten as:
for i in range(1,6):
    print i, '*' * i



Results:
1 *
2 **
3 ***
4 ****
5 *****



However, the 1 to 5 thing is unnecessary. You could instead figure out the value you want for a given row. e.g.
for i in range(5):
    ct = i + 1
    print i, ct, '*' * ct



Results:
0 1 *
1 2 **
2 3 ***
3 4 ****
4 5 *****



Thinking of it like this, your solution becomes:
PEEK=5
SIZE=PEEK + PEEK - 1

for i in range(SIZE):
    ct = # your code here
    print i, ct, '*' * ct



Results:
0 1 *
1 2 **
2 3 ***
3 4 ****
4 5 *****
5 4 ****
6 3 ***
7 2 **
8 1 *



Good luck.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1