On 08/06/2017 11:35 AM, boB Stepp wrote:

> So these final two "if" groupings should be _outside_ your while loop:
> while guessesTaken < 6:
>     <All of your earlier code inside the while loop>
> if guess_value == number:
>     print('good job, ' + myName + '! you guessed my number in',
> guessesTaken, 'guesses!')
> else:
>     print('nope. the number i was thinking of was', number)
> Notice the differences in indentation between what is _inside_ the
> while loop and what now follows _outside_ the while loop.  Also
> instead of two consecutive "if" blocks, I used the "if - else"
> structure.  If the "if" condition is not true then the code will
> automatically execute the "else" block.  Per what the others have
> said, I did not convert "guessesTaken" and "number" to strings,  The
> print function will handle that for us.  Also, with the print function
> if items to be printed are listed as separate arguments, that is,
> separated by commas, the default behavior  of print is to insert a
> single space in the place of each comma.

Yes, to highlight this last comment, the code as written is building one
string to pass to print, in other words it is similar to doing:

x = chunk1 + chunk2 + chunk3

of course there's no assignment to 'x' taking place, I just use that to
illustrate that print is called with one argument.

and Bob is suggesting you can also let print do the combining:

print(chunk1, chunk2, chunk3)

it which case it applies a little formatting work for you (inserting the
space).  There will be lots more options to string formatting for print.

Meanwhile, it is worth pointing out that while: (as with other python
loops) can take an else: clause, which is executed if the loop runs to
completion and was not exited via break.  That means you could ALSO
write (this is pseudo-code for brevity):

while guessesTaken < 6:
    if guess_value == number

# if we got here it's because a guess was correct

Using this is no more or less correct than not using it, just pointing
it out for your learning about Python loops.  Apparently some people
think while/else, for/else and so on are useless, or vile, or whatever.
But they're certainly a part of the language.

Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:

Reply via email to