Number guess game program in python 3 I started learning Python just yesterday, and so I am aware of my lack of knowledge regarding the language. Below is a program I wrote to simulate a guessing game. I ended up creating a lot of ad hoc patches for some possible errors, and suspect that there are more elegant ways to write the program. I'd like pointers/assistance/an example of a "best practices" version of the program I write below:

import random, sys

sys.setrecursionlimit(10000)    #I expect any troll who's trying to fuck with the program to give up before they reach 10,000.
control = True    #global variable to control the behaviour of the 'guessNumber()' function.
errors = 0    #To store the number of errors a user triggers.

def guessNumber(maxx):
    global errors
    global control
    try:
        maxx = float(maxx)    #Incase a cheeky user decided to submit floating point numbers.
        if int(maxx) != maxx:     #If they submitted a floating point number instead of an integer.
            print("Input whole numbers only.")
            errors += 1
            guessNumber(input('Input the Maximum number: \t'))  #Call a new instance of 'guessNumber()'
        else:   #If they indeed submitted a whole number.
            maxx = int(maxx)
    except ValueError:  #They didn't submit a whole number.
        print("Input whole numbers only.")
        errors += 1
        guessNumber(input('Input the Maximum number: \t'))  #Call a new instance of 'guessNumber()'

    if not control:    #control variable. If the program has already properly evaluated, it should terminate.
        return None

    print('I am thinking of a whole number between 1 and ' + str(maxx) + ' (both inclusive).')        
    num = random.randint(1, maxx)
    count = 0
    check = True
    var = None

    while check:
        print("Guess the number.")
        try:
            var = float(input())    #Incase a cheeky user decided to submit floating point numbers.
            if int(var) != var:     #If they submitted a floating point number instead of an integer.
                print("Input whole numbers only.")
                errors += 1
                continue
            else:   #If they indeed submitted a whole number.
                var = int(var)
        except ValueError:  #They didn't submit a whole number.
            print("Input whole numbers only.")
            errors += 1
            continue
        count +=  1
        if var == num:  #If they guess the number right.
            check = False
        elif var &gt; num:
            print('Too high! \nTry a little lower.')
        elif var &lt; num:
            print('Too low! \nTry a little higher.')

    print('Congratulations!!!\nYou guessed the number after ' + str(count) + " tries and triggering " + str(errors) + " errors.")
    control = False     #After every completed execution, we set control to false so that if the function was called from another instance of itself it terminates and doesn't cause errors.
    return None

guessNumber(input('Input the Maximum number (integers only): \t'))


I am particularly worried about the memory cost, due to using recursion to deal with errors.
Your comments describe what the code is doing, not why the code's doing what it is. And so are generally unhelpful.
Please don't use profanity in comments, or anywhere. It makes you look unprofessional, and is not really that helpful.
To hell with sys.setrecursionlimit. It just delays the inevitable, rather than fixes your problem.
Rather than using global variables, you could instead use a closure.
Your recursion should be in a separate function. That only gets user input. But it'd be better if it were a while loop.

For example you could use:

def get_user_input(fn, prompt='', exception_message=''):
    while True:
        try:
            return fn(input(prompt))
        except ValueError:
            print(exception_message)

Don't repeat yourself. You write get_user_input out twice. In different ways.
The while loop for your higher or lower game seems pretty good. Nice job on that.


And so ignoring your error part I'd use:

import random


def get_user_input(fn, prompt='', exception_message=''):
    while True:
        try:
            return fn(input(prompt))
        except ValueError:
            print(exception_message)


def main():
    limit = get_user_input(int, prompt='Input the Maximum number: \t',
                           exception_message='Input whole numbers only.')


    print('I am thinking of a whole number between 1 and {limit} (both inclusive).'.format(limit=limit))
    number = random.randint(1, limit)
    count = 0
    while True:
        print("Guess the number.")
        guess = get_user_input(int, exception_message='Input whole numbers only.')
        count += 1
        if guess == number:
            break

        if guess &gt; number:
            print('Too high!\nTry a little lower.')
        else guess &lt; number:
            print('Too low!\nTry a little higher.')

    print('Congratulations!!!\nYou guessed the number after {count} tries.'.format(count=count))

if __name__ == '__main__':
    main()


If errors is super important to you, you could return the amount of errors from get_user_input and add it to a local errors variable.
Say:

errors = 0
while True:
    limit, input_errors = get_user_input(...)
    errors += input_errors