Skip to content

Instantly share code, notes, and snippets.

@abhiomkar
Created January 16, 2010 11:12
Show Gist options
  • Save abhiomkar/278784 to your computer and use it in GitHub Desktop.
Save abhiomkar/278784 to your computer and use it in GitHub Desktop.
Python Game: Guess Number Game
#!/usr/bin/python
#Author: Abhinay Omkar
#Title: Guess Number Game
import random
class GuessNumber:
def startGame(self):
print 'Guess a number!'
t=0
for t in xrange(6):
num = raw_input("\ntry ("+str(t+1)+")] ")
try:
inp = int(num)
except ValueError:
inp=0
if(inp == random.randint(1,10)):
print "\nWinner! You guessed the number in "+str(t+1)+" guesses! Well done!"
return self.quitGame()
else:
print random.choice(['nah!', 'what?', 'try again plz', 'No', 'he he..', 'bad'])
print 'You reached maximum number of guesses :('
return self.quitGame()
def quitGame(self):
if(str(raw_input("\nTry Again? (Y/n)")).lower() in ('y','')):
# Game is Interesting! :)
return True
else:
# Game is NOT Interesting! :(
print "Thx for using our useless games! Bye! ;)"
return False
if __name__ == '__main__':
G = GuessNumber()
GameIsInteresting = True
while GameIsInteresting:
GameIsInteresting = G.startGame()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment