CS 115 Pre-Lab 4 Instructions

Deadline: Tu 2/10/2015 at 7 AM

Refer to the Week 4 reading as necessary to complete this assignment.


Part 1: Nested loops

  1. Enter the following code into the Online Python 3 Tutor:
    line = 1
    for i in range(1, 4):
        for j in range(1, 5):
            print('Line ', line, ': (', i, ', ', j, ')', sep="")
            line += 1
    
    Try to predict what this loop will do. Answer Questions 1-4 in your pre-lab. Then use the tutor to step through this loop and check your work.
  2. Modify the code as follows:
    line = 1
    for i in range(1, 6):
        for j in range(1, 3):
            print('Line ', line, ': (', i, ', ', j, ')', sep="")
            line += 1
    
    Again, try to predict what this loop will do. Answer Questions 5-7 in your pre-lab. Then use the tutor to step through this loop and check your work.

Part 2: If-statements

  1. Answer Question 8 in your pre-lab, which is based on the following code. Use the tutor to help if necessary.
    # For Question 8
    def main():
        num = 5  # change this value
    
        if num == 0:
            num += 2  # identical to num = num + 2
            print('Your value is now ', num, '.', sep="")
        else:
            num += 4  # identical to num = num + 4
            print('Your value is now ', num, '.', sep="")
        if num > 0:
            num -= 4  # identical to num = num - 4
            print('Your value is now ', num, '.', sep="")
    
    
    main()
    
  2. Next, try the following revisions (changes are bolded and italicized):
    # For Question 9
    if num == 0:
        num += 2
        print('Your value is now ', num, '.', sep="")
    if num < 0:
        num += 4
        print('Your value is now ', num, '.', sep="")
    else:
        num -= 4
        print('Your value is now ', num, '.', sep="")
    
    Answer Question 9 in your pre-lab.
  3. Next, try the following revision (changes are bolded and italicized):
    # For Question 10
    if num == 0:
        num += 2
        print('Your value is now ', num, '.', sep="")
    elif num < 0:
        num += 4
        print('Your value is now ', num, '.', sep="")
    if num > 0:
        num -= 4
        print('Your value is now ', num, '.', sep="")
    
    Answer Question 10 in your pre-lab.
  4. Finally, link together your if-statements as follows (changes are bolded and italicized):
    # For Question 11
    if num == 0:
        num += 2
        print('Your value is now ', num, '.', sep="")
    elif num < 0:
        num += 4
        print('Your value is now ', num, '.', sep="")
    else:
        num -= 4
        print('Your value is now ', num, '.', sep="")
    
    Answer Question 11 in your pre-lab.

Part 3: Submit the pre-lab (due Tu at 7 AM)

Review your answers, and then click the Next button at the bottom of the Moodle quiz. Once you do that, you should see something similar to this:
Quiz attempt summary

Click the Submit all and finish button. You MUST do this so that your pre-lab can be graded! Once you have submitted your quiz, you should see something similar to this at the top of your Moodle window. The important part is that the State shows up as Finished.
Quiz confirmation