[Tutor] Guess my number game

Joel Goldstick joel.goldstick at gmail.com
Sat Dec 7 16:21:31 CET 2013


On Sat, Dec 7, 2013 at 9:16 AM, Mark Lawrence <breamoreboy at yahoo.co.uk>wrote:

> On 06/12/2013 23:34, Lelani Slabber wrote:
>
>> Hi,
>> I am learning Python witht Python for beginners book by Michael Lawson
>> and have trouble with one task in chapter 3 - challenge 3.
>> I have to add code so the user has a limited number of tries - in this
>> case I have set it to less than 5 in the while loop and I want the
>> program to stop if the tries are equal to 5.  I get an invalid syntax
>> error.  Please help.
>> # Guess My Number
>> #
>> # The computer picks a random number between 1 and 100
>> # The player tries to guess it and the computer lets
>> # the player know if the guess is too high, too low
>> # or right on the money
>> import random
>> print("\tWelcome to 'Guess My Number'!")
>> print("\nI'm thinking of a number between 1 and 100.")
>> print("Try to guess it in as few attempts as possible.\n")
>> # set the initial values
>> the_number = random.randint(1, 100)
>> guess = int(input("Take a guess: "))
>> tries = 1
>> # guessing loop
>> while (guess != the_number) and (tries <5):
>>      if guess == the_number:
>>          print("You guessed it")
>>
>>      else:
>>          if guess > the_number:
>>              tries=tries +1
>>              print("Higher...")
>>              guess = int(input("Take a guess: "))
>>          else:
>>              tries=tries+1
>>              print("too low")
>>              guess = int(input("Take a guess: "))
>>              else:
>>
>
> Telling us where you got the syntax error often helps :)  But in this case
> I'd hazard a guess that it's in the line above.  I'll leave you to
> restructure your code as you see fit as part of your learning curve.
>
>
>                   if tries == 5:
>>                      break
>>
>> print("You guessed it!  The number was", the_number)
>> print("And it only took you", tries, "tries!\n")
>>
>> input("\n\nPress the enter key to exit.")
>>
>>
> As Mark pointed out, you have a 'dangling else' which isn't paired with
and if.  Think about what that block would do anyway.  It checks to see if
you have run out of changes and leaves the loop.  However, you already have
code that will leave the loop when the guess is correct -- the 'while'
clause does this

You also have nearly identical code in each part of here:

         if guess > the_number:
             tries=tries +1
             print("Higher...")
             guess = int(input("Take a guess: "))
         else:
             tries=tries+1
             print("too low")
             guess = int(input("Take a guess: "))

Why not check if higher or lower, print that message, THEN do this below
the if/else:
             tries=tries +1
             guess = int(input("Take a guess: "))

This way you don't repeat these two lines of code.

Also, send message in plain text, not html or whatever is was formatted
in.  And cut and paste the actual traceback along with a run of your code.
The traceback usually tells you exactly where your problem lies.

-- 
Joel Goldstick
http://joelgoldstick.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131207/6dd87154/attachment.html>


More information about the Tutor mailing list