# ========== Python 2.7 START ========== import random def IsAnInt(input): retVal = True try: i = int(input) except: retVal = False return retVal def GetNumericInput(prompt): gotAns = False retVal = 0 while(not gotAns): ans = raw_input("{0}: ".format(prompt)) if IsAnInt(ans): gotAns = True retVal = int(ans) else: print "Your entry was not a number." return retVal def GetInput(question): gotAns = False retVal = "" while(not gotAns): ans = raw_input("{0}: ".format(question)) if ans == "": print "Your entry seems empty." else: gotAns = True retVal = ans return retVal def AskToPlayAgain(): gotAns = False retVal = False while(not gotAns): ans = GetInput("Would you like to play again? (\"YES\" or \"NO\")").upper() if ans == "YES" or ans == "Y": retVal = True gotAns = True elif ans == "NO" or ans == "N": retVal = False gotAns = True else: print "Please respond with \"YES\" or \"NO\"" return retVal def Run(): random.seed() Running = True GotMaxNumber = False MaxNumber = 0 Answer = 0 GamOver = False Tries = 0 print "*** GUESS A NUMBER : Python ***" while(Running): while(not GotMaxNumber): MaxNumber = GetNumericInput("Please enter the maximum number you wish to use") if(MaxNumber > 0): GotMaxNumber = True Answer = random.randint(1,MaxNumber) GameOver = False Tries = 0 print "Ok, I am thinking of a number between 1 and {0}".format(MaxNumber) else: print "The maximum number should be greater than zero." Guess = GetNumericInput("Guess a number between 1 and {0}".format(MaxNumber)) Tries = Tries + 1 if(Guess > Answer): print "Too high." if(Guess < Answer): print "Too low." if(Guess == Answer): if(Tries == 1): print "You got it in 1 try!" else: print "You got it in {0} tries!".format(Tries) GameOver = True if(GameOver): Running = AskToPlayAgain() if(Running): GotMaxNumber = False print "Thanks for playing!" print "*** GAME OVER ***" Run() # =========== Python 2.7 END =========== # =========== Python 3 START =========== import random def IsAnInt(input): retVal = True try: i = int(input) except: retVal = False return retVal def GetNumericInput(prompt): gotAns = False retVal = 0 while(not gotAns): ans = input("{0}: ".format(prompt)) if IsAnInt(ans): gotAns = True retVal = int(ans) else: print("Your entry was not a number.") return retVal def GetInput(question): gotAns = False retVal = "" while(not gotAns): ans = input("{0}: ".format(question)) if ans == "": print("Your entry seems empty.") else: gotAns = True retVal = ans return retVal def AskToPlayAgain(): gotAns = False retVal = False while(not gotAns): ans = GetInput("Would you like to play again? (\"YES\" or \"NO\")").upper() if ans == "YES" or ans == "Y": retVal = True gotAns = True elif ans == "NO" or ans == "N": retVal = False gotAns = True else: print("Please respond with \"YES\" or \"NO\"") return retVal def Run(): random.seed() Running = True GotMaxNumber = False MaxNumber = 0 Answer = 0 GamOver = False Tries = 0 print("*** GUESS A NUMBER : Python ***") while(Running): while(not GotMaxNumber): MaxNumber = GetNumericInput("Please enter the maximum number you wish to use") if(MaxNumber > 0): GotMaxNumber = True Answer = random.randint(1,MaxNumber) GameOver = False Tries = 0 print("Ok, I am thinking of a number between 1 and {0}".format(MaxNumber)) else: print("The maximum number should be greater than zero.") Guess = GetNumericInput("Guess a number between 1 and {0}".format(MaxNumber)) Tries = Tries + 1 if(Guess > Answer): print("Too high.") if(Guess < Answer): print("Too low.") if(Guess == Answer): if(Tries == 1): print("You got it in 1 try!") else: print("You got it in {0} tries!".format(Tries)) GameOver = True if(GameOver): Running = AskToPlayAgain() if(Running): GotMaxNumber = False print("Thanks for playing!") print("*** GAME OVER ***") Run() # ============ Python 3 END ============