コード例 #1
0
def advancedGuessingGame():
    """Play a guessing game with a user.

    The exercise here is to rewrite the exampleGuessingGame() function
    from exercise 3, but to allow for:
    * a lower bound to be entered, e.g. guess numbers between 10 and 20
    * ask for a better input if the user gives a non integer value anywhere.
      I.e. throw away inputs like "ten" or "8!" but instead of crashing
      ask for another value.
    * chastise them if they pick a number outside the bounds.
    * see if you can find the other failure modes.
      There are three that I can think of. (They are tested for.)

    NOTE: whilst you CAN write this from scratch, and it'd be good for you to
    be able to eventually, it'd be better to take the code from exercise 2 and
    marge it with code from excercise 1.
    Remember to think modular. Try to keep your functions small and single
    purpose if you can!
    """
    low = not_number_rejector("Enter a lower bound as an integer: ")
    high = not_number_rejector("Enter an upper bound as an integer: ")
    while high <= (low + 1):
        high = not_number_rejector("Upper bound too low: ")
    guess = super_asker(low, high)
    print(low)
    print(guess)
    print(high)
    return "You got it!"
コード例 #2
0
def advancedGuessingGame():
    """Play a guessing game with a user.

    The exercise here is to rewrite the exampleGuessingGame() function
    from exercise 3, but to allow for:
    * a lower bound to be entered, e.g. guess numbers between 10 and 20
    * ask for a better input if the user gives a non integer value anywhere.
      I.e. throw away inputs like "ten" or "8!" but instead of crashing
      ask for another value.
    * chastise them if they pick a number outside the bounds.
    * see if you can find the other failure modes.
      There are three that I can think of. (They are tested for.)

    NOTE: whilst you CAN write this from scratch, and it'd be good for you to
    be able to eventually, it'd be better to take the code from exercise 2 and
    marge it with code from excercise 1.
    Remember to think modular. Try to keep your functions small and single
    purpose if you can!
    """
    print("\nLet's play a game")
    print("\nStart by picking a number")
    lower_bound = not_number_rejector("Enter a number: ")
    lower_bound = int(lower_bound)
    greater = False
    print("Now pick another number which is greater than {}  "
          "plus 1".format(lower_bound))
    while not greater:
        upper_bound = not_number_rejector("Enter number : ")

        if upper_bound > lower_bound + 1:
            greater = True
        elif upper_bound == lower_bound + 1:
            print("The number must be greater "
                  "than {} + 1 ".format(lower_bound))
        elif upper_bound == lower_bound:
            print("The number can't equal {}, try again".format(lower_bound))
        else:
            print("That number is not greater "
                  "than {}, try again ".format(lower_bound))
    upper_bound = int(upper_bound)
    guessed = False

    actual_number = random.randint(lower_bound + 1, upper_bound - 1)
    print("Now guess a number   ")

    while not guessed:
        guessed_number = super_asker(lower_bound, upper_bound)
        guessed_number = int(guessed_number)
        print("you guessed {}".format(guessed_number))
        if guessed_number == actual_number:
            print("You got it!!")
            guessed = True
        elif guessed_number < actual_number:
            print("Too low, try again")
        else:
            print("Too high, try again")
    return "You got it!"
コード例 #3
0
def test_super_asker(low, high):
    """Test the super asker function."""
    dirty_things = ["aword", [1, 2, 3], {"an": "object"}]
    neat_range = range(low - 25, high + 20, 5)
    mockInputs = dirty_things + neat_range
    try:
        with mock.patch('__builtin__.raw_input', side_effect=mockInputs):
            return exercise1.super_asker(low, high)
    except Exception as e:
        print("exception:", e)
コード例 #4
0
ファイル: exercise3.py プロジェクト: cathyvu/cathyvu
def advancedGuessingGame():
    """Play a guessing game with a user.

    The exercise here is to rewrite the exampleGuessingGame() function
    from exercise 3, but to allow for:
    * a lower bound to be entered, e.g. guess numbers between 10 and 20
    * ask for a better input if the user gives a non integer value anywhere.
      I.e. throw away inputs like "ten" or "8!" but instead of crashing
      ask for another value.
    * chastise them if they pick a number outside the bounds.
    * see if you can find the other failure modes.
      There are three that I can think of. (They are tested for.)

    NOTE: whilst you CAN write this from scratch, and it'd be good for you to
    be able to eventually, it'd be better to take the code from exercise 2 and
    marge it with code from excercise 1.
    Remember to think modular. Try to keep your functions small and single
    purpose if you can!
    """
    print("\nwelcome to the guessing game!")
    print("A number between _ and _ ?")

    lowerBound = not_number_rejector("Please give me a lower bound: ")

    while True:
        upperBound = not_number_rejector("Please give me an upper bound: ")
        if lowerBound < upperBound + 1:
            break

    print("OK then, a number between {} and {} ?".format(
        lowerBound, upperBound))

    actualNumber = random.randint(lowerBound, upperBound)
    print(actualNumber)

    guessed = False

    while not guessed:
        guessedNumber = super_asker(lowerBound, upperBound)
        print("you guessed {},".format(guessedNumber))
        if guessedNumber not in range(lowerBound, upperBound + 1):
            print("The gussed number is not between {} and {}.".format(
                lowerBound, upperBound))
        elif guessedNumber == actualNumber:
            print("you got it!! It was {}".format(actualNumber))
            guessed = True
        elif guessedNumber < actualNumber:
            print("too small, try again ")
        else:
            print("too big, try again   ")
        return "You got it!"
コード例 #5
0
def advancedGuessingGame():
    """Play a guessing game with a user.

    The exercise here is to rewrite the exampleGuessingGame() function
    from exercise 3, but to allow for:
    * a lower bound to be entered, e.g. guess numbers between 10 and 20
    * ask for a better input if the user gives a non integer value anywhere.
      I.e. throw away inputs like "ten" or "8!" but instead of crashing
      ask for another value.
    * chastise them if they pick a number outside the bounds.
    * see if you can find the other failure modes.
      There are three that I can think of. (They are tested for.)

    NOTE: whilst you CAN write this from scratch, and it'd be good for you to
    be able to eventually, it'd be better to take the code from exercise 2 and
    marge it with code from excercise 1.
    Remember to think modular. Try to keep your functions small and single
    purpose if you can!
    """
    print("\nwelcome to the guessing game!")

    lowerbound = not_number_rejector("Enter a lower bound: ")
    while True:
        upperbound = not_number_rejector("Enter a upper bound: ")
        if upperbound <= lowerbound:
            print ("Type in a number over {}".format(upperbound))
        elif upperbound == lowerbound + 1:
            print ("Type in a number over {}".format(upperbound))
        elif lowerbound >= upperbound:
            print("{} is incorrect, please do one bellow upperbound"
                  .format(lowerbound))
        else:
            print("go ahead")
            break

    actual_number = random.randint(lowerbound, upperbound)

    guessed = False
    while not guessed:
        guess_number = super_asker(lowerbound, upperbound)
        print("You guessed {}".format(guess_number),)
        if guess_number == actual_number:
            print("{}, You guessed correctly".format(actual_number))
            guessed = True
        elif guess_number < actual_number:
            print("incorrect, try larger number")
        else:
            print("incorrect, try smaller number")
    return "You got it champ!"