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
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to