Last active
May 5, 2017 09:30
-
-
Save akinjide/4d8d37016d35fa7befddb5ed78da5c1f to your computer and use it in GitHub Desktop.
A simple Guess The Number game implementation in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randrange | |
class Guess: | |
def play(self): | |
print "The computer will select a secret number between 1 and 10." | |
print "Try to find the secret number using a minimum number of guesses." | |
play, secret, guess, guesses, answer = True, 0, 0, 0, None | |
while play: | |
secret = randrange(1, 10) | |
while guess != secret: | |
guess = int(raw_input("Your guess: ")) | |
if guess < secret: | |
print "The secret number is greater than %s. Guess again!" % (guess) | |
elif guess > secret: | |
print "The secret number is less than %s. Guess again!" % (guess) | |
guesses += 1 | |
print "Congratulations :)" | |
print "You found the secret number (%s) in %s guess(es)." % (secret, guesses) | |
while answer != "yes" and answer != "no": | |
answer = raw_input("Play again? (yes or no): ").lower() | |
if answer == "no": | |
play = False | |
print "Thank you for playing :)" | |
guess = Guess() | |
guess.play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment