Wikibooks example doesn't work

Seymore4Head Seymore4Head at Hotmail.invalid
Wed Aug 6 23:48:09 EDT 2014


On Thu, 07 Aug 2014 13:43:40 +1000, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:

>Seymore4Head wrote:
>
>> On Wed, 06 Aug 2014 22:58:51 -0400, Seymore4Head
>> <Seymore4Head at Hotmail.invalid> wrote:
>> 
>>>number = 7
>>>guess = -1
>>>count = 0
>>> 
>>>print("Guess the number!")
>>>while guess != number:
>>>    guess = int(input("Is it... "))
>>>    count = count + 1
>>>    if guess == number:
>>>        print("Hooray! You guessed it right!")
>>>    elif guess < number:
>>>        print("It's bigger...")
>>>    elif guess > number:
>>>        print("It's not so big.")
>> 
>> The part to here is supposed to be an example to allow the user to
>> guess at a number (7) with an infinite amount of tries.
>> 
>> 
>> This part was added as an exercise.
>
>
>Ah, now things make sense! Your subject line is misleading! It's not that
>the wikibooks example doesn't work, the example works fine. It's that the
>code you added to it doesn't do what you expected. You should have said.
>
I copied it verbatim from the web page's solution.  After indenting as
you suggested, it does work now though.
Thanks

>
>> A counter is added to give 3 tries to guess the number.
>> It is supposed to stop after count gets to 3.  It doesn't.  It just
>> keeps looping back and asking for another guess.
>
>You don't check the counter until after the loop has finished. It needs to
>be inside the loop, not outside:
>
>while looping:
>    # See the indent?
>    # this is inside the loop
>
># No indent.
># This is outside the loop.
>
>
>Also, having reached the count of three, you will want to break out of the
>loop. The "break" command does that.
>
>Is this enough of a hint to continue? Please feel free to ask any further
>questions you need.



More information about the Python-list mailing list