Created
March 10, 2023 17:41
-
-
Save R3DHULK/898adfa9979c05ee26072cef782435c0 to your computer and use it in GitHub Desktop.
Text Based Guess The Number Game In Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| import math | |
| # Taking Inputs | |
| lower = int(input("Enter Lower bound:- ")) | |
| # Taking Inputs | |
| upper = int(input("Enter Upper bound:- ")) | |
| # generating random number between | |
| # the lower and upper | |
| x = random.randint(lower, upper) | |
| print("\n\tYou've only ", | |
| round(math.log(upper - lower + 1, 2)), | |
| " chances to guess the integer!\n") | |
| # Initializing the number of guesses. | |
| count = 0 | |
| # for calculation of minimum number of | |
| # guesses depends upon range | |
| while count < math.log(upper - lower + 1, 2): | |
| count += 1 | |
| # taking guessing number as input | |
| guess = int(input("Guess a number:- ")) | |
| # Condition testing | |
| if x == guess: | |
| print("Congratulations you did it in ", | |
| count, " try") | |
| # Once guessed, loop will break | |
| break | |
| elif x > guess: | |
| print("You guessed too small!") | |
| elif x < guess: | |
| print("You Guessed too high!") | |
| # If Guessing is more than required guesses, | |
| # shows this output. | |
| if count >= math.log(upper - lower + 1, 2): | |
| print("\nThe number is %d" % x) | |
| print("\tBetter Luck Next time!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment