Skip to content

Instantly share code, notes, and snippets.

@fomightez
Created March 11, 2024 00:28
Show Gist options
  • Save fomightez/7b8cc447ead3b5cf6d79de4302eddb97 to your computer and use it in GitHub Desktop.
Save fomightez/7b8cc447ead3b5cf6d79de4302eddb97 to your computer and use it in GitHub Desktop.
Guessing game using ipywidgets to enter guesses and not legacy-from-consoles `input()`, related to https://discourse.jupyter.org/t/input-function-not-working/2290/13?u=fomightez
import ipywidgets as widgets
import random
max_secret = 10
secret = random.randint(1, max_secret)
guesses_allowed = 3
guesses_made = 0
game_still_going = True
# to leave out the quotes around strings sent to `ouptut.update()`, based on https://stackoverflow.com/a/70130979/8508004
class printer(str):
def __repr__(self):
return self
slider = widgets.IntSlider(
value=0,
min=0,
max= max_secret,
step=1,
description='Slide to Your Guess:',
disabled=False,
continuous_update=False,
orientation='horizontal',
style= {'description_width': 'initial'},
readout=True,
readout_format='d'
)
slider.default_value = slider.value # for resetting slider based on
# https://stackoverflow.com/a/63592983/8508004
def on_value_change(change):
global guesses_allowed
global guesses_made
global game_still_going
if game_still_going and (guesses_made < guesses_allowed - 1):
guesses_made += 1
if slider.value == secret:
game_still_going = False
output.update(printer(f'You win!\nYou guessed on your guess #{guesses_made} the number' + str(slider.value) + f". The secret is: {secret}!\nPress 'Play again' below to see if you are so lucky next time." ))
else:
output.update(printer(f"Guess #{guesses_made}:\nToo small" if slider.value < secret else f"Guess #{guesses_made}:\nToo big"))
elif game_still_going:
if slider.value == secret:
game_still_going = False
output.update(printer(f'You win!\nYou did it on the last of your {guesses_allowed} guesses, you guessed ' + str(slider.value) + f"! The secret was: {secret}" ))
else:
game_still_going = False
output.update(printer(f'Sorry. You lose!\nYou failed to guess the number in {guesses_allowed} guesses. Your last guess was ' + str(slider.value) + f". The secret was: {secret}\nPress 'Play again' below to try the guessing game again." ))
elif (guesses_made >= guesses_allowed - 1) and not game_still_going:
output.update(printer("THE GAME ENDED ALREADY.\nPress 'Play again' below to try the guessing game again." ))
initial_state_n_rules_text = f"The secret number is a number between 1 and {max_secret}.\nClick and drag the slider from zero to your guess and then release the mouse button to submit your guess.\nYou have three tries. You'll get a hint after each wrong guess."
output = display(printer(initial_state_n_rules_text), display_id=True)
display(slider)
reset_button = widgets.Button(
description='Play again',
tooltip='Start again',
style={'description_width': 'initial'}
)
def on_button_clicked(event):
global secret
global guesses_made
global game_still_going
secret = random.randint(1, max_secret)
slider.value = slider.default_value # reset slider based on
# https://stackoverflow.com/a/63592983/8508004
# had to put the resetting of `guesses_made` and `game_still_going` after
# resetting slider.value or got the note about ga,e having ended
guesses_made = 0
game_still_going = True
output.update(printer(initial_state_n_rules_text))
display(printer("\n\n\n"),reset_button) # The `"\n\n\n"` printed in front of the
# reset button is to have it below the game area a bit, lessening potential of
# hitting it mistakenly.
reset_button.on_click(on_button_clicked)
slider.observe(on_value_change, names='value')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment