Created
November 26, 2021 19:10
-
-
Save Elijah-trillionz/fcc30ee06799ee740e7ca844a26a4605 to your computer and use it in GitHub Desktop.
Guessing game for computers
This file contains 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
# guess number game for computers | |
import random as rd | |
# def guess_number(): | |
# user_input = int(input('Enter a number for computer to guess btw 1 and 10 (inclusive): ')) | |
# attempts = 2 | |
# | |
# while attempts >= 0: | |
# is_wrong = compare_guess(user_input, attempts) | |
# if is_wrong: | |
# print(is_wrong) | |
# else: | |
# print('Guessed right') | |
# break | |
# attempts -= 1 | |
# else: | |
# print('Computer failed') | |
# | |
# | |
# def compare_guess(user_input, attempts): | |
# if attempts < 2: | |
# print('trying again...') | |
# else: | |
# print('guessing...') | |
# | |
# # a minor delay, just to make the game interesting | |
# i = 0 | |
# while i < 30000000: | |
# i += 1 | |
# | |
# computer_input = rd.randint(1, 10) | |
# | |
# if computer_input == user_input: | |
# return False | |
# elif computer_input > user_input: | |
# return f'Computer guessed {computer_input}\nToo high. Computer has {attempts} more trials' | |
# else: | |
# return f'Computer guessed {computer_input}\nToo low. Computer has {attempts} more trials' | |
# | |
# | |
# guess_number() | |
# refactoring everything to make more sense and provide more features | |
# algorithm of the game | |
# 1. get the user's input, the number to guess | |
# 2. generate a random number for the computer to guess with | |
# 3. if computer's number is equal, jump out of recursion | |
# 4. if computer's number is too high, make the randint range between the initial/modified start and the guessed number | |
# 5. if computer's number is too low, make the randint range between the guessed number and the initial/modified end | |
user_input = int(input('Enter a number for computer to guess btw 1 and 10 (inclusive): ')) | |
def guess_number(start, end, attempts=2): | |
if attempts < 0: | |
return print('Game over bro') # comment this out to see how the algorithm works | |
elif attempts < 2: | |
print('trying again...') | |
else: | |
print('Guessing...') | |
# a minor delay, just to make the game transparent and interesting | |
i = 0 | |
while i < 30000000: | |
i += 1 | |
computer_input = rd.randint(start, end) | |
if user_input == computer_input: | |
return print(f'Guessed right {computer_input}') | |
elif user_input > computer_input: | |
start = computer_input + 1 # incrementing because computer input is of no use | |
print(f'Computer guessed {computer_input}\nToo low. Computer has {attempts} more trials') | |
guess_number(start, end, attempts - 1) | |
else: | |
end = computer_input - 1 # decrementing because computer input is of no use | |
print(f'Computer guessed {computer_input}\nToo high. Computer has {attempts} more trials') | |
guess_number(start, end, attempts - 1) | |
guess_number(1, 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment