Skip to content

Instantly share code, notes, and snippets.

@dentolos19
Last active August 7, 2023 15:01
Show Gist options
  • Save dentolos19/c33e74130ceae71b1635c11689843029 to your computer and use it in GitHub Desktop.
Save dentolos19/c33e74130ceae71b1635c11689843029 to your computer and use it in GitHub Desktop.
import os
import random
import time
from colorama import Fore # py -m pip install colorama
MINIMUM_VALUE = 1
MAXIMUM_VALUE = 100
def clear():
command = "clear"
if os.name in ("nt", "dos"):
command = "cls"
os.system(command)
def check_int(input):
try:
int(input)
return True
except ValueError:
return False
def main():
value = random.randint(MINIMUM_VALUE, MAXIMUM_VALUE)
tried_values = []
while True:
clear()
print("Guess The Number!")
print()
print("Legend:")
print(f"- {Fore.GREEN}Green{Fore.RESET} = Close (within 5 of the number)")
print(f"- {Fore.YELLOW}Yellow{Fore.RESET} = Getting Closer (within 15 of the number)")
print(f"- {Fore.RED}Red{Fore.RESET} = Far (more than 15 away from the number)")
print()
if len(tried_values) > 0:
print(f"Tries: {len(tried_values)}")
print("Values:", end="")
for tried_value in sorted(tried_values):
value_difference = abs(value - tried_value)
color = Fore.GREEN
if value_difference > 5:
color = Fore.YELLOW
if value_difference > 15:
color = Fore.RED
print(" ", end="")
if tried_value > value:
print("<", end="")
print(f"{color}{tried_value}{Fore.RESET}", end="")
if tried_value < value:
print(">", end="")
print(Fore.RESET)
print()
guess_value_string = input(">>> ")
print()
if not check_int(guess_value_string):
print("Invalid Input!")
continue
guess_value = int(guess_value_string)
tried_values.append(guess_value)
if guess_value == value:
print("You guessed it!")
value = random.randint(MINIMUM_VALUE, MAXIMUM_VALUE)
tried_values = []
elif guess_value < value:
print("Go higher!")
else:
print("Go lower!")
time.sleep(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment