Skip to content

Instantly share code, notes, and snippets.

@vitalizzare
Last active May 25, 2021 22:35
Show Gist options
  • Select an option

  • Save vitalizzare/c926e2747b70494f9c6329f5dc252851 to your computer and use it in GitHub Desktop.

Select an option

Save vitalizzare/c926e2747b70494f9c6329f5dc252851 to your computer and use it in GitHub Desktop.
"Guess a Number" game using #Python, started by @angelwhocodes
logo = """
____ _ _ _ _
/ ___|_ _ ___ ___ ___ __ _ | \ | |_ _ _ __ ___ | |__ ___ _ __| |
| | _| | | |/ _ \/ __/ __| / _` | | \| | | | | '_ ` _ \| '_ \ / _ \ '__| |
| |_| | |_| | __/\__ \__ \ | (_| | | |\ | |_| | | | | | | |_) | __/ | |_|
\____|\__,_|\___||___/___/ \__,_| |_| \_|\__,_|_| |_| |_|_.__/ \___|_| (_)
"""
# https://twitter.com/angelwhocodes/status/1397032685286039552
# https://replit.com/@winetoxin/Guess-a-Number
from random import randint
def number_of_guess():
while True:
level = input("Choose the level ([e]asy, [h]ard): ").lower()
if level in ['e', 'h']:
break
return 10 if level == "e" else 5
def get_int(prompt):
while True:
try:
number = int(input(prompt))
except ValueError:
pass
else:
break
return number
def play_game(right_answer=None):
max_steps = number_of_guess()
if right_answer == None:
right_answer = randint(1, 100)
print('I picked a number in a range [1..100]')
print('Try to guss it in {} steps'.format(max_steps))
for step in range(1, max_steps + 1):
guess = get_int('Step {}: '.format(step))
if guess > right_answer:
print("Your number is higher than the right answer. Try again...")
elif guess < right_answer:
print("Your number is lower than the right answer. Try again...")
else:
print("You guessed it! So incredible!")
break
else:
print("Sorry you do not have a chance to guess again. Game over...")
print(f"The answer is {right_answer}")
if __name__ == '__main__':
try:
right_answer = int(input('Test answer: '))
assert 1 <= right_answer <= 100
print('Expected right answer is {}'.format(right_answer))
except Exception:
right_answer = None
print('Right answer will be choosen randomly.')
play_game(right_answer)
from guess_number import play_game
from art import logo
def approve(prompt=''):
while True:
answer = input(prompt + ' [y]es, [n]o: ')[0].lower()
if answer in ['y', 'n']:
break
return True if answer == 'y' else False
print(logo)
intro = "C R E A T E D B Y W I N E T O X I N"
border = "+" * len(intro)
intro = "\n".join([border, intro, border])
print(intro, '\n')
while approve("Wanna play?"):
play_game()
print("You exit the program. Thanks in advance!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment