# How to Build a Guessing Number Game Using Python

### **Through this project, you're going to learn about:**

```plaintext
1. The Random Module
2. Basic concepts of Python: type casting, conditional statements,
   iterative statements and string interpolation
3. Implementation of the guessing the number game with detailed explanation
```

## **Step 1: Modules Required:**

To help us build this game, we are going to use an amazing module of Python called **Random.** The random module generates random numbers for us. This gets in real handy as we do not want our computer to be biased.

## Step 2: Time to Code!

The first step is to import the random module:

```plaintext
import random
```

Now, we want the computer to choose a random number between 1 and 10. To set these limits, we make use of the randint() method of python.

**Syntax of randint() is given as:**

`randint(lower limit , upper limit)`

**For Our Case:**

```plaintext
number = random.randint(1,10)
```

Let's give the player 3 chances to guess the lucky number. To do that, we would require a loop that would repeat our game 3 times.

```plaintext
for i in range(0,3):
```

Inside our loop, we would like to ask the player for a number using the input() function. As python accepts string by default, we would convert the string into a number using function int(). The conversion of one data type into another is known as type casting or type conversion.

```plaintext
user = int(input("Guess the lucky number"))
```

Next, we would compare if the number guessed by the user equals the number generated by the computer. To compare, we use the 'if' statement and check for equality using the == operator.

```plaintext
if user == number:
```

If the number guessed is correct, we display "Hurray!!" using the print statement.

```plaintext
print("Hurray!!")
```

We would also like to display the lucky number. We would implement this concept using a mechanism called f-string or literal string interpolation.

Interpolation or interpolate means insert (something of a different nature) into something else. Here, we insert the value of the variables inside the statement of the string we are going to display.

```plaintext
#syntax for f-string or string interpolation
name = 'Sneha'
age = 20
print(f"Hello, My name is {name} and I'm {age} years old."

#output is generated as the value of variable replaced with contents of {}
Hello, My name is Sneha and I'm 20 years old.
```

In our game, we use interpolation as:

**Copy**

```plaintext
print(f"You guessed the number right, it's {number}")
```

Now, if the player has 3 unsuccessful attempts, the game gets over and the number is displayed on the screen as:

```plaintext
if user != number:
    print(f"Your guess is incorrect, the number is {number}"
```

The Final Source Code Is Displayed Below:

![Final Source Code for Guessing Game Using Python](https://cdn.hashnode.com/res/hashnode/image/upload/v1611258527222/-BwCgOnvM.png?auto=compress,format&format=webp align="left")

The Output if You're Lucky Indeed😍

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729956518925/f013d95f-aaa5-4557-a0c5-045a33963486.png align="center")

![Output Scenario 1 for Guessing Game Using Python](https://cdn.hashnode.com/res/hashnode/image/upload/v1611258853305/dcDxucNsk.png?auto=compress,format&format=webp align="left")

Well, Better Luck Next Time😢

![Output Scenario 2 for Guessing Game Using Python](https://cdn.hashnode.com/res/hashnode/image/upload/v1611258932421/ov1uahMvJ.png?auto=compress,format&format=webp align="left")

Hope you enjoyed my content. Happy Learning!!

For any feedback please mail on: (tripathisneha809@gmail.com)
