Dry-running nested while loops using a trace table - Answers
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==len(monsters)-1 | output |
2 | 0 | ||||||
3 | '' | ||||||
4 | True | ||||||
5 | 0 | ||||||
6 | 0 < 0 = False | ||||||
11 | Ghost | ||||||
12 | Ghost. | ||||||
13 | 0 == 2 is False | ||||||
15 | 1 | ||||||
6 | 0 < 1 = True | ||||||
7 | True | ||||||
8 | 'Ghost. Ghost' | ||||||
9 | 'Ghost. Ghost and' | ||||||
10 | 1 | ||||||
6 | 1 < 1 = False | ||||||
11 | Ghost. Ghost and Warlock | ||||||
12 | Ghost. Ghost and Warlock. |
||||||
13 | 1 == 2 is False | ||||||
15 | 2 | ||||||
4 | True | ||||||
5 | 0 | ||||||
6 | 0 < 2 = True | ||||||
7 | True | ||||||
8 | Ghost. Ghost and Warlock. Ghost and | ||||||
9 | |||||||
10 | 1 | ||||||
6 | 1 < 2 = True | ||||||
7 | True | ||||||
8 | Ghost. Ghost and Warlock. Ghost and Warlock | ||||||
9 | Ghost. Ghost and Warlock. Ghost and Warlock and | ||||||
10 | 2 | ||||||
6 | 2 < 2 = False | ||||||
11 | Ghost. Ghost and Warlock. Ghost and Warlock and Dragon | ||||||
12 | Ghost. Ghost and Warlock. Ghost and Warlock and Dragon. | ||||||
13 | 2 == 2 is True | ||||||
14 | Ghost etc We will fight them and win ... | ||||||
15 | 3 | ||||||
4 | False | ||||||
16 | Ghost etc We will fight them ... |