CSCE 110 1st EditionLecture 6Outline of Last LectureI. What is a while function?a) Example of a while functionII. Execution order of the lines of code in a while functionIII. Using our new knowledge to write different types of while functionsOutline of Current LectureI. LogarithmsA. What are they?B. How are they useful to us?II. FunctionsCurrent LectureI. LogarithmsA. First let's look at an example of a logarithmic equation. They are similar to exponential equations, only rearranged. Logarithmic: log28 = 3 Exponential: 23 = 8In order to use what is called a binary search we typically will have a base of 2 when using logarithmic functions. B. What does this have to do with what we've learned?Let's revisit the guessing game we wrote in the last set of notes. 1 >>> print "Let's play a guessing game!"2 >>> x = 03 >>> import random4 >>> number = random.randint(1,100)5 >>> while x != number:6 >>> x = int(raw_input("Guess a number! "))7 >>> if number == x:8 >>> print "You got it!"9 >>> elif x < number:10 >>> print "Higher!"11 >>> else:12 >>> print "Lower!"These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.This game should never require more than 7 guesses to get the right number. This is because log2(100) is equal to some number between the integers 6 and 7. We say that 7 is the "ceiling" in order for the maximum number of guesses needed to be an integer. The way we can use this to our advantage in our guessing game is make our next guess the halfway point of our new range, based on whether the computer tells us "higher" or "lower." This method should be usedno matter how large the range of numbers is. For example, if we used this strategy when guessing a number 1-1000, it would only take a maximum of 10 guesses, for log21000 is equal tosome number between 9 and 10 (so we take the ceiling, 10).II. FunctionsFunctions are essentially sets of commands that are executed whenever the name of the function is called by the user. They are defined by the user, and are written in the following format:>>> def printing():>>> print "A">>> print "B">>> print "C"Every function begins with "def" (for it is defining a word to mean a set of commands), is followed by the name of the function, and immediately after (no space), the line ends with "():" - this may or may not have something within the parentheses (we will talk about that more in the next set of notes). Every time the function "printing" is called, each command indented under it will be executed. Let's look at an example:1 >>> def printing():2 >>> print "A"3 >>> print "Z"4 >>> def count():5 >>> start = 06 >>> end = 107 >>> while start <= 2:8 >>> print start9 >>> start = start + 110 >>> printing()11 >>> count()We have the definitions of the function, and then we call them in lines 10 and 11. The program reads the lines in this order: 10, 1, 2, 3, 11, 4, 5, 6, 7, 8, 9, 7, 8, 9, 7, 8, 9. Once the function is called, the computer goes back up to the definition and reads those lines assigned to it. In otherwords, the program jumps around a lot. Functions are very useful. If we want to perform thesame series of operation multiple times, all we have to do is call it, using one word, however many times we
View Full Document