Random Number Guessing Game Code Efficiency - Python UPDATE: I have added:

a play-again option
score keeping thing
difficulty levels

**(but I still need help with the functions part).
I don't feel it's necessary to explain what my code does, as it is pretty self-explanatory. I have the entire program completed, but I need to put some of the components of the program into functions. I don't know how to do this. I also need to add a play again option (I have done this before, but for some reason this particular code isn't working...).
import random

#variables:
score = 0
play = True

while play:
    while True:
        try:
            difficulty = int(input(&quot;Give me a number between 1 and 3 (these are the difficulty levels) \n\n 1 = easy \n 2 = medium \n 3 = hard \n\n&quot;))
        except:
            print(&quot;This is not a number!&quot;)
            continue

        if difficulty &gt; 3 or difficulty &lt; 1:
            print(&quot;That's an invalid input, please re-enter your number&quot;)
            continue
        break

    number = random.randint(1, 10 * difficulty)
    attempts = 0
    guess = 0

    while guess != number and attempts &lt; 3:
        attempts += 1
        print(&quot;attempt number &quot; + str(attempts))

        while True:
            try:
                guess = int(input(&quot;Guess what number I am thinking of  &quot;))
            except:
                print(&quot;That's not a number!&quot;)
                continue

            if guess &gt; 10 * difficulty or guess &lt; 1:
                print(&quot;That's an invalid input, please re-enter your number.&quot;)
                continue
            break

        if guess == number:
            print(&quot;You guessed it correct!&quot;)
            score += 1
        elif abs(guess - number) == 1:
            print(&quot;You're hot on the trail!&quot;)
        elif abs(guess - number) &lt;= 3:
            print(&quot;You're getting warmer...&quot;)
        else:
            print(&quot;You're freezing cold!!&quot;)

    if attempts == 3 and guess != number:
        print(&quot;Oops! You've run out of guesses!&quot;)
        print(&quot;The number I was thinking of was &quot; + str(number) + &quot;.&quot;)

    print(&quot;Your final score is &quot; + str(score))

    text = input(&quot;If you want to play again, type 'yes' &quot;)
    if text != &quot;yes&quot;:
        play = False
        print(&quot;Ok, thank you for playing!&quot;)
I'm not exactly sure what you're asking but with my best guess, I'll give it a try.
What you should be turning into functions (in my opinon)

&quot;bad&quot; inputs (such as 'a', 'b' etc. or '%', '*' etc.); error trapping algorithm chunk
of code into a function.

&quot;You're hot on the trail&quot; etc.) block of code can also be converted
to a function.


My biggest concern with your program is the following:
if text != &quot;yes&quot;:  then  play = False - Why just &quot;yes&quot;?? Why not &quot;yes&quot; and &quot;Yes&quot;?
What if the user inputs &quot;Yes&quot; - with a capital &quot;Y&quot; instead of &quot;yes&quot; - with a lower-case &quot;y&quot;? (I tried this by the way, and this was the output: &quot;Ok, thank you for playing!&quot;). For them (the user) they're indicating that they want to have another try at the game, but due to your code's incompetence for a variety of options (like &quot;Yes&quot; or &quot;yes&quot; - for replaying, &quot;No&quot; or &quot;no&quot; - for NOT replaying). Please solve this problem.
Now to your more imperative problem (the functions), I can give it a shot.
Note: You should aim for maximum clarity if you want your code to be re-used by others, or to make it easier to read and understand (not just for others but for you as well!).
if guess &gt; 10 * difficulty or guess &lt; 1:
                print(&quot;That's an invalid input, please re-enter your number.&quot;)
                # ⬆ this particular line!! ⬆
                continue
            break

You didn't specify WHY the input is invalid, you stated the obvious by saying &quot;It's an invalid input&quot;...be concise and again, aim for MAXIMUM clarity.
Note: I did abbreviate some of the variable/function names just for saving time.
Anyway, here's how I would've done this:
import random

#variables:
sc = 0
pl = True

# &quot;bad&quot; input loop function.
def get_num(diff): 

    while True:
        try:
            gs = int(input(&quot;Guess what number I am thinking of  &quot;))
        except:
            print(&quot;Please enter a NUMBER. Letters (a, b...) and symbols (#, %...) are not appropriate vales.&quot;)
            continue

        if gs &gt; 10 * diff or gs &lt; 1:
            print(&quot;Your number is not in the right range, please re-enter your number!&quot;)
            continue
        return gs

# &quot;hot and cold&quot; function
def temp_func(gs, num):

    if gs == num:
        print(&quot;CORRECT!&quot;)
        return True
    elif abs(gs - num) == 1:
        print(&quot;HOT!&quot;)
        return False
    elif abs(gs - num) &lt;= 3:
        print(&quot;WARM&quot;)
        return False
    else:
        print(&quot;COLD!&quot;)
        return False

while pl:
    while True:
        try:
            diff = int(input(&quot;Give me a number between 1 and 3 &quot;
                                   &quot;(these are the difficulty levels) \n\n 1 = easy \n 2 = medium \n 3 = hard \n\n&quot;))
        except:
            print(&quot;This is not a number!&quot;)
            continue

        if diff &gt; 3 or diff &lt; 1:
            print(&quot;That's an invalid input, please re-enter your number&quot;)
            continue
        break

    num = random.randint(1, 10 * diff)

    attmp = 0
    gs = 0

    while gs != num and attmp &lt; 3:
        attmp += 1
        print(&quot;attempt number &quot; + str(attmp))

        gs = get_num(diff)

        did_win = temp_func(gs, num)
        if did_win:
            sc += 1
            break

    if attmp == 3 and gs != num:
        print(&quot;Oops! You've run out of guesses!&quot;)
        print(&quot;The number I was thinking of was &quot; + str(num) + &quot;.&quot;)

    print(&quot;Your final score is &quot; + str(sc))

    txt = input(&quot;If you want to play again, type 'yes' &quot;)
    if txt != &quot;yes&quot;:
        pl = False
        print(&quot;TYPE GOODBYE MESSAGE HERE&quot;)