CS 115 Lab 4, Setup and practice with if, else, and elif

[Back to Lab 4] [Back to CS 115 schedule]


Setup

  1. If the machine was on when you started using it, reboot it to MacOS.
  2. Follow these procedures to mount your blue home-directory on the local machine and create a PyCharm project (call it Lab04). You will use PyCharm in the next part, but, it is important that you set up your project now so that if you run into any issue, we can resolve them quickly.

Practice

As you work through this section, be sure that you really understand each of the programs, and call for help if you don't!

  1. In a new browser tab, visit the online Python 3 tutor and enter the following Python code:
    def main():
        x = 7
        if x < 0:
            print('negative')
        if x > 0:
            print('positive')
    
    
    main()
    
    Then click "Visualize execution."
  2. Using the visualizer to help you if necessary, answer Questions 1 and 2 in your writeup. Try to answer the question without using the visualizer at first, and then use it if you get stuck.
  3. Modify the if-statements as follows (changes are bolded and italicized):
    if x < 0:
        print('negative')
    else:
        print('non-negative')
    Then click "Visualize execution."
  4. Using the visualizer to help you if necessary, answer Questions 3 and 4 in your writeup. Try to answer the question without using the visualizer at first, and then use it if you get stuck.
  5. Next, try the following code:
    def main():
        num = 5  # change this value
    
        if num == 0:
            num += 2
            print('Your value is now ', num, '.', sep="")
        else:
            num += 4
            print('Your value is now ', num, '.', sep="")
        if num > 0:
            num -= 4
            print('Your value is now ', num, '.', sep="")
    
    
    main()
    
  6. Answer Question 5 in your writeup without running the program. Then run the program to check your answers.
  7. Next, try the following revisions (changes are bolded and italicized):
    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="")
    
  8. Answer Question 6 in your writeup.
  9. Next, try the following revision (changes are bolded and italicized):
    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="")
    
  10. Answer Question 7 in your writeup.
  11. Finally, link together your if-statements as follows (changes are bolded and italicized):
    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="")
    
  12. Answer Question 8 in your writeup.
  13. Practice with Nested Loops. 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 9-12 in your writeup. Then use the tutor to step through this loop and check your work.
  14. Begin Part A.