Compsci 6, Fall 2011, Classwork 6

PRINT Names and NetID of students in group (min 2, max 3)
Name: __________________________ NetID: _____________   Name: __________________________   NetID: _____________

Name: __________________________ NetID: _____________

Problem:

  1. What is the value of the string variable st when the code below executes? Explain (to yourself or the class, you don't have to write it down) your answer.
      word = "creation"
      st = ""
      for i in range(0,len(word)):
          if i % 2 == 0:
              st = st + word[i]
    
      print st   # value here?
    
    
  2. The code below does the same thing as the code above, what do you think the third parameter to range does?
      word = "creation"
      st = ""
      for i in range(0,len(word),2):
          st = st + word[i]
    
      print st   # value here?
    
    
  3. The code below prints each of the numbers from 1 to 9 on a separate line. How would you modify the code to print the numbers from 12 to 36, each on a separate line?
    
       for num in range(1,10):
           print num
    
    
    
  4. The sum of the numbers from 1 to 100, e.g., 1 + 2 + 3 + ... + 100 is 5050. Explain why the code below prints 4950 and explain how to change one digit in the code -- that is change a '6' to a '5' or a '0' to a '1', so that the code prints 5050.
    
       total = 0
       for num in range(1,100):
           total = total + num
       print total