[Tutor] guess age programme (still stuck!!!!!)

ADRIAN KELLY kellyadrian at hotmail.com
Fri Sep 30 21:18:15 CEST 2011


please guys
still stuck on this problem and i have been at it for hours so please if anyone can help.  it nearly works. am i looking at it from the wrong angle? i have tried everyone's suggestions but i am stuck still...
correct code would be nice.............
thanksadrian (new pythoner)

print("\tWelcome to 'Guess My Number'!")print("I'm thinking of a number between 1 and 100.")print("Try to guess it in as few attempts as possible.\n")
# set the initial valuesage = 35guess = " "tries = 5
# guessing loopwhile guess!=age and tries>0:    tries=tries-1    guess = input("Take a guess: ")    if guess > age and tries>0:        print "Lower...",tries,"left"    elif guess < age and tries>0:        print "Higher...",tries,"left"    elif guess == age and tries>0:        print "Correct...well done!!"    else:        break        print "Out of attempts"                   
print "\n\nGood guess!!"
  input ("\n\nPress the enter key to exit.")
 
Date: Fri, 30 Sep 2011 14:31:52 +0200
From: cwitts at compuscan.co.za
To: kellyadrian at hotmail.com
CC: tutor at python.org
Subject: Re: [Tutor] guess age programme (please help)



  


    
  
  
    On 2011/09/30 02:04 PM, ADRIAN KELLY wrote:
    
      
      
        Hi all, 

        can anyone help me with the attached programme. it is fairly
        basic (like me)  i want it to ask the user to guess the age and
        countdown when making incorrect guesses.  it doesn't seem to
        count the first attempt (run it and see please).  i also want to
        know how to go about setting a condition so

        that when the guesses = 0, the programme closes or stops etc. 

        any advice would be very grateful (particularly the correct
        code) i am learning if's and loops so please keep explanations
        as 

        simple as possible.

         

        i really appreciate everyones help

         

        adrian 

      
      _______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
    
    

    Your program does count the first guess, but you print out the
    remaining number of tries before you subtract the attempt from your
    counter so if you move your `tries=tries-1` up before your if
    statements it will reflect the correct number of attempts left to
    the user.

    

    I would suggest changing your while loop to read `while tries >
    0:` so it exits when you've exhausted your attempts.  You also do
    not test for equality of your age and the guess in your if
    statements so if the user guesses the correct age it will tell them
    that the number is higher. And if the user guesses the correct
    number you should `break` from the loop else it will carry on going
    till the number of attempts have been exhausted.

    

    

    -- 

      
      Email Signature
      
      Christian Witts

        Python Developer

        

        
     		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110930/ccacdb6b/attachment-0001.html>


More information about the Tutor mailing list