P. 1
IYOCGwP_book1

IYOCGwP_book1

|Views: 412|Likes:
Published by aishwaniraj

More info:

Published by: aishwaniraj on Apr 11, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

01/01/2013

pdf

text

original

if guess == number:

28.

Unlike the code in line 25, this line has no indentation, which means the while-block has ended and this is
the first line outside the while-block. When we left the while block, we did so either because the while
statement's condition was False (when the player runs out of guesses) or if we executed the break
statement (when the player guesses the number correctly). With line 28, check again to see if the player
guessed correctly. If so, we enter the if-block that follows.

guessesTaken = str(guessesTaken)

29.

print('Good job, ' + myName + '! You guessed my number
in ' + guessesTaken + ' guesses!')

30.

Lines 29 and 30 are inside the if-block. They only execute if the condition in the if statement on line 28
was True (that is, if the player correctly guessed the computer's number).

In line 29 (which is similar to the guess = int(guess) code on line 15), we call the new function
str(), which returns the string form of an argument. We use this function because we want to change the
integer value in guessesTaken into its string version because we can only use strings in calls to print
().

Line 29 tells the player that they have won, and how many guesses it took them. Notice in this line that we
change the guessesTaken value into a string because we can only add strings to other strings. If we were
to try to add a string to an integer, the Python interpreter would display an error.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->