|
| 1 | +# Guessing-Number-game |
| 2 | +Created simple mathematical game of guessing number using Python. |
| 3 | + |
| 4 | +## Installatiom |
| 5 | +You just require python IDE in your laptop/PC. |
| 6 | + |
| 7 | +## What is this game?? |
| 8 | +The game is straightforward. The user must select a random number between 1 to 100 from a number of choices. The system will attempt to guess the number with the minimum possible guesses. That's it. |
| 9 | + |
| 10 | +## Algorithm |
| 11 | +* Define the range of the numbers (lower and upper). It's 1-100 here. |
| 12 | +* Let user choose the number from the above range (1-100). |
| 13 | +* Set the guessed number initially to the middle of the range. |
| 14 | +* Start the game by displaying the user a message saying “Choose the number from X to Y”. You may update the message as you wish. |
| 15 | +* Initialize a variable guess_count to 0 to count the total number of chances that the computer has taken to guess the number correctly. |
| 16 | +* Write an infinite loop. |
| 17 | + - Increment the chances that the computer has taken to guess. |
| 18 | + - Show the guessed number to user and also give other option to get further guesses correct. |
| 19 | + - If the current guessed number is less than the users guessed number, then set lower range to guesssed number and get new guessed number. |
| 20 | + - If the current guessed number is greater than the users guessed number, then set lower range to guesssed number and get new guessed number. |
| 21 | + - If the current guessed number is equal to the users guessed number, break the loop and end the game. |
| 22 | + |
| 23 | +## Python code |
| 24 | +```python |
| 25 | +# Start code |
| 26 | +import time |
| 27 | + |
| 28 | +# Ask user to choose a number |
| 29 | +print("Please choose any integer between 1 and 100 and memorise it.") |
| 30 | +print("I'll (computer) take a guess at the number you've chosen; nevertheless, please assist me in answering the following questions.") |
| 31 | + |
| 32 | +time.sleep(2) |
| 33 | + |
| 34 | +print("Have you decided on a number?") |
| 35 | +print("Press Enter") |
| 36 | +input() |
| 37 | + |
| 38 | +print("Lets start the game!") |
| 39 | + |
| 40 | +# Set the guessed number to middle of range |
| 41 | +lower_range = 1 |
| 42 | +upper_range = 100 |
| 43 | +guessed_number = (lower_range + upper_range)//2 |
| 44 | + |
| 45 | +# Number of guesses required |
| 46 | +guess_count = 0 |
| 47 | + |
| 48 | +while True: |
| 49 | + guess_count += 1 |
| 50 | + print("Computer's guessed nummber is", guessed_number) |
| 51 | + print("Please Enter 1: If computer's guessed number is lower then what you have choosen!") |
| 52 | + print("Please Enter 2: If computer's guessed number is greater then what you have choosen!") |
| 53 | + print("Please Enter 3: If computer's guessed number is equal to what you have choosen!") |
| 54 | + |
| 55 | + user_response = int(input("Please Enter your response: ")) |
| 56 | + |
| 57 | + if user_response == 1: |
| 58 | + lower_range = guessed_number |
| 59 | + |
| 60 | + elif user_response == 2: |
| 61 | + upper_range = guessed_number |
| 62 | + |
| 63 | + elif user_response == 3: |
| 64 | + print("Oh! I found it in ", guess_count, "guesses") |
| 65 | + break |
| 66 | + |
| 67 | + else: |
| 68 | + print("Invali Number!, Please Enter valid number") |
| 69 | + |
| 70 | + guessed_number = (lower_range + upper_range)//2 # New guessed number |
| 71 | + |
| 72 | +``` |
| 73 | + |
| 74 | +## Conclusion |
| 75 | +The main objective of this project is to learn how to implement various algorithms in Python. This project is for absolute beginner. I have also attached .py file along with this. |
| 76 | + |
| 77 | +## Contribution |
| 78 | +Pull requests are welcome. For major changes, please open and issue first to discuss what you would like to change. |
| 79 | + |
| 80 | +Please make sure to update tests as appropriate. |
0 commit comments