Lab: Interactive Functions#

This lab has some simple user interfaces that will be completed by a function you write. Run the next cell before you begin:

[ ]:
import ipywidgets
from p4e.widgets import bind

Part 1: Interactive Functions#

Each of the following questions has code for a user interface (UI) and asks you to write a specified funtion.

1. Add Integers#

Using the UI in the next cell:

[ ]:
a_widget = ipywidgets.IntText(description="a:")
b_widget = ipywidgets.IntText(description="b:")
    bind('add_ints', {'a': a_widget, 'b': b_widget})

Write a function add_ints that takes two inputs, a and b and returns their sum.

  • Function name: add_ints

  • Arguments:

    • a (int): The first int

    • b (int): The second int

  • Returns: The sum of a and b

[ ]:

Part 2: Global Widgets#

In this part your interactive functions will have to use global variables.

1. Minimum and Maximum#

The interface in the next cell has three widgets:

  1. A slider that selects an integer

  2. An IntText that sets the minimum for the slider

  3. An IntText that sets the maximum for the slider

Run the UI in the next cell:

[ ]:
int_widget = ipywidgets.IntSlider(description='Number:', min=0, max=10)
min_widget = ipywidgets.IntText(description='Min:', value=0)
max_widget = ipywidgets.IntText(description='Max:', value=10)
bind('set_minmax', {'min': min_widget, 'max': max_widget,})
    int_widget, min_widget, max_widget,

Write a funcion called set_minmax that sets the values int_widget.min and int_widget.max.

  • Function name: set_minmax

  • Arguments:

    • min (int) - The minimum

    • max (int) - The maximum

  • Returns None

[ ]: