[Tutor] Need a solution.

David david at abbottdavid.com
Fri Jun 12 07:01:14 CEST 2009


Randy Trahan wrote:
> Well that didin't work I tried the program and it went into an infinite 
> loop...is that the problem you were asking about?
> 
> On Fri, Jun 12, 2009 at 12:14 AM, Randy Trahan <mattie289404 at gmail.com 
> <mailto:mattie289404 at gmail.com>> wrote:
> 
>     Hi Raj,
>      
>     The only thing I see is that under #guessing Loop you do not have ()
>     around the code; ie.
>     while (guess != the_number):
>      if (guess > the_number):
>     print "Lower.."
> 
> 
>      
>     On Fri, Jun 12, 2009 at 12:02 AM, David <david at abbottdavid.com
>     <mailto:david at abbottdavid.com>> wrote:
> 
>         Raj Medhekar wrote:
> 
>             I have been having some difficulty modifying this program to
>             where a player has limited number of guesses and if he fails
>             to guess correctly within that number he is asked to "Try
>             Again" The original program code and the code that I
>             modified are given below. Thanks for your help.
> 
>             Sincerely,
>             Raj
> 
>             The Original program code:
> 
>             # 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.randrange(100)+1
>             guess = int(raw_input("Take a guess:"))
>             tries = 1
> 
>             #guessing loop
>             while guess != the_number:
>                if guess > the_number:
>                    print "Lower..."
>                else:
>                    print "Higher..."
> 
>                guess = int(raw_input("Take a guess:"))
>                tries += 1
> 
>             print "You guessed it! The number was", the_number
>             print "And it only took you", tries, "tries!\n"
> 
>             raw_input("\n\nPress the enter key to exit")
> 
>         Hi Raj,
>         I am also learning Python and never did any real programming
>         before, I have been attempting to learn about classes, this is
>         my version, comments, suggestions always welcome.
>         -david
> 
>         #!/usr/bin/python
>         from random import randrange
>         from sys import exit
> 
>         class GuessNumber:
>            def __init__(self):
>                self.number = randrange(100)+1
>                self.count_tries = 1
>                self.total_tries = 5
>            def update_count(self):
>                self.count_tries += 1
> 
>         def guess_number():
>            gn = GuessNumber()
>            number = gn.number
>            tries = gn.total_tries
>            attempts = gn.count_tries
>            update_count = gn.update_count()
>            guess = 'Take a guess: '
>            question = 'Try again? '
>            decision = 'Do you want to try again? y/n '
>            discription = \
>            """\n\tWelcome to 'Guess My Number'!
>            I'm thinking of a number between 1 and 100.
>            Try to guess it in 5 attempts!\n
>            """
>            print discription
>            answer = int(raw_input(guess))
>            while answer != number and attempts < tries:
>                if answer > number:
>                    print 'Lower ...'
>                else:
>                    print 'Higher ...'
>                update_count
>                print 'You have %i attempts left!' % (tries - attempts)
>                answer = int(raw_input(guess))
>                attempts += 1
>            if answer == number:
>                print 'You guessed it the number was', number
>                print 'And it only took you %i attempts' % attempts
>                choice = raw_input(decision)
>                if choice == 'y':
>                    choice = choice.lower()
>                    guess_number()
>                else:
>                    exit()
>            else:
>                print 'You did not pick the number!'
>                print 'The number was', number
>                choice = raw_input(decision)
>                if choice == 'y':
>                    choice = choice.lower()
>                    guess_number()
>                else:
>                    exit()
> 
>         if __name__ == "__main__":
>            guess_number()
> 
> 
> 
> 
> 
>         -- 
>         Powered by Gentoo GNU/Linux
>         http://linuxcrazy.com <http://linuxcrazy.com/>
>         _______________________________________________
>         Tutor maillist  -  Tutor at python.org <mailto:Tutor at python.org>
>         http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> 
>     -- 
>     Randy Trahan
>     Owner, BullDog Computer Services, LLC
>     478-396-2516
> 
> 
> 
> 
> -- 
> Randy Trahan
> Owner, BullDog Computer Services, LLC
> 478-396-2516

Hi Randy,

Looks like you are top posting, on most mailing lists the mail is read 
from top to bottom with the bottom being the most recent. So my attempt 
is on the bottom, the last one. Also on this list you will need to send 
your reply to reply all or reply list to sent the mail to the list.
-david

-- 
Powered by Gentoo GNU/Linux
http://linuxcrazy.com


More information about the Tutor mailing list