In: Computer Science
Trace Each of the following programs on the paper provided.
Program 1
User enters “cat”, “dog”, “chair”, “quit”
Line Number |
||||
it doesn't have to be this specific table...
Solution:
------------------------
Line 1:
Declaring variable full and assign empty string ("") to it.
Line 2:
Declaring variable count and assign 0 to it.
Line 3:
Asking user to enter word. convert that input word into string using str() function. and assign that word to variable si.
Assume that user enter cat. thus si = cat.
Line 4:
This loop will run untill user enter quit.
Here si !=quit condition true so loop body will execute.
Line 5 - Concatenate si to full . thus full will now contain cat
Line 6- Adding 1 to count . count become 1.
Line 7- Asking user to enter word. convert that input word into string using str() function. and assign that word to variable si.
Assume that user enter dog
Go back to Line 4:-
Here si !=quit condition true so loop body will execute.
Line 5 - Concatenate si to full . thus full will now contain catdog
Line 6- Adding 1 to count . count become 2.
Line 7- Asking user to enter word. convert that input word into string using str() function. and assign that word to variable si.
Assume that user enter chair
Go back to Line 4:-
Here si !=quit condition true so loop body will execute.
Line 5 - Concatenate si to full . thus full will now contain catdogchair
Line 6- Adding 1 to count . count become 3.
Line 7- Asking user to enter word. convert that input word into string using str() function. and assign that word to variable si.
assume that user enter quit
Go back to Line 4:-
Here si !=quit condition fails. thus loop body will not execute.
Line 8:
convert count to string by using str(count) . concatenate it with " words" and print it.
So it will print 3 words
Line 9:
convert full to string by using str(full) concatenate it with Full and print result.
So it will print Full: catdogchair