1 Replies - 339 Views - Last Post: 24 November 2011 - 03:10 AM Rate Topic: -----

#1 collegecsstudent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-November 11

For Loop in Python: Creating Symmetric Images

Posted 13 November 2011 - 03:53 PM

I need help learning how to use a condition controlled or nested for loop to print the following outputin Python:

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

I know I need to use for loops so I was thinking:

for i in range (0,1):
    print ('*' * 36)
for i in range (2,4):
    print ('***   ' *6)
for i in range (4,6):
    print ('   ***' *6)
for i in range (6,7):
    print ('*' * 36)



This generates the required output but is there a simpler way to do this with less code/fewer for statements? Help is appreciated thank you.

Required Output came out incorrectly. This is what it should be:

'*' * 36
'*** ' * 6
'*** ' * 6
' ***' * 6
' ***' * 6
'*' * 36

MOD EDIT: Fixed code tags. The proper way to use code tags:

:code:

This post has been edited by JackOfAllTrades: 13 November 2011 - 04:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: For Loop in Python: Creating Symmetric Images

#2 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: For Loop in Python: Creating Symmetric Images

Posted 24 November 2011 - 03:10 AM

You can use a nested for loop to print out the inner 4 lines. This function would do it:

def func():
	print "* "*36
	for i in range(4):
		s = ""
		for j in range(6):
			s += "*** "
		print s
	print "* "*36



Just call func().
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1