Chapter 06 Worksheet

     
     For each of the first two questions, write out your best guess as to what the
     code will print. Clearly label this as your guess. Then run the code and look at
     the output. Write if your guess was correct. If it was not, briefly describe what
     was different and why.

     Predicting what the code will do is important in writing programs, and
     figuring out why programs don't run the way expected.
     
  1. What does this program print out?
     (Remember: TWO answers. Your guess and the actual result. Label both.)
     
     x = 0
     while x < 10:
         print(x)
         x = x + 2
     
  2. What does this program print out?
     
     x = 1
     while x < 64:
         print(x)
         x = x * 2
     
  3. Why is the ``and x >= 0'' not needed?
     
     x = 0
     while x < 10 and x >= 0:
         print(x)
         x = x + 2
     
  4. What does this program print out? (0 pts) Explain. (1 pt)
     
     x = 5
     while x >= 0:
         print(x)
         if x == "1":
             print("Blast off!")
         x = x - 1
     
  5. Fix the following code so it doesn't repeat forever, and keeps asking
     the user until he or she enters a number greater than zero: (2 pts)
     
     x = float(input("Enter a number greater than zero: "))

     while x <= 0:
         print("Too small. Enter a number greater than zero: ")
     
  6. Fix the following code:
     
     x = 10

     while x < 0:
         print(x)
         x - 1

     print("Blast-off")
     
  7. What is wrong with this code? It runs but it has unnecessary code.
     Find all the unneeded code. Also, answer why it is not needed. (1 pt)
     
     i = 0
     for i in range(10):
         print(i)
         i += 1
     
  8. Explain why the values printed for x are so different. (2 pts)
     
     # Sample 1
     x = 0
     for i in range(10):
         x += 1
     for j in range(10):
         x += 1
     print(x)

     # Sample 2
     x = 0
     for i in range(10):
         x += 1
         for j in range(10):
             x += 1
     print(x)