In [67]:
import random
import time
from IPython.display import Image, display, clear_output
from ipywidgets import widgets
x = ['lamb.jpg','lion.jpg','tiger.jpg','dog.jpg','ant.jpg','spider.jpg']
def redraw():
choices = random.sample(x, 4)
correct = random.choice(choices)
display(Image(correct))
time.sleep(3)
button1 = widgets.Button(description = choices[0])
button2 = widgets.Button(description = choices[1])
button3 = widgets.Button(description = choices[2])
button4 = widgets.Button(description = choices[3])
container = widgets.HBox(children=[button1,button2,button3,button4])
display(container)
def on_button1_clicked(b):
# [insert code to record choice]
container.close()
clear_output()
redraw()
def on_button2_clicked(b):
# [insert code to record choice]
container.close()
clear_output()
redraw()
def on_button3_clicked(b):
# [insert code to record choice]
container.close()
clear_output()
redraw()
def on_button4_clicked(b):
# [insert code to record choice]
container.close()
clear_output()
redraw()
button1.on_click(on_button1_clicked)
button2.on_click(on_button2_clicked)
button3.on_click(on_button3_clicked)
button4.on_click(on_button4_clicked)
redraw() # initializes th
In [14]:
import random
import time
from IPython.display import Image, display, clear_output
from ipywidgets import widgets
nchoices = 4
x = ['lion.jpg','tiger.jpg','lamb.jpg','dog.jpg','ant.jpg','spider.jpg']
def redraw():
choices = random.sample(x, nchoices)
correct = random.choice(choices)
display(Image(correct))
time.sleep(0.5)
buttons = [widgets.Button(description = choice) for choice in choices]
container = widgets.HBox(children=buttons)
display(container)
def on_button_clicked(b):
choice = b.description
b.color = 'white'
b.background_color = 'green' if choice == correct else 'red'
time.sleep(2)
container.close()
clear_output()
redraw()
for button in buttons:
button.on_click(on_button_clicked)
redraw()
In [1]:
import random
import time
from IPython.display import Image, display, clear_output
from ipywidgets import widgets
from gtts import gTTS
import os
nchoices = 4
x = ['23','35','45','55','66','77']
def redraw():
choices = random.sample(x, nchoices)
correct = random.choice(choices)
#display(Image(correct))
display(correct)
time.sleep(0.5)
buttons = [widgets.Button(description = choice) for choice in choices]
container = widgets.HBox(children=buttons)
display(container)
def on_button_clicked(b):
choice = b.description
b.color = 'white'
b.background_color = 'green' if choice == correct else 'red'
if choice == correct:
tts = gTTS(text='Roger that. That is correct', lang='en')
tts.save("answer.mp3")
os.system("afplay answer.mp3")
if choice != correct:
tts = gTTS(text='Wrong answer Roger', lang='en')
tts.save("answer.mp3")
os.system("afplay answer.mp3")
time.sleep(2)
container.close()
clear_output()
redraw()
for button in buttons:
button.on_click(on_button_clicked)
redraw()
In [66]:
import random
import time
from IPython.display import Image, display, clear_output
from ipywidgets import widgets
text=widgets.Text("Enter your name:")
name = str()
display(text)
def handle_submit(sender):
print("Thank you")
text.on_submit(handle_submit)
name= (text.value.split(':')[1])
In [57]:
In [65]:
In [62]:
In [63]:
In [ ]:
In [ ]:
In [ ]: