In Lecture 4, the professor says that even without his explanation, we should be able to understand this code (see below). Unfortunately that is not the case so far. I can't get it to do anything when I run it in IDLE. Can anyone help me figure out how to make sense of it?
I copied this directly from the file "Lec04.py"
def findRoot(pwr, val, epsilon):
"""assumes pwr an int; val, epsilon floats
pwr and epsilon > 0
if it exists,
returns a value within epsilon of val**pwr
otherwise returns None"""
assert type(pwr) == int and type(val) == float\
and type(epsilon) == float
assert pwr > 0 and epsilon > 0
if isEven(pwr) and val < 0:
low = -abs(val)
high = max(abs(val), 1.0)
ans = (high + low)/2.0
while not withinEpsilon(ans**pwr, val, epsilon):
#print 'ans =', ans, 'low =', low, 'high =', high
if ans**pwr < val:
low = ans
high = ans
ans = (high + low)/2.0
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
The indentation may be out, try re-writing it below the original copy in python to see if it aligns correctly.
Also this function calls two other functions:
withinEpsilon(ans**pwr, val, epsilon)
if these functions haven't also been copied into python then it won't know what its referring to.
the second function above should look something like:
def withinEpsilon(x, y, epsilon):
if abs(x-y) < epsilon:
I'm new to this also so apologies if this wasn't of much help
def means define, so this is a function that needs to be called, it won't execute on it's own. To make it run, probably the simplest thing to do would be to put:
print findRoot( 2, 16.0, 0.01 )
with NO indentation at the bottom and then tell IDLE to run it.
Thanks to both of you. I'm troubleshooting this now, with your help.
That file looks like a dump of the teacher's computer screen from the lecture. IMO, the value of the file is to make reading what the teacher's showing during the lecture much easier. You can play around with, or study the code, but it looks to be more of a screen dump with several different functions and function calls.
Makes sense. But at the end of the lecture, he mentions that we should be able to understand everything in the handout in order to tackle the next problem set, and also in order to maintain a basic grasp of functions. This is where I grew frustrated, as I don't understand it in the least.
Functions are like paragraphs of code that do one, or part of one, thing. They need to be defined. When they're called, they're usually given data to work on. Once they're finished, they frequently return an answer.
def add( firstNumber, secondNumber ):
return firstNumber + secondNumber
print add( 2 + 2 )
The definition ( def ) defines the name of the function ( add ), and in the parentheses are the names for the passed in variables that the function is going to use. The colon ( ':' ) starts the function body ( the code that's going to execute ). The return statement will return the value to the calling code.
The print statement prints the returned value from the function, which is substituted for the function call ( which is done by using the function's name and giving it values/variables in the parenthesis ).
If this didn't help, you're going to have to ask a more specific question.
this is a prime example of how the lecturer assumes that the students (both in the class and watching the ocw) already have some prerequisite knowledge that is not stipulated in the course work. it's difficult to continue trying to learn, when there are obviously snippets (or larger bits) of info that are not being passed on or explained.
i feel that the book written by the lecturer should be free if you sign up for this study group. as i'm pretty sure that the information he's NOT giving us, is included in that book.