Guess the number game (Python) I'm very new to coding and decided to try a mini-project in python to create a 'guess the number game'. I was just hoping for some feedback on my code-writing to prevent bad habits before they develop. This program runs smoothly but I would like to know if there are improvements I could make.
In particular:


How can I improve the efficiency and readability?
Is it okay to call a function within its own body?
Is there any ugly implementation that I should avoid?




import random

number = 0
counter = 0

def start_game():
    print("I'm thinking of a number between 1 and 10.")
    print("Can you guess it?")
    global number
    number = random.randint(1,10)
    global counter
    counter = 0
    check_valid()


def check_valid():
    global counter
    counter += 1
    guess = input()
    try:
        val = int(guess)
        if int(guess) not in range(0,11):
            print("Hmmm.. that number is not between 1 and 10! Try again!")
            check_valid()
        elif int(guess) &gt; number:
            print("Too high, try a smaller number")
            check_valid()
        elif int(guess) &lt; number:
            print("Too low, try a bigger number")
            check_valid()
        elif int(guess) == number:
            print("Congratulations, you guessed it! The number was " + 
                  str(number) + ".\nIt took you " + str(counter) + " tries!")
            print("Do you want to play again?")
            check_replay()
    except ValueError:
        print("That's not a number, try again!")
        check_valid()


def check_replay():
    answer = input()
    valid_yes = ["yes", "ye", "y"]
    valid_no = ["no", "n"]
    if answer.lower() in valid_yes:
        start_game()
    elif answer.lower() in valid_no:
        print("Thanks for playing!")
    else:
        print("Please enter yes or no")
        check_replay()


start_game()
One thing I notice is that, you use global variables. While they are not necessarily bad and work fine in this particular case, it might be beneficial to read these answers. It would be better to define these variables in main function and pass them as parameters for other functions.

def start_game(number, counter):
  #Your code here

def main():
  number = 0
  counter = 0
  start_game(number, counter)

if __name__ == "__main__":
    main()


Calling the function within its body is called a recursion and as long as you assure that the calling sequence won't become an infinite loop, it is widely used technique in programming.

One more minor thing: you define val = int(guess) but never use it, maybe delete it?

Other than that, everything looks fine and the code is readable.