Difference between revisions of "CSC111 Nested-Loop Exercises"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- ===Single Loop=== * What is the behavior of this loop? cards = [ "As", "King", "Queen", "Jack" ] for card in cards: print( "Card = ", card ) cards.appe...")
 
(Single Loop)
 
Line 8: Line 8:
 
       print( "Card = ", card )
 
       print( "Card = ", card )
 
       cards.append( card )
 
       cards.append( card )
 +
 +
<center><videoflash>BWMM9Dz_rFs</videoflash><br />(Go to 7-minute point in video...)</center>
  
 
===Nested-Loops===
 
===Nested-Loops===

Latest revision as of 07:40, 29 September 2011

--D. Thiebaut 08:26, 29 September 2011 (EDT)


Single Loop

  • What is the behavior of this loop?
cards = [ "As", "King", "Queen", "Jack" ]
for card in cards:
     print( "Card = ", card )
     cards.append( card )

(Go to 7-minute point in video...)

Nested-Loops

  • Create nested loops that will print the following patterns, without using the '*' * n construct.


*****
*****
*****
*****
  • triangle
*
**
***
****
*****
  • another triangle
*****
****
***
**
*
  • A rectangle of numbers
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5

  • Another square of numbers
0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
  • All the characters in the alphabet:
a
b
c
d
.
.
.
y
z
  • Some of the account numbers for the class:
111a-aa
111a-ab
111a-ac
...
111a-az
  • All the account numbers for the class
111a-aa
111a-ab
111a-ac
...
111a-az
111a-ba
111a-bb
111a-bc
...
111a-bz
  • All the account numbers for the CS Department, with classes 111, 231, 270, and 240.








.