Back

Dry-running nested while loops using a trace table

Dry-run this code using the table provided. When you have worked through the code, run the code in Python and compare the output you get to what you predicted on paper. Remember, only change a value if it changes, and move on to the next line after each change. The table has been started for you. (Note: len(monsters) = 3)

1    monsters=["Ghost","Warlock","Dragon"]
2    tries=0
3    manuscript=""
4    while tries < len(monsters) :
5         count=0
6         while count<tries :
7              print(count<tries)
8              manuscript += monsters[count]
9              manuscript += " and "
10            count+=1
11       manuscript += monsters[tries]
12       manuscript += ". "
13       if tries == len(monsters) - 1 :
14            manuscript += " We will fight them and win ..."
15       tries +=1
16   print (manuscript)

line tries manuscript tries<3 count count<tries tries==3-1 output
2 0            
3   ''          
4     0 < 2 = True        
5       0      
6         0 < 0 = False    
11   Ghost          
12   Ghost.          
13           0 == 2  is False  
15 1            
6              
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               

 Back