<jupyter_start><jupyter_text>Guessing Game This session is going to help us improve our Python programming skills using:*   Functions*   Flow Control*   User Inputs*   Error Handling*   Flexible Printing*   Randomized Responses*   If-Then and Else-If Statements  Game Details:This game is very simple... *   The program will randomly pick a number between some range (we'll ask the player to pick the range). *   The player will have a certain number of attempts to guess that number. *   The program will let the player know if their guess was either too high or too low after each guess. *   Game Over. Player either wins or losses.  Steps to Build Game1.   Create function the gets a guess from the player2.   Create responses to guess3.   Map out the game flow4.   Wrap it all into one function to play the game---  Step 1 - Ask Player For Their Guess Whenever you are dealing with a user input you need to make sure that they enter what you actually want them to enter...Python handles numbers and characters differently and we need to make sure that we get a numeric response. We also need to make sure the guess is an integer. We don't want people guessing random decimal numbers.  Key Concept: User Input<jupyter_code># ask player for a number between a range 
start = 1
end = 100
guess = input("Guess a number between {0} and {1}: ".format(start, end))

# view guess
guess<jupyter_output><empty_output><jupyter_text>Potential Problem 1: What if they input something besides an integer?Let's force the input to be an integer and then use built in error handling python procedures to save us.<jupyter_code># try: 
# except():
try:
    guess = int(input("Guess a number between {0} and {1}: ".format(start, end)))
except(ValueError, TypeError):
    print("That is not an integer! Try again!")<jupyter_output><empty_output><jupyter_text>Need to Loop it If you don't use a while function then it won't run again.<jupyter_code># while True: if you don't use 
# try: 
# except():
while True:
    try:
        guess = int(input("Guess a number between {0} and {1}: ".format(start, end)))
        break
    except(ValueError, TypeError):
        print("That is not an integer! Try again!")<jupyter_output><empty_output><jupyter_text>Potential Problem 2: What if they guess a number outside of our range?We need to do an if else statement checking that the guess is in the range and if it's not telling them to guess again<jupyter_code># while True:
# try: 
# except
# else:
#  if guess is in range break
#  else: tell them to guess again..."That isn't in the range. I should dock you 2 guesses for that mistake but since I'm so nice I won't count that against your guesses but please try to stay within the range when you guess.""
while True:
    try:
        guess = int(input("Guess a number between {0} and {1}: ".format(start, end)))
    except(ValueError, TypeError):
        print("That is not an integer! Try again!")
    else:
        if guess >= start and guess <= end:
            break
        else:
            print("That isn't in the range. I should dock you 2 guesses for that mistake but since I'm so nice I won't count that against your guesses but please try to stay within the range when you guess.")<jupyter_output><empty_output><jupyter_text>Wrap it into a function<jupyter_code># define function as valid_num
# the function should take in 2 inputs (start and end)
def valid_num(start = 1, end = 100):
    while True:
        try:
            guess = int(input("Guess a number between {0} and {1}: ".format(start, end)))
        except(ValueError, TypeError):
            print("That is not an integer! Try again!")
        else:
            if guess >= start and guess <= end:
                return guess
            else:
                print("That isn't in the range. I should dock you 2 guesses for that mistake but since I'm so nice I won't count that against your guesses but please try to stay within the range when you guess.")


# test function
valid_num()<jupyter_output><empty_output><jupyter_text>For more information on error handling visit: https://docs.python.org/3/library/exceptions.htmlhttps://docs.python.org/3/tutorial/errors.html  Step 2 - Create Responses to GuessesWe don't want our game to be too repetitive for the player. We need to come up with a bunch of responses for each guess.The player's guess will either be too high or too low. ---Create 2 lists1.   Responses for guesses that are too high2.   Responses for guesses that are too low<jupyter_code># here are my reponses if the guess is too high. Add in a few more of your own..
highSayings = ["Who do you think you are? <NAME>? Cuz you are TOO HIGH!",
               "You need to come down from that ledge cuz you're TOO HIGH!",
               "Way too high!",
               "Tough break. Too high.",
               "Are you related to Snoop Dog? Cuz you are TOO HIGH.",
               'Dang...Too high...',
               "Tough break. Too high.",
               "So close but yet so far! You're too high.",
               "Think carefully before you guess again! You are too high.",
               "Too high. You might want to give up, I don't think your pride can handle losing to a computer.",
               "Nope! Too high."]

# here are my reponses if the guess is too low. Add in a few more of your own..
lowSayings = ["You might need a ladder because you are TOO LOW!",
              "You seem down. Maybe you need to guess higher.",
              "Shawty you are too low low low low...",
              'Dang...Too low...',
              "Tough break. Too low.",
              "So close but yet so far! You're too low.",
              "Think carefully before you guess again! You are too low.",
              "Too low. You might want to give up, I don't think your pride can handle losing to a computer.",
              "Nope! Too low.",
              "You are not smart. You're too low."]<jupyter_output><empty_output><jupyter_text>Step 3 - Game Flow<jupyter_code># intro statement to the game
# if you use """it will keep the format of your string"""
print("""
Want to play a little game?

I'll think of a number and you have 7 guess to figure it out...

Good Luck!
""")
# create the max number of guesses variable and call it "tries"
print("""
Want to play a little game?

I'll think of a number and you have 7 guess to figure it out...

Good Luck!
""")
tries = 7
# Ask player for starting number in range of numbers to guess between
# Follow similar format as our valid number function
print("""
Want to play a little game?

I'll think of a number and you have 7 guess to figure it out...

Good Luck!
""")

tries = 7

while True:
    try:
        start = int(input("What should the lower bound of our range be? "))
        break
    except(ValueError, TypeError):
        print("That is not an integer! Try again!")
        

# Ask player for ending number in range of numbers to guess between
# Follow similar format as our valid number function
print("""
Want to play a little game?

I'll think of a number and you have 7 guess to figure it out...

Good Luck!
""")

tries = 7

while True:
    try:
        start = int(input("What should the lower bound of our range be? "))
        break
    except(ValueError, TypeError):
        print("That is not an integer! Try again!")
        
while True:
    try:
        end = int(input("What should the lower bound of our range be? "))
        break
    except(ValueError, TypeError):
        print("That is not an integer! Try again!")
        
        

import random

# have the program randomly pick a number (randint)
print("""
Want to play a little game?

I'll think of a number and you have 7 guess to figure it out...

Good Luck!
""")

tries = 7

while True:
    try:
        start = int(input("What should the lower bound of our range be? "))
        break
    except(ValueError, TypeError):
        print("That is not an integer! Try again!")
        
while True:
    try:
        end = int(input("What should the higher bound of our range be? "))
        break
    except(ValueError, TypeError):
        print("That is not an integer! Try again!")
        
number = random.randint(start, end)

# Initiate counter for the number of guesses
print("""
Want to play a little game?

I'll think of a number and you have 7 guess to figure it out...

Good Luck!
""")

tries = 7

while True:
    try:
        start = int(input("What should the lower bound of our range be? "))
        break
    except(ValueError, TypeError):
        print("That is not an integer! Try again!")
        
while True:
    try:
        end = int(input("What should the higher bound of our range be? "))
        break
    except(ValueError, TypeError):
        print("That is not an integer! Try again!")
        
number = random.randint(start, end)
counter = 0<jupyter_output><empty_output><jupyter_text>Game FlowThe game only ends if:1.   Guess correctly on the first try2.   Guess correctly3.   Run out of guessesThe game will loop through the player's guesses until one of those 3 outcomes occurs.  Guess FlowThe flow when a guess is made:1.   Guess is correct2.   Guess is too high3.   Guess is too low<jupyter_code># create while loop 
while counter < tries:
    # ask player for a guess
    guess = valid_num(start, end)
    # increase the counter by 1
    counter += 1
    # determine how many guesses are left "guesses_left"
    guesses_left = tries - counter
    # if the player guesses it correct on the first try, let's give them a special congrats! 
    if guess == number and counter == 1:
        print("WINNER WINNER CHICKEN DINNER!! You got it on the first try! Great Job!")
        # break out of loop to end the game
        break
    # else if in python = elif
    # else if the player guesses it correctly
    elif guess == number:
        # tell them they won and tell them how many guesses it took
        print("Great Job! You guessed correct! It took you {} tries to get it right!".format(counter))
        # game over
        break
    # else if the player runs out of guesses
    elif guesses_left == 0:
        # tell them they lost and what the actual number was.
        print("Tough break kiddo... maybe next time! The number I was thinking about was {}!".format(number))
        # game over
        break
    # else if the guess is too high
    elif guess > number:
        # generate a random response
        response = random.choice(highSayings)
        # we need to tell the player how many chances they have left.
        print("{0} You have {1} guesses left. Use them wisely...".format(response, guesses_left))
    # else the guess is too low
    else:
        # generate a random response
        response = random.choice(lowSayings)
        # we need to tell the player how many chances they have left.
        print("{0} You have {1} guesses left. Use them wisely...".format(response, guesses_left))<jupyter_output><empty_output><jupyter_text>Step 4 - Putting it all together<jupyter_code># define a function to play the game. Name it "guessingGame"
def guessingGame():
    print("""
Want to play a little game?

I'll think of a number and you have 7 guess to figure it out...

Good Luck!
""")

    tries = 7
    
    while True:
        try:
            start = int(input("What should the lower bound of our range be? "))
            break
        except(ValueError, TypeError):
            print("That is not an integer! Try again!")
            
    while True:
        try:
            end = int(input("What should the higher bound of our range be? "))
            break
        except(ValueError, TypeError):
            print("That is not an integer! Try again!")
            
    number = random.randint(start, end)
    counter = 0
    # create while loop 
    while counter < tries:
        # ask player for a guess
        guess = valid_num(start, end)
        # increase the counter by 1
        counter += 1
        # determine how many guesses are left "guesses_left"
        guesses_left = tries - counter
        # if the player guesses it correct on the first try, let's give them a special congrats! 
        if guess == number and counter == 1:
            print("WINNER WINNER CHICKEN DINNER!! You got it on the first try! Great Job!")
            # break out of loop to end the game
            break
        # else if in python = elif
        # else if the player guesses it correctly
        elif guess == number:
            # tell them they won and tell them how many guesses it took
            print("Great Job! You guessed correct! It took you {} tries to get it right!".format(counter))
            # game over
            break
        # else if the player runs out of guesses
        elif guesses_left == 0:
            # tell them they lost and what the actual number was.
            print("Tough break kiddo... maybe next time! The number I was thinking about was {}!".format(number))
            # game over
            break
        # else if the guess is too high
        elif guess > number:
            # generate a random response
            response = random.choice(highSayings)
            # we need to tell the player how many chances they have left.
            print("{0} You have {1} guesses left. Use them wisely...".format(response, guesses_left))
        # else the guess is too low
        else:
            # generate a random response
            response = random.choice(lowSayings)
            # we need to tell the player how many chances they have left.
            print("{0} You have {1} guesses left. Use them wisely...".format(response, guesses_left))<jupyter_output><empty_output><jupyter_text>Play the Game!<jupyter_code># Play the game
guessingGame()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08033503277494537, 'fraction_numerical': 0.005462490895848507, 'mean_word_length': 2.6011014948859166, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '545339', 'license': 'MIT', 'n_tokens_mistral': 3870, 'n_tokens_neox': 3752, 'n_words': 1853, 'path': 'Guessing_Game.ipynb', 'repo_name': 'papagorgio23/Python101', 'size': '13734'}