The following weeks mini-project was starting to get a bit trickier.

A simple game called, “Guess the number”

# http://www.codeskulptor.org/#user38_p561LbO9KO_0.py # http://www.codeskulptor.org/#user38_SeTv0Tz7E7nAlil_11.py # Mini-project #2 - "Guess the number game" # Created by Mpho "!nerd" Mphego - Marion Island SANSA Engineer 2014-2015 # # 'Introduction to Interactive Programming in Python' Course # RICE University - https://class.coursera.org/interactivepython-005 # by Joe Warren, John Greiner, Stephen Wong, Scott Rixner # One of the simplest two-player games is “Guess the number” # The first player thinks of a secret number in some known range while # the second player attempts to guess the number. After each guess, # the first player answers either “Higher”, “Lower” or “Correct!” # depending on whether the secret number is higher, lower or equal to the guess. # http://www.codeskulptor.org/#user38_SeTv0Tz7E7nAlil_12.py try: import simplegui except: import SimpleGUICS2Pygame.simpleguics2pygame as simplegui # To run simplegui in idle python, install SimpleGUICS2Pygame module # download module : https://pypi.python.org/pypi/SimpleGUICS2Pygame import random import math # Global Variables secret_number = 0 limit = 100 No_of_attempts = int(math.ceil(math.log(limit + 1 ,2))) def new_game(): global secret_number,guess secret_number = random.randint(0, 100) def range100(): global secret_number, limit, No_of_attempts limit = 100 secret_number = random.randint(0, limit) No_of_attempts = int(math.ceil(math.log(limit,2))) #print "Lets Play A Game!!!\n" print "\nNew Game.\nGuess a number between 0 to 100" print "You have about %s attempts at this!" %(No_of_attempts) def range1000(): global secret_number, limit, No_of_attempts limit = 1000 secret_number = random.randint(0, limit) No_of_attempts = int(math.ceil(math.log(limit,2))) print "Lets Play A Game!!!\nGuess a number between 0 to 1000" print "You have about %s attempts at this!" %(No_of_attempts) def input_guess(guess): global No_of_attempts, secret_number try: guess = int(guess) # print No_of_attempts #print "\nYour guess was", guess # print "You have %s "%(No_of_attempts) No_of_attempts -= 1 if No_of_attempts > 0: print "\nYour guess was", guess # for No_of_attempts in range(No_of_attempts): # print guess, secret_number, No_of_attempts # No_of_attempts -= 1 if (guess > secret_number): print "Go Lower!!!!!" print "No. of Attempts left:",No_of_attempts elif(guess < secret_number): print "Go Higher!!!!!" print "No. of Attempts left:",No_of_attempts else: print "\nCorrect!!!\n" print "You guessed it right!!!\nCorrect number was %s, you had %s attempts left...\nChoose a New game. " %(secret_number, No_of_attempts) #break else : print "\nTime up, You Lost\nCorrect number was %s"%(secret_number) print "Try again a new game." #new_game() range100() except: print 'Input is not a number!!!\nTry Again!!!' def end_game(): print "\t*******************" print "\t* *" print "\t* Game Over!!! *" print "\t* *" print "\t*******************" frame.stop() def Init_screen(): print "\t***************************" print "\t* I want to Play a Game. *" print "\t* The rules are simple. *" print "\t* All you have to do is- *" print "\t* sit here and guess, a *" print "\t* number I am thinking *" print "\t* ---JigSaw *" print "\t* *" print "\t***************************" range100() frame = simplegui.create_frame('Guess the number', 200, 200) frame.add_button('Range is [ 0 - 100 ]', range100,200) frame.add_button('Range is [ 0 - 1000 ]', range1000, 200) frame.add_input('Enter A Guess', input_guess, 40) frame.add_button('End Game', end_game, 70) frame.start() # call new_game Init_screen() new_game()