Py.Cafe

fomightez/

super-basic-number-guessing-game-in-all-ipywigets-plus-MARKDOWN

super basic number guessing game in all ipywidgets Plus MARKDOWN FOR FANCIER TEXT

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import ipywidgets as widgets
import markdown 
import random

# Generate a secret random number
secret = random.randint(1, 10)
guessed_correctly = False

# Create a slider for the user's guess
slider = widgets.IntSlider(
    value=0,
    max=10,
    description='Slide to Your Guess:',
    continuous_update=False,
    style={'description_width': 'initial'},
)

# Create an HTML widget to display the output with Markdown formatting
output = widgets.HTML(
    value=markdown.markdown("#### The secret number is a number between 1 and 10. Drag the slider to guess."),
    placeholder='Output will be displayed here',
    disabled=True,
    layout=widgets.Layout(width='500px', height='100px', border='none', font_weight='bold', color='black')
)

# Define a function to handle the slider value change
def on_value_change(change):
    global guessed_correctly
    if not guessed_correctly:
        if slider.value == secret:
            guessed_correctly = True
            output.value = markdown.markdown("## You got it!")
        else:
            output.value = markdown.markdown("#### Too small") if slider.value < secret else markdown.markdown("#### Too big")

# Create a VBox to hold the slider and output widgets
ui = widgets.VBox([slider, output])

# Bind the on_value_change function to the slider's value event
slider.observe(on_value_change, names='value')

# Assign the VBox to the page variable
page = ui