Interact Exercise 01

Import


In [44]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np

In [45]:
from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display

Interact basics

Write a print_sum function that prints the sum of its arguments a and b.


In [46]:
def print_sum(a, b):
    return (a+b)

Use the interact function to interact with the print_sum function.

  • a should be a floating point slider over the interval [-10., 10.] with step sizes of 0.1
  • b should be an integer slider the interval [-8, 8] with step sizes of 2.

In [1]:
w = interactive(print_sum, a = (-10.0,10.0,0.1), b = (-8, 8, 2))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-57a1bdb0d2c7> in <module>()
----> 1 w = interactive(print_sum, a = (-10.0,10.0,0.1), b = (-8, 8, 2))

NameError: name 'interactive' is not defined

In [48]:
display(w)

In [49]:
w.result


Out[49]:
-0.09999999999999945

In [50]:
assert True # leave this for grading the print_sum exercise

Write a function named print_string that prints a string and additionally prints the length of that string if a boolean parameter is True.


In [77]:
def print_string(s, length=False):
    print (s)
    if length == True:
        print(len(s))

Use the interact function to interact with the print_string function.

  • s should be a textbox with the initial value "Hello World!".
  • length should be a checkbox with an initial value of True.

In [78]:
w = interactive(print_string, s = "Hello, World!")

In [79]:
w


Hello, World!

In [54]:
assert True # leave this for grading the print_string exercise

In [ ]: