CSC111 Nested-Loop Exercise Solutions

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 17:56, 17 October 2011 (EDT)


# the following loop is endless
#
#cards = [ "As", "King", "Queen", "Jack" ]
#for card in cards:
#     print( "Card = ", card )
#     cards.append( card )


# print a square of 4 lines of 5 stars
for i in range( 4 ): # 4 lines
    for j in range( 5 ): # 5 stars per line
        print( '*', sep='', end='' )
    print()

print( "\n\n" )

# print a triangle of 5 lines of stars
# the first line contains 1 star
for i in range( 1, 6 ): # 5 lines
    for j in range( i ): # stars
        print( '*', sep='', end='' )
    print()

print( "\n\n" )

# print a triangle of 5 lines of stars
# the first line contains 5 stars
for i in range( 5, 0, -1 ):
    for j in range( i ):
        print( '*', sep='', end='' )
    print()

print( "\n\n" )

# print a rectangle of numbers

for i in range( 3 ): # 3 lines
    for j in range( 0, 6 ):
        print( j, end= ' ' )
    print()

print( "\n\n" )

# print another rectangle of numbers:
for i in range( 3 ): # 3 lines
    for j in range( i, 6+i ):
        print( j, end= ' ' )
    print()

print( "\n\n" )

# print all the characters in the alphabet
for c in "abcdefghijklmnopqrstuvwxyz":
    print( c )


print( "\n\n" )

# print all the accounts in the class
for c in "abcdefghijklmnopqrstuvwxyz":
    print( "111a-a" + c )

print( "\n\n" )

# print all the CSC accounts
for course in ["111a", "212a", "231a", "240a" ]:
    for c in "abcdefghijklmnopqrstuvwxyz":
        print( course + "-a" + c )
    print()