creating patterns using nested FOR loops

Page 1 of 1

2 Replies - 10938 Views - Last Post: 05 October 2010 - 08:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=193598&amp;s=1126a29356659b62ed723d2843602bbc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 reggraw

Reputation: 0
• Posts: 1
• Joined: 19-June 09

creating patterns using nested FOR loops

Posted 05 October 2010 - 08:31 AM

I am trying to figure out the logic for creating a pattern of circles using a number entered by a user and can not get my code to print the circles next to each other during the inner loop!! Please help!

This is my code so far:

userAmt = 0
userAmt = int(raw_input('What is your number: '))
for circle in range(1, userAmt, 1):
for circle2 in range(1, userAmt, 2):
circleList = list()
circleList.append(circle)
print'o'

But my output should look like this

o
oo
ooo
oooo

oooo
ooo
oo
o

Is This A Good Question/Topic? 0

Replies To: creating patterns using nested FOR loops

#2 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,888
• Joined: 16-October 07

Re: creating patterns using nested FOR loops

Posted 05 October 2010 - 08:41 AM

You only need one loop. You want to count up to a number and then count down from it. You may do this with two separate loops, if that's easier.

You hardly need a nested loop. You can simply do this:
```>>> print 'o' * 5
ooooo

```

#3 Brewer

• Awesome

Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

Re: creating patterns using nested FOR loops

Posted 05 October 2010 - 08:47 AM

To keep with the for loop I would probably do this:

```for i in range(1,n+1): # Where n is the user's input
```

I think this would work perfectly well. However, I agree with baagvai, you only need one loop. If you are doing this to get a feel for nested loops, there are better examples. I currently have a tutorial pending acceptance on Nested Lists in Python.

This post has been edited by Jambr: 05 October 2010 - 08:48 AM