Number guessing game, beginner project in Python! 
Tags: python, beginners, codenewbie, tutorial
Published: 2021-02-05 16:39:55

This is one of the simplest **Python** projects for beginners in Python and programming in general. We will be making a program that generates a number between two numbers and then the user has to guess the number generated. We will add some nice feautures like **choosing a difficulty** and **giving hints** to guess better. This project was actually my first Python project so I'm eager to teach you guys this, Let's get into it.

## Generating random numbers
To generate a random number everytime a new game is started we are going to `import random` and use the `randint()` function like this.

```
import random
number = random.randint(1, 100)
```
now if we use `print(number)` it will print a number between 1 and 100. Later we will change 1 and 100 with min and max and the bounds of the number will change according to the difficulty choosed. Shown in the next step.

## Choosing difficulty
To choose the difficulty of the game we will just make maximum number allowed bigger and also we will have an option for a Custom Game. Where the user chooses the minimum and maximum allowed numbers.
```
min = 1
while True:
    difficulty = input("Choose difficulty of guess (Easy/Medium/Hard/Impossible/Custom):")
    if difficulty == "easy" or difficulty == "Easy" or difficulty == "EASY":
        max = 10
        break
    elif difficulty == "medium" or difficulty == "Medium" or difficulty == "MEDIUM":
        max = 25
        break
    elif difficulty == "hard" or difficulty == "Hard" or difficulty == "HARD":
        max = 50
        break
    elif difficulty == "impossible" or difficulty == "Impossible" or difficulty == "IMPOSSIBLE":
        max = 200
        break
    elif difficulty == "custom" or difficulty == "Custom" or difficulty == "CUSTOM":
        min = int(input("What do you want the minimum number possible to be: "))
        max = int(input("What do you want the maximum number possible to be: "))
        break
    else: print("Wrong input, try again!")
```
So this piece of code will be looped until a correct input is passed(do while). So at this point we add the min and max to the `randint()` function. So the whole code looks like this :

```
min = 1
while True:
    difficulty = input("Choose difficulty of guess (Easy/Medium/Hard/Impossible/Custom):")
    if difficulty == "easy" or difficulty == "Easy" or difficulty == "EASY":
        max = 10
        break
    elif difficulty == "medium" or difficulty == "Medium" or difficulty == "MEDIUM":
        max = 25
        break
    elif difficulty == "hard" or difficulty == "Hard" or difficulty == "HARD":
        max = 50
        break
    elif difficulty == "impossible" or difficulty == "Impossible" or difficulty == "IMPOSSIBLE":
        max = 200
        break
    elif difficulty == "custom" or difficulty == "Custom" or difficulty == "CUSTOM":
        min = int(input("What do you want the minimum number possible to be: "))
        max = int(input("What do you want the maximum number possible to be: "))
        break
    else: print("Wrong input, try again!")

import random
number = random.randint(min, max)
```
You can test this by choosing different difficulties and printing the number to see if it's in the bounds given. 

## Checking the guess
Now we have to add the ability to take input for the guess and assign that to a variable to check if it's the same as the number generated. Like this :
```
while True:
    #shows the bounds of the number and asks for guess
    print("A number from ", min, " to ", max, " has been generated.")
    guess = input("Guess the number: ")
    #allows only ints to be entered
    if guess.isdecimal() :
        guess = int(guess)
    else:
        continue
    
    #checks if correct and if wrong gives tips
    if guess == number :
        print("You guessed correctly!")
        break
    else:
        if guess > max or guess < min:
            print("You are out of minimum and maximum bounds you dummy")
            continue
        elif guess > number:
            print("Guess Lower")
        elif guess < number:
            print("Guess Higher")
```

Also if the guess isn't correct we have a message pop out to help the user to guess higher or lower.

#Thanks for reading 
I hope this helps anyone starting out like it helped me. 
For any questions comment down bellow.
You can get the full code here: [GitHub](https://github.com/miljkovicjovan/number-guessing-game)

Thank you again, 
Stay safe,
~Jovan