Day 8 - pre-class assignment

Goals for today's pre-class assignment

  • Use complex if statements and loops to make decisions in a computer program

Assignment instructions

Watch the videos below, read through Sections 4.1, 4.4, and 4.5 of the Python Tutorial, and complete the programming problems assigned below.

This assignment is due by 11:59 p.m. the day before class, and should be uploaded into the "Pre-class assignments" dropbox folder for Day 8. Submission instructions can be found at the end of the notebook.


In [ ]:
# Imports the functionality that we need to display YouTube videos in a Jupyter Notebook.  
# You need to run this cell before you run ANY of the YouTube videos.

from IPython.display import YouTubeVideo

In [ ]:
# WATCH THE VIDEO IN FULL-SCREEN MODE

YouTubeVideo("8_wSb927nH0",width=640,height=360)  # Complex 'if' statements

Question 1: In the cell below, use numpy's 'arange' method to create an array filled with all of the integers between 1 and 10 (inclusive). Loop through the array, and use if/elif/else to:

  1. Print out if the number is even or odd.
  2. Print out if the number is divisible by 3.
  3. Print out if the number is divisible by 5.
  4. If the number is not divisible by either 3 or 5, print out "wow, that's disappointing."

Note 1: You may need more than one if/elif/else statement to do this!

Note 2: If you have a numpy array named my_numpy_array, you don't necessarily have to use the numpy nditer method. You can loop using the standard python syntax as well. In other words:

for val in my_numpy_array:
    print(val)

will work just fine.


In [ ]:
# put your code here.

In [ ]:
# WATCH THE VIDEO IN FULL-SCREEN MODE

YouTubeVideo("MzZCeHB0CbE",width=640,height=360)  # Complex 'if' statements

Question 2: In the space below, loop through the given array, breaking when you get to the first negative number. Print out the value you're examining after you check for negative numbers. Create a variable and set it to zero before the loop, and add each number in the list to it after the check for negative numbers. What is that variable equal to after the loop?


In [ ]:
# put your code here.

my_list = [1,3,17,23,9,-4,2,2,11,4,-7]

Question 3: In the space below, loop through the array given above, skipping every even number with the continue statement. Print out the value you're examining after you check for even numbers. Create a variable and set it to zero before the loop, and add each number in the list to it after the check for even numbers. What is that variable equal to after the loop?


In [ ]:
# put your code here

Question 4: Copy and paste your code from question #2 above and change it in two ways:

  1. Modify the numbers in the array so the if/break statement is never called.
  2. There is an else clause after the end of the loop (not the end of the if statement!) that prints out "yay, success!" if the loop completes successfully, but not if it breaks.

Verify that if you use the original array, the print statement in the else clause doesn't work!


In [ ]:
# put your code here!

Assignment wrapup

Question 5: What questions do you have, if any, about any of the topics discussed in this assignment after watching the videos, reading the link to the Python tutorial, and trying to write the programs?

Put your answer here!

Question 6: Do you have any further questions or comments about this material, or anything else that's going on in class?

Put your answer here!

Question 7: How long did this assignment take you to complete?

Put your answer here!

Congratulations, you're done!

Submit this assignment by uploading it to the course Desire2Learn web page. Go to the "Pre-class assignments" folder, find the dropbox link for Day 8, and upload it there.

See you in class!