06.0 Python‎ > ‎Python Projects‎ > ‎Guess It Project‎ > ‎

Guess It v0 Once

Objective

The student will learn the following new concepts:
  • import
  • the dot . operator
  • random
    • .seed()
    • .randint(start##,stop##)
  • Use the Python Standard Library
  • write or finish writing pre and post conditions for each method
The student will practice the following programming concepts:
  • building independent specific task oriented functions (methods)
  • print
  • integer
  • input
  • selection programming structures (if else)
  • conditional repetitive programming structures (loops) 

Project Description:

  • See the Random Number Example
  • starting with the stub program, complete each function to complete the task as its name describes. The functions you need to complete are:
    • nextGuess()
    • printHint(guess, secret)
  • write or finish writing pre and post conditions for each method
  • This version of the program will only allow the user to have one guess at the secret number. You will add multiple guesses in the next version.

Program Stub

import random 
 
def nextSceretNumber(): 
    """ Sets a random secret number 
        pre-condition:   None 
        post condition:  a random number from 1 to 10 is returned 
    """ 
    return random.randint(1,10) 
 
def nextGuess(): 
    """ prompts the user for a guess 
        this method assumes a valid integer will be entered by the user 
        pre-condition:   none 
        post condition:  returns the users guess 
    """ 
    usersGuess = int(input("Enter Your Guess:  ")) 
    return usersGuess 
 
def isCorrect(guess, secret): 
    """  
        :param guess 
        :param secret 
        :return True if the the guess and the secret numbers are the same 
 
        pre-condition: the guess and secret number are available 
        post condition: returns True if the the guess and the secret numbers are the same 
    """ 
    return (guess == secret) 
 
def isNotCorrect(guess, secret): 
    """  
        :param guess 
        :param secret 
        :returns True if the the guess and the secret numbers are NOT the same 
         
        pre-condition:  the guess and secret number are available 
        post condition:  returns True if the the guess and the secret numbers are NOT the same 
    """ 
    return not isCorrect(guess,secret) 
 
def printHint(guess, secret): 
    """ :param guess 
        :param secret 
         
        pre-condition:  the guess in not correct 
        post condition:  prints a hint to the user to guess higher or lower 
    """ 
    print ("You are NOT correct") 
 
def printAWinner(guess): 
    """ 
        pre-condition:  The user has guessed the correct number 
        post condition:   The winning message is displayed 
    """ 
    print ("You are correct") 
 
def playGuessIt(): 
    """ 
        pre-condition: None 
        post condition: one game of Guess has been played 
    """ 
    secret=nextSceretNumber() 
    guess=nextGuess() 
    if isNotCorrect(guess, secret): 
        printHint(guess,secret) 
    else: 
        printAWinner(guess) 
 
 
def main(): 
    playGuessIt() 
 
if __name__ == '__main__': 
    main() 
 

PART 3

Count the number of guesses it take the user find the secret number
Comments