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!"
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!"
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! """ """Play a game with the user. This is an example guessing game. It'll test as an example too. """ print("\welcome to the guessing game!") lowerbound = not_number_rejector("Enter a lower bound") upperbound = not_number_rejector("Enter a upper bound") while lowerbound >= upperbound: upperbound = not_number_rejector( "Enter an upper bound higher than the lower") print("OK then, a number between {} and {} ?".format( lowerbound, upperbound)) actualNumber = random.randint(lowerbound, upperbound) guessed = False while not guessed: try: guessedNumber = int(input("guess a number: ")) print("you guessed {},".format(guessedNumber)) if guessedNumber > actualNumber: print("too big, try again ") elif guessedNumber < actualNumber: print("too small, try again ") else: print("you got it!! It was {}".format(actualNumber)) guessed = True except ValueError: print("That's not a number, try again.") continue return ('You got it!')
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("Enter an lower bound: ") flag = False while not flag: upperBound = not_number_rejector("Enter an upper bound: ") if (upperBound>=lowerBound): flag = True else: print("the upperBound is less than lowerBound") print("OK then, a number between {} and {} ?".format(lowerBound,upperBound)) actualNumber = random.randint(lowerBound, upperBound) guessed = False xstr = "" while not guessed: try: xstr = input("guess a number: ") guessedNumber = int(xstr) print("you guessed {},".format(guessedNumber),) if (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 ") except Exception: print("you entered {} is not a number".format(xstr)) return "You got it!"
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 Guessing Game") lowerBound = not_number_rejector("Enter a lower bound:") print("Now enter the upper bound:") upperBound = not_number_rejector("Enter an upper bound:") while upperBound <= lowerBound: print("re-enter upper bound") upperBound = not_number_rejector("Enter an upper bound:") pass print("then enter a number between {} and {}?".format( lowerBound, upperBound)) lowerBound = int(lowerBound) upperBound = int(upperBound) actualNo = random.randint(lowerBound, upperBound) guessed = False while not guessed: try: guessedNo = int(raw_input("guess a number:")) print("you guessed {},".format(guessedNo), ) if guessedNo == actualNo: print("you got it! it was {}".format(actualNo)) guessed = True elif guessedNo < lowerBound: print("out of range") elif guessedNo > upperBound: print("out of range") elif guessedNo < actualNo: print("too small, try again...") else: print("too big, try again...") except Exception as e: print("that is not a number...try again".format(e)) return "You got it!" pass
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 MY F*****G guessing game!") lBound = not_number_rejector("Enter an lower bound: ") uBound = not_number_rejector("Enter an upper bound: ") print("OK then, a number between {} and {} ?".format(lBound, uBound)) while uBound <= lBound: print("ERROR 1: Both Lower and Upper bounds are equal, change bounds") uBound = not_number_rejector("Enter an upper bound: ") print("OK then, a number between {} and {} ?".format(lBound, uBound)) lBound = int(lBound) uBound = int(uBound) actualNumber = random.randint(lBound, uBound) guessed = False while not guessed: try: guessedNumber = int(raw_input("guess a number: ")) print("you guessed {},".format(guessedNumber), ) if guessedNumber == actualNumber: print("you got it!! It was {}".format(actualNumber)) guessed = True elif guessedNumber < lBound: print("YOU'RE OTUSIDE THE F*****G PARAMETERSBDASDA ") elif guessedNumber > uBound: print("YOU'RE OTUSIDE THE F*****G PARAMETERSBDASDA ") elif guessedNumber < actualNumber: print("too small, try again ") else: print("too big, try again ") except Exception as e: print("that is not a numebr".format(e), ) return "You got it!"
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 _ ?") # Ask for lower and upper bound and check if they are valid lowerBound = not_number_rejector("Enter an lower bound: ") while True: upperBound = not_number_rejector("Enter an upper bound: ") if upperBound > lowerBound + 1: print("OK then, we are guessing a number between ", "{} and {}".format(lowerBound, upperBound)) break else: print("Invalid bounds. Make sure upper bound is greater than ", "lower bound by 2") # Set actual number to be guessed actualNumber = random.randint(lowerBound, upperBound) guessed = False # Guessing game time! while not guessed: # check if number is within bounds guessedNumber = super_asker(lowerBound, upperBound) if 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!"
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!"
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("Welcome to my guessing game") print("Let's pick both od the bounds") lowerBound = not_number_rejector("Enter a lower bound:") print("Ok, so a number between {} and _?".format(lowerBound)) guess_input = False while not guess_input: upperBound = not_number_rejector("Enter an upper bound:") if upperBound > lowerBound + 1: print("Ok, so a number between {} and {}?".format( lowerBound, upperBound)) guess_input = True else: print("Number too small!") actualNumber = random.randint(lowerBound, upperBound) guessed = False while not guessed: guessedNumber = super_asker(lowerBound, upperBound) print("you guessed {},".format(guessedNumber)) if 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!"
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("\nLet\'s set the bounds of the game first:") print("What will be the lower bound?") lowerBound = not_number_rejector(0) print("What will be the upper bound?") upperBound = 1 while lowerBound >= upperBound: upperBound = not_number_rejector(0) if lowerBound >= upperBound: print("The upper bound can\'t be less than or equal to the lower") else: print("\nOK then, a number between {} and {}".format( lowerBound, upperBound)) actualNumber = random.randint(lowerBound, upperBound) guessed = False print("\nTime to guess!") while not guessed: guessedNumber = not_number_rejector(0) print("You guessed {},".format(guessedNumber), ) if guessedNumber == actualNumber: print("You got it!! It was {}".format(actualNumber)) guessed = True elif guessedNumber < lowerBound or guessedNumber > upperBound: print("c'mon, that's outside the bounds, try again") elif guessedNumber < actualNumber: print("Too small, try again ") else: print("Too big, try again ") return "You got it!"
def test_not_number_rejector(): """Test the not number rejector function.""" try: import exercise1 except Exception as e: return syntax_error_message(e) mockInputs = ["aword", [1, 2, 3], {"an": "object"}, 40] try: with mock.patch('__builtin__.raw_input', side_effect=mockInputs): return exercise1.not_number_rejector() except Exception as e: print("exception:", e)
def advancedGuessingGame(): print("\nwelcome to the guessing game!") lowerBound = not_number_rejector("Enter a lower bound: ") upperBound = not_number_rejector("Enter an upper bound: ") while lowerBound >= upperBound: print("{} is too high".format(lowerBound)) upperBound = not_number_rejector("Enter an upper bound: ") print("OK then, a number between {} and {} ?".format( lowerBound, upperBound)) lowerBound = int(lowerBound) upperBound = int(upperBound) actualNumber = random.randint(lowerBound, upperBound) guessed = False while not guessed: try: guessedNumber = int(input("Guess a NUmber: ")) print("{} is the number".format(guessedNumber)) except Exception as e: print("{} is not a number, please enter a number only".format(e)) continue if guessedNumber < lowerBound or guessedNumber > upperBound: print("{} is outside the bound".format(guessedNumber)) else: print("you guessed {},".format(guessedNumber), ) if 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!"
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("Goood day,let's play") print("Enter a lowerBound:") lowerBound = not_number_rejector("your lower bound is:") upperBound = not_number_rejector("What is your upper bound?:") while upperBound <= lowerBound: print("re-enter upperBound") pass upperBound = int(upperBound) lowerBound = int(lowerBound) actualNumber = random.randint(lowerBound, upperBound) guess = False while not guess: try: input_number = int(raw_input("Guess Number:")) print("{} is valid".format(input_number)) if input_number == actualNumber: print("{} is correct".format(input_number)) guess = True elif input_number < actualNumber: print("too small. try again") elif input_number > upperBound: print("out of range") elif input_number < lowerBound: print("out of range") else: print("too big, try again") except Exception as e: print("Try again because it's not a number ({})".format(e)) return "You got it!" pass actualNumber = random.randint(upperBound, lowerBound) guess = False while not guess: try: input_number = int(raw_input("Guess Number:")) print("{} is valid".format(input_number)) if input_number == actualNumber: print("{} is correct".format(input_number)) guess = True elif input_number < actualNumber: print("too small. try again") elif input_number > lowerBound: print("out of range") elif input_number < upperBound: print("out of range") else: print("too big, try again") except Exception as e: print("Try again because it's not a number ({})".format(e))
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("Welcome to my guessing game") print("Let's pick both of the bounds") check = False lowerBound = not_number_rejector("Enter the lower bound: ") while check is False: try: upperBound = int(raw_input("Enter the upper bound: ")) if upperBound > (lowerBound + 1): print("Ok you need to guess " + "between {} and {}".format(lowerBound, upperBound)) check = True elif upperBound == (lowerBound + 1): print("Numbers too close together") else: print("{} isn't higher than {}, try again".format( upperBound, lowerBound)) except: print("Not an Integer") continue actualNumber = random.randint(lowerBound, upperBound) guessed = False while not guessed: try: guessedNumber = int(raw_input("Have a guess: ")) if guessedNumber == actualNumber: print("You win, {} was the answer!".format(actualNumber)) guessed = True elif guessedNumber <= lowerBound: print("No, {} is too low to be valid!".format(guessedNumber)) elif guessedNumber >= upperBound: print("No, {} is too high to be valid!".format(guessedNumber)) elif guessedNumber < actualNumber: print("Guess higher!") elif guessedNumber > actualNumber: print("Guess lower!") except: print("Not an Integer") continue return "You got it!"