2 Replies - 7703 Views - Last Post: 05 October 2010 - 08:47 AM Rate Topic: -----

#1 reggraw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Please help!

Is This A Good Question/Topic? 0
  • +

Replies To: creating patterns using nested FOR loops

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • 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


Was This Post Helpful? 0
  • +
  • -

#3 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1