Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Homework 1: Introduction to Variables, Lists, and Loops

Contents

Homework 1: Introduction to Variables, Lists, and Loops

CMSE 201 Fall 2022

Version check


Put your name here


Learning Goals


Content Goals


Utilize loops to store, access, and manipulate data in a multiple lists

Write code to execute simple mathematical operations

Utilize variables and lists to store data


Practice Goals

Evaluate different ways to solve problems using code

Diagnose and debug code provided to you


Assignment instructions


Work through the following assignment, making sure to follow all the directions and answer all

the questions.


This assignment is due at 11:59 pm on Sunday, September 18th. It should be uploaded into

the “Homework Assignments” submission folder for Homework #1. Submission instructions can

be found at the end of the notebook.


Before you begin, make sure your Jupyter kernel is Python3. There are significant differences

between Python2 and Python3, and we will use Python3 in this course. The kernel type at the

upper-right corner should say Python3”, and the following code should show a version 3.x.y.


If your kernel is Python2, try selecting Python3 from the Kernel menu above under the submenu

“Change kernel” . If that does not work, consult the Slack help channel for assistance.


import sys

print(sys.version)

Grading


Academic integrity statement (2 Points)


1. Going to Office Hours (8 Points)

2. Looping by Index (10 Points)

3. Looping by Value (10 Points)

4. While loops (10 Points)

5. Debugging with Lists and Loops (10 Points)


Total points possible: 50


Academic integrity statement (2 Points)


In the markdown cell below, paste your personal academic integrity statement. By including this

statement, you are confirming that you are submitting this as your own work and not that of

someone else.


Put your personal academic integrity statement here.


0. Going to Office Hours (8 Points)


Why are we doing this?


We want to make sure that everyone knows how to access the resources available to you. One

of the best resources you have at your disposal is office hours.


What will you do?


(At minimum) Go to one office hour session (it doesnt matter which one you go to). Come with

one question that you would like to talk about. It can be big or small. Ask your question. All of

the instructors for CMSE 201 (section leads, TAs, and LAs) will be adding to a running list of folks

that we see during office hours; as long as your name appears on the list, you’ll get credit for

this part of Homework 1.


NOTE: The day when the homework is due (Friday, September 16) will be the busiest time for

folks to go to office hours. You are STRONGLY encouraged to go to office hours before Friday

to get credit for this part of this assignment. (You should still feel free to go to office hours on

Friday for help, though!)


You can find the office hours calendar on the course website.


1. Looping by Index (10 Points)


It’s important to understand how to use the indices to access elements of a list and how to use    loop variables in for loops. For the following exercises, we’ll be focusing on looping by index.”

Note: Each of these problems provides you with a bit of starter code. You must use the provided

starter code to answer the questions.


Below is a list (animals), which you will use to answer the subsequent questions.


animals = ["Cat", "Dog", "Bird", "Pig", "Cow"]

✅  1.1 Task (2 Points)

Use the following starter code to print out the indices 0 through 4 AND all of the values in the

animals list. You must loop by index. Your final result should look like:


0 Cat

1 Dog

2 Bird

3 Pig

4 Cow


# write your answer here, starting with the given code

for a in range(len(animals)):


✅  1.2 Task (3 Point)

Now that we know which indicies corresponds to each animal, lets try printing out the same list,

but this time switching Dog and Pig in the print statement by changing indices. Use the starter

code below. You must loop by index. Your final result should look like:


0 Cat

1 Pig

2 Bird

3 Dog

4 Cow


# write your answer here, starting with the given code

for b in range(len(animals)):


Input In [7]

for b in range(len(letters)):

^

IndentationError: expected an indented block


✅  1.3 Task (3 Points)

Lets try slicing an array. For this task, we are going to be printing the same output that you did

in question 1. 1, only this time, it will be backwards. You must loop by index. Your final result

should look like this:

4 Cow

3 Pig

2 Bird

1 Dog

0 Cat


# write your answer here, starting with the given code

for c in range(len(animals)):


Input In [36]

for c in range(len(animals)):

^

IndentationError: expected an indented block


✅  1.4 Task (2 Points)

Now we are going to only extract one value and index. Try to only print out bird from the

animals list.


# write your answer here


2. Looping by Value (10 Points)


Example (goes with Part 1)

In all of the questions above in the previous problem, we had you loop by index.” However,

we can also loop by value,” as the example below demonstrates.


for animal in animals:

print(animal)


Cat

Dog

Bird

Pig

Cow


✅  2.1 Task (2 Points)

In the cell below, try to solve each of the problems above using loop by value.” You dont

have to demonstrate success (or failure) for each problem; just tinker with it and see if

you can solve them.



#Try to solve the problems above using loop by value.

#This cell won't be graded for correctness; it's basically just scratch work


✅  2.2 Task (4 Points)

Which problems can be solved using a loop by value?” Which ones can NOT be solved

using a loop by value?”


Write your answer here


✅  2.3 Task (4 Points)

Is there a problem that could be solved using loop by value that could NOT be solved

using loop by index”? If so, give an example in the (code) cell below. If not, explain why

not in the (text) cell below.



# write an example here

Write your answer here



3. While Loops (10 Points)


Your friend has convinced you to help her out with her catsitting business this weekend. She has

a bunch of clients who are out of town and need their cats to be fed. You offered to tag along,

help drive to different locations, and keep track of the tasks she’ll have to do. She gave you

some relevant information for the day: the names of cats and the number of cans of food they’ll

need to eat. The lists below show this information in the same order as the feeding schedule for

this weekend.