how would I go about making this code cleaner? Right now it's a pain to look at and try to understand.from msvcrt import getwch, kbhitfrom os import systemfrom time import sleepfrom random import randintdef check_type(num, type):    # Check if int, if not return 0    if type == int:        try:            float(num)        except:            print( Error: Not number.)            print()            return 0        else:            # Check if has decimal            try:                int(num)            except:                print( Error: Number can't have a decimal.)                print()                return 0    # Check if float    elif type == float:        try:            float(num)        except:            print( Error: Not number.)            print()            return 0    # Check if string    elif type == str:        try:            str(num)        except:            print( Error: Not string.)            print()            return 0    return type(num)exit = 0while exit == 0:    print(Game settings)     Board     board = []    # Board size input    board_size = check_type(input( Board size: ), int)    if board_size == 0:        continue    aspect_ratio = 16 / 9    for num in range(board_size):        board.append([.] * round(board_size * (aspect_ratio)))     Variables     # Defaults x and y to middle of the board    x = round((board_size * (aspect_ratio)) / 2) - 1    y = round(board_size / 2)    # Defaults to moving right    directionx = 1    directiony = 0    key_press = 0    count = -1    apple_count = 0    x2 = []    y2 = []    applex = 0    appley = 0    apple_count_count = 0    # Speed input    while 1:        speed = check_type(input( Speed (lower = faster): ), float)        if speed == 0:            continue        else:            break    speed_increment = 0    x2.append(x)    y2.append(y)     Main     while board[y][x] != x:        # Loops board while no key presses        while kbhit() == 0:            x += directionx            y += directiony            # Border overflow check            if x >= round(board_size * (aspect_ratio)) and directionx == 1:                x = 0            elif x < 0 and directionx == -1:                x = round(board_size * (aspect_ratio)) - 1            if y >= board_size and directiony == 1:                y = 0            elif y < 0 and directiony == -1:                y = board_size - 1            # Checks to see if crash            if board[y][x] == x:                while 1:                    print()                    print(You have crashed!)                    # User input                    while 1:                        exit = check_type(input(Restart? (yes/no): ), str)                        if exit == 0:                            continue                        else:                            break                    if exit.lower() == yes:                        exit = 0                        break                    elif exit.lower() == no:                        exit = 1                        break                system(cls)                break            # If eat apple            elif board[y][x] == o:                apple_count += 1                apple_count_count = 0                # Updates x position            board[y][x] = X            x2.append(x)            y2.append(y)            # Makes body small x            board[y2[count + 1]][x2[count + 1]] = x            # Tail eater            if count - apple_count >= 0:                board[y2[count - apple_count]][x2[count - apple_count]] = .            count += 1            # Only allows one apple at a time            if apple_count_count <= 10:                # Makes sure apple doesnt spawn on anything                while apple_count_count == 10:                    applex = randint(0, round(board_size * (aspect_ratio)) - 1)                    appley = randint(0, board_size - 1)                    if board[appley][applex] != .:                        continue                    board[appley][applex] = o                    break                apple_count_count += 1            # Clears previous board then prints updated one            system(cls)            for row in board:                print( .join(row))            # Prints extra info            print(Speedup: +%d%%  Apple count: %d % (round(((speed - (speed - speed_increment)) / speed) * 100), apple_count))            # Delay the loop            sleep(round(speed - speed_increment, 2))            if speed - speed_increment >= speed / 2:                speed_increment += speed / 1000            key_press = 0        # Game logic        else:            key = getwch()            if key == w and directiony != 1 and key_press == 0:                directionx = 0                directiony = -1                key_press = 1            elif key == a and directionx != 1 and key_press == 0:                directionx = -1                directiony = 0                key_press = 1            elif key == s and directiony != -1 and key_press == 0:                directionx = 0                directiony = 1                key_press = 1            elif key == d and directionx != -1 and key_press == 0:                directionx = 1                directiony = 0                key_press = 1I was thinking about turning some while loops into functions but I'm not sure if that would make the game run slower. I'm planning on getting into website back-end development so knowing how to write code with good performance would be handy.Also, how should I go about commenting? Creating a newline at the end of every section I comment doesn't seem efficient at all. Is there some sort of guideline about commenting?Any help would be greatly appreciated!
Snake game made with Python
python;game;snake game
FunctionsYou can greatly improve your use of functions. For example, you could wrap the game logic into a function called game_logic() and call that. The same goes for your 'main' loop, which can be wrapped in game_loop.Improving check_type()Using a plain except: clause is not a good idea. Especially for longer and more complicated projects, if something else raises an exception while the try / except block is being executed and no specific exception is required, it will also run. To avoid this, use except ValueError:.Returning 0 can confuse people because it looks similar to exit code (0). To avoid the confusion, return False.Simplifying game logicTo shorten the length of 'game logic' statements, you could put the second part in a boolean variable:conditional = not directory and not key_pressif key in [w, a, s, d]:    # This is true for all keys in [w a s d]    key_press = Trueif key == w and conditional:    directionx, directiony = 0, -1    # Use multiple assignment to shorten codeelif key == a and conditional:    directionx, directiony = -1, 0elif key == s and conditional:    directionx, directiony = 0, 1elif key == d and conditional:    directionx, directiony = 1, 0Don't use os.system()If you need to clear the screen, you can do something like print(\n * y) where y is the vertical height of your terminal. You could also use a carriage return.  This might look a lot cleaner (depending on your implementation).Questionable codeHere's some things I thought were quite unusual:Why are you starting a print() statement with a 'space'? And why are you doing that inconsistently?Why are you using docstrings where they are not at all needed and especially in cases where you'd much prefer -no- documentation?You have some redundant comments, for example # Speed input to explain the use of a variable called speed_input. You can greatly simplify boolean logic, instead of using if x == 0 or if x == 1 (etc.), use if not x and if x. This makes the code easier to read.You are using 1 and 0 a lot for boolean logic, which I find quite confusing. Where possible, it would, for readability, be a better choice to use True and False: while True: etc.It is not needed to check exit for being a string. Any input will automatically be a string, and there's no need to convert it here.BugsIf there's an error converting board_size to integer, the board size will remain 0. Set a default size to avoid this.Trying to turn a float into an integer will not raise an exception, so using that to check for decimal numbers is useless.I have left out comments on the many overly complicated while, if and else blocks, the unnecessary variables, the shortage of whitespace and the fact that there is no main() function / guard. 