A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

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: return None 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 else: high = ans ans = (high + low)/2.0 return ans

  • This Question is Open
  1. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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: isEven(pwr) 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: return True else: return False I'm new to this also so apologies if this wasn't of much help

  2. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks to both of you. I'm troubleshooting this now, with your help.

  4. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    The problem is that he didn't name or specify which code is interconnected and which is stand-alone, so we're left to guess. I'm attaching the whole file: any help appreciated

    1 Attachment
  5. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  6. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  7. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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. Simple example: 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.

  8. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  9. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.