Last active
October 16, 2021 03:31
-
-
Save msyvr/d8a51305c9d571a056955b95cc6a5a41 to your computer and use it in GitHub Desktop.
bot randomly guesses the player's number
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
# resource: https://youtu.be/8ext9G7xspg?t=431 | |
import random | |
min = int(input("What's the minimum integer for the number guessing game?: ")) | |
max = int(input("What's the maximum integer for the number guessing game?: ")) | |
# choose which game to play by calling it (end of this file - scroll down) | |
# player guesses which number the computer randomly chose from a range | |
def guess(min, max): | |
random_number = random.randint(min,max) | |
guess = random_number + 1 | |
while guess != random_number: | |
guess = int(input(f"Guess a number between {min} and {max}: ")) | |
print(f"Great! Your guess is {guess}.") | |
if guess == random_number: | |
print(f"Awesome guess - the random number was {random_number}") | |
exit | |
elif guess > random_number: | |
print(f"No cigar - your guess is too high. Try again!") | |
elif guess < random_number: | |
print(f"No cigar - your guess is too low. Try again!") | |
#else | |
#error | |
# computer guesses which number the player randomly chose from a range, with auto-update of range after wrong guesses to zoom in on right guess | |
def cguess(min, max): | |
low = min | |
high = max | |
player_input = int(input(f"Think of a number between {min} and {max}... enter it here (don't worry, the computer won't peek!): ")) | |
cguess = player_input + random.randint(min-player_input+1, max-player_input-1) | |
while cguess != player_input: | |
cguess=random.randint(min, max) | |
print(f"The computer's guess is {cguess}.") | |
if cguess == player_input: | |
print(f"Lucky computer - your number was {player_input}") | |
exit | |
elif cguess > player_input: | |
print(f"No cigar - the computer's guess is too high. It's going to try again!") | |
max = cguess - 1 | |
elif cguess < player_input: | |
print(f"No cigar - the computer's guess is too low. It's going to try again!") | |
min = cguess + 1 | |
# computer guesses which number the player randomly chose from a range, with auto-update of range based on player feedback, to zoom in on right guess | |
def guess_game(min, max): | |
# computer guesses, player gives feedback (too high, too low), guess range updated accordingly, computer guesses again, ... | |
low = min | |
high = max | |
feedback = '' | |
cguess = 0 | |
while feedback not in {'correct', 'c'}: | |
if low != high: | |
cguess=random.randint(low, high) | |
else: | |
print("Only one choice left!...") | |
cguess=low #could be high, they're equal here | |
print(f"The computer guessed {cguess} and there aren't any more choices left in the range - so your number must be {cguess}!") | |
break | |
feedback = input(f"Is {cguess} correct (c), too high (h), or too low (l)?: ").lower() | |
if feedback in {'too high', 'h'}: | |
high = cguess - 1 | |
elif feedback in {'too low', 'l'}: | |
low = cguess + 1 | |
print(f"Computer guessed right! Your number was {cguess}.") | |
# choose which of the above games to play by calling it: either | |
# guess(min, max) or cguess(min, max) or guess_game(min, max) | |
guess_game(min,max) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment