This homework is designed to give you practice in tracing through programming code.
Copy the stencil code for this assignment to your computer
Throughout the stencil code, there are several commented numbers (#1, #2, etc) scattered around. These are break points. At each break point, we want you to write a comment (the lines which begin with a #
) with the following:
If this is still a little confusing, read lines 14-29 in the stencil for a better explanation with examples. Once you've finished, double check your answers by adding print() statements at each break point to see what the variables actually are. If you got any answers wrong, do not correct what you wrote. Instead, write a CORRECTION
line after the mistake with the correct answer (See line 19 in the stencil.)
Rename your file Name_StudentID_HW2-2_TraceCode.py
. You will hand it in as part of your assignment.
Carl Friedrich Gauss (1777–1855) was one of the greatest mathematicians of all time, who contributed significantly to many fields in mathematics, statistics, physics, and astronomy. There is a famous anecdote about him (whether true or not). When he was a little boy in elementary school, his math teacher one day asked the class to add up all the integers from 1 through 100. The teacher thought it would take the kids a long while and that he had just gotten himself some time for a cigarette break. However, to his annoyance, little Carl raised his hand right after he pulled out a match.
“The answer is 5050,” said Gauss. The teacher frowned suspicously and sneered, “You are wrong. Do it again.” Honestly he didn't know the answer himself, but he believed there was no way to come up with the right answer that quickly.But Gauss looked down at his sketch paper and lifted up his head in about five seconds. “I just checked my calculation. I'm sure 5050 is the right answer.”
“Now you're just messing with me,” said the teacher, as he strode through many amazed eyes towards Gauss' desk. “Show me your work.”
“Well, if you add 1 and 100, you get 101. Adding 2 and 99 also gives 101. The same goes for 3 and 98, 4 and 97, etc. There are 50 such pairs from 1 to 100. So the answer is 101 times 50 which is 5050.”
max_val
, where max_val
is a variable with the number to add up to.
max_val
. range(x,y)
that generates a list with a given beginning and end values. Try to call this function with various values of x
and y
in the IDLE interpreter to see what it does. Notice how the first and last values of the list it returns are related to the input arguments you give to range
.) Run it with 100 and see if it produces Gauss' answer. What about 1,000,000? HW2-2.py
. Rename it to Name_StudentID_HW2-2.py
before you hand this in.arg
. (Hint: Iterate as you did in part 1, but add the number to your total only if it is an odd one. Use the function we've provided called isOdd
to help.)We will now have some fun with lists. In starter code, there are two variables: word
, which is a string, and myList
, which is a list.
word
is an element of myList
. To do this:
found
(it's already in the starter code.)myList
, and check if it is the same as word
.found
to True
.word
was found in myList
. We have provided an in-built Python function that does the same check for you, so you know whether your answer was right.word
is in myList
, we will return the index
of word
in myList
(and -1 if word
is not in myList
). Again, the comments in the starter code will guide you.You need to hand in both Name_StudentID_HW2-2_TraceCode.py
and Name_StudentID_HW2-2.py
. Share the files with PolyUCOMP1D04@gmail.com
.
Note: Before you turn in your Python files, make sure they run without any errors(Save your Python file. Then select Run > Run Module
or hit F5
on your keyboard)! If nothing appears in the Shell, don't worry as long as no red error messages appear. If they don't run, i.e. if red stuff starts appearing in the shell, points will be taken off!