# Solution to Number Guessing Game using Random package in Python

**URL:** https://forum.codewithmosh.com/t/solution-to-number-guessing-game-using-random-package-in-python/28590
**Category:** Uncategorized
**Created:** [September 7, 2024, 3:16am UTC](https://forum.codewithmosh.com/t/solution-to-number-guessing-game-using-random-package-in-python/28590 "2024-09-07T03:16:25Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Gourav97](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/gourav97/32/8436_2.png) [@Gourav97](https://forum.codewithmosh.com/u/Gourav97)
#### Post date: [September 7, 2024, 3:16am UTC](https://forum.codewithmosh.com/t/solution-to-number-guessing-game-using-random-package-in-python/28590/1 "2024-09-07T03:16:25Z")

</div>

Here is the solution to Number Guessing Game using Random package in Python which is given below:

import random

print(“Welcome to the number guessing game!”)  
randrange = int(input("Enter a range: "))  
numbertoguess = random.randrange(randrange)

chances = int(input("Enter number of chances: "))

guesscounter = 0

while guesscounter \< chances:

```
guesscounter += 1
myguess = int(input('Please Enter your Guess : '))

if myguess == numbertoguess:
    print(f'The number is {numbertoguess} and you found it right !! in the {guesscounter} attempt')
    break

elif guesscounter >= chances and myguess != numbertoguess:
    print(f'Oops sorry, The number is {numbertoguess} better luck next time')

elif myguess > numbertoguess:
    print('Your guess is higher ')

elif myguess < numbertoguess:
    print('Your guess is lesser')

```
