• No results found

Step by Step, One More Time

Let's go over the code one more time. To help you understand everything, I will briefly go through the program just like the computer would, starting from the top. We will remember what the values of variables are ourselves (you can write them down on a piece of paper as we go). This is called tracing

through the program. It's what programmers do to figure out exactly how the program will behave. Some lines of code are executed more than once (because they are inside loops), so we will go over those lines of code more than once.

This line is a comment. The computer will ignore this line, and move down to line 2.

2. import random

This line will import the random module so that we can use the randint() function in our program. Line 3 is blank, so the computer will skip ahead to line 4.

4. guessesTaken = 0

The computer will create a new variable called guessesTaken, and the integer 0 will be stored inside this variable.

6. print 'Hello! What is your name?'

A greeting is displayed to the player.

7. myName = raw_input()

The raw_input() function is called, and will let the user type in a string. This string is then stored in a variable called myName. Let's pretend that when the program runs, the player types in Bob. The value of the myName variable is the string, 'Bob'.

On line 9 we call the randint() function, which is inside the random module. Because this function is inside a module we imported, we have to put the module name and a period in front of the function name. The two arguments we pass are the integers 1 and 20. This tells the randint()

function to return a random integer between 1 and 20 (including 1 and 20). Let's pretend that it returns the integer 8. The value of number will be 8.

10. print 'Well, ' + myName + ', I am thinking of a number between 1 and 20.'

Because the value inside myName is the string 'Bob', this will print out Well, Bob, I am thinking of a number between 1 and 20.

12. while guessesTaken < 6:

This is the start of a while-block. If the condition is True, then the program execution will enter the while-block. If the condition is False, we will skip past the while-block to line 28. The variable

guessesTaken has 0 stored inside of it, and 0 is less than 6, which makes the condition True. So the next line to run is line 13.

13. print 'Take a guess.' # There are four spaces in front of print.

We print a message that asks the player to type in a value. There is a comment on this line that the computer ignores. The comment reminds the programmer that we should put four spaces at the beginning of the line because we are now inside a block.

The player now types in a string, and this string will be stored in the guess variable. Let's pretend that the player typed in the string '12'.

15. guess = int(guess)

We want to store the integer value of what the player typed in, not the string value. int() function will return the integer value of the argument we give it. (The argument is the value in between the parentheses next to the function name "int".) The guess variable holds the string '12', so '12' is the argument we pass to the int() function, and the integer value 12 is what the int() function returns. This value is then stored as the new value in the guess variable. After this line runs, guess stores the integer 12 instead of the string '12'.

17. guessesTaken = guessesTaken + 1

The value stored in guessesTaken is 0 (this was set on line 4). We want to keep track of how many guesses the player has taken, so we make the new value of guessesTaken to be the current value of guessesTaken plus one. After this line executes, guessesTaken will now hold the integer 1.

19. if guess < number:

Now we check if the if-statement's condition is True. The value of guess is the integer 12 (set on line 15), and the value of number is 8 (set on line 9). 12 is not less than 8, so this condition is False. That means we will skip the if-block that follows and go directly to line 22.

22. if guess > number:

the if-block at line 23.

23. print 'Your guess is too high.'

We display a message that tells the player their guess was too high.

25. if guess == number:

The condition in this if-statement is False, because 12 is not equal to 8. We skip the if-block that follows. But line 28 has fewer spaces than the four spaces we have been indenting our code inside the while-block. That means we have reached the end of the while-block too, and execution will loop back to the while-statement on line 12.

12. while guessesTaken < 6:

The condition for the while-statement is True, because guessesTaken is 1, but 1 is still less than

6. So the program execution enters the while-block at line 13.

13. print 'Take a guess.' # There are four spaces in front of print.

We display this message to the player again.

We get a string typed by the player, and store it in the variable guess. Let's pretend that the user typed in the string '6'. The string '6' is stored in the variable guess, and the old value of 12 is forgotten.

15. guess = int(guess)

We want to get the integer value of the string inside guess. We pass the int() function an argument of '6', and it will return 6. The new value of guess is the integer 6.

17. guessesTaken = guessesTaken + 1

We want to increase the number of guesses taken by one, so the new value of guessesTaken is the current value (the integer 1) plus one. The new value of guessesTaken is 2.

19. if guess < number:

We check to see if this if-statement's condition is True. It is, because 6 is less than 8. That means our program's execution will enter the if-block at line 20.

20. print 'Your guess is too low.' # There are eight spaces in front of print.

We display a message to the player tell them that their guess was too low. The text after the # pound sign is a comment and is ignored.

22. if guess > number:

We check if guess (the integer 6) is greater than number (the integer 8). It is not, so this condition is

False and we skip the if-block.

25. if guess == number:

We check if guess (the integer 6) is equal to number (the integer 8). It is not, so this condition is

False and we skip the if-block. We have reached the end of the while-block, so we jump back to line 12.

12. while guessesTaken < 6:

This time when we check the condition, guessesTaken has the value 3. But 3 is still less than 6, so the condition is True and we enter the while-block again.

13. print 'Take a guess.' # There are four spaces in front of print.

We ask the player to type in a number again.

14. guess = raw_input()

The function call to the raw_input() function lets the player type in a string. Let's pretend that the player types in the string '8'. Then the new value of guess is '8'.

15. guess = int(guess)

We want to get the integer value of the string inside guess. We pass the int() function an argument of '8', and it will return 8. The new value of guess is the integer 8.

17. guessesTaken = guessesTaken + 1

We want to increase the number of guesses taken by one, so the new value of guessesTaken is the current value (the integer 2) plus one. The new value of guessesTaken is 3.

19. if guess < number:

We check if guess (the integer 8) is less than number (the integer 8). It is not. (If I had 8 apples and you had 8 apples, you would not say I had less apples than you because we have an equal number of apples.) This condition is False and we skip the if-block. Next we execute line 22.

22. if guess > number:

We check if guess (the integer 8) is greater than number (the integer 8). It is not, so this condition is False and we skip the if-block. Next we execute line 25.

We check if guess (the integer 8) is equal than number (the integer 8). It is, so we enter the if- block at line 26.

26. break

The break statement tells us to break out of the while-block that we are inside, and go to the first line after the while-block. This will be line 28.

28. if guess == number:

We check if guess (the integer 8) is equal than number (the integer 8). It is, so we enter the if- block at line 29.

29. guessesTaken = str(guessesTaken)

On this line we convert guessesTaken to the string '3'.

30. print 'Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!'

Now we display the winning message to the player. The variable myName holds the string value

'Bob' and guesses holds the string value '3', so the final string printed is 'Good job, Bob! You guessed my number in 3 guesses!'

This condition will evaluate to False, so we skip past the if-block that follows it. But there is no more code after it, so the program terminates.