Making a fancy number guessing game
Tags: beginners, python, tutorial
Published: 2022-04-17 00:59:52

**This article is tailored towards new and inexperienced coders that want to start their coding careers**

Demo of what we will be coding
![Guessing game test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ppj4ly5n9si3sc6bib2.png)
```
C:\Users\Josh\Desktop>guessing.py
Enter player name: JoshTheCodingAddict

Hi JoshTheCodingAddict Welcome to my fancy number guessing game
=====================
| enter 10---Easy   |
| enter 20---Medium |
| enter 30---Hard   |
=====================

[*] Select difficulty level: 10
[*] You have 3 number of guesses left!
[!] Guess the lucky number: 9
[-] Sorry please try again

[*] You have 2 number of guesses left!
[!] Guess the lucky number: 8
[-] Sorry please try again

[*] You have 1 number of guesses left!
[!] Guess the lucky number: 7
[-] Sorry please try again

[!] Game over, type c to retry and q to quit game: c
[*] You have 3 number of guesses left!
[!] Guess the lucky number: 7
[-] Sorry please try again

[*] You have 2 number of guesses left!
[!] Guess the lucky number: 6
[-] Sorry please try again

[*] You have 1 number of guesses left!
[!] Guess the lucky number: 5
[-] Sorry please try again

[!] Game over, type c to retry and q to quit game: 4
[-] Wrong input
[!] Game over, type c to retry and q to quit game: c
[*] You have 3 number of guesses left!
[!] Guess the lucky number: 3
[-] Sorry please try again

[*] You have 2 number of guesses left!
[!] Guess the lucky number: 2
[+] Congratulations!! you won the game

[!] Thanks for playing!
```

## Game requirements:

- Player name
- Game levels: Easy, Intermediate, Hard
- Number of guesses: how many wrong guesses before ending the game
- Fancy Instructions

Lets dive into it
## Game logic

- Users are presented a welcome message and asked to select the game difficulty
- Then game starts and player has n number of chances to guess the lucky number
- If the correct is number is inputed, player is presented with a congratulations message
- If player runs out of guesses then player is presented with a message telling them they failed to guess the correct number and should retry or exit the game.

Without further ado lets get our hands dirty and write some code

## Modules used:
`random` - used to generate pseudo random numbers

So we start by importing random
```
import random
```
Then setting our variables: player name, welcome message, congratulations message, failed message, difficulty level, lucky_number and number of guesses
```
player_name = input("Enter player name: ")
welcome_message = f"""
Hi {player_name} Welcome to my fancy number guessing game
=====================
| enter 10---Easy   |
| enter 20---Medium |
| enter 30---Hard   |
=====================
"""
print(welcome_message)
congratulations_message = "[+] Congratulations!! you won the game\n"
failed_message = "[-] Sorry please try again\n"
difficulty = int(input("[*] Select difficulty level: "))
lucky_number = random.randint(1, difficulty)
guesses = 3
```

Now start the game loop and write the logic for guessing and comparing guesses

```
while guesses >= 0:
    if guesses == 0:
        retry = input("[!] Game over, type c to retry and q to quit game: ")
        if retry == "c": guesses = 3
        elif retry == "q": break
        else: print("[-] Wrong input")
    else:  
        print(f"[*] You have {guesses} number of guesses left!")
        guessed = int(input("[!] Guess the lucky number: "))
        if guessed == lucky_number:
            print(congratulations_message)
            break
        else:
            print(failed_message)
            guesses = guesses - 1

print("[!] Thanks for playing!")
```

## Full code below

```
import random
player_name = input("Enter player name: ")
welcome_message = f"""
Hi {player_name} Welcome to my fancy number guessing game
=====================
| enter 10---Easy   |
| enter 20---Medium |
| enter 30---Hard   |
=====================
"""
print(welcome_message)
congratulations_message = "[+] Congratulations!! you won the game\n"
failed_message = "[-] Sorry please try again\n"
difficulty = int(input("[*] Select difficulty level: "))
lucky_number = random.randint(1, difficulty)
guesses = 3
while guesses >= 0:
    if guesses == 0:
        retry = input("[!] Game over, type c to retry and q to quit game: ")
        if retry == "c": guesses = 3
        elif retry == "q": break
        else: print("[-] Wrong input")
    else:  
        print(f"[*] You have {guesses} number of guesses left!")
        guessed = int(input("[!] Guess the lucky number: "))
        if guessed == lucky_number:
            print(congratulations_message)
            break
        else:
            print(failed_message)
            guesses = guesses - 1

print("[!] Thanks for playing!")

```

Feel free to improve on this and Happy Coding :)