Interact Exercise 01

Import


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

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


:0: FutureWarning: IPython widgets are experimental and may change in the future.

Interact basics

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


In [17]:
def print_sum(a, b):
    """Print the sum of the arguments a and b."""
    c=a+b
    return c
raise NotImplementedError()


---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-17-7487ad8e8b6b> in <module>()
      3     c=a+b
      4     return c
----> 5 raise NotImplementedError()

NotImplementedError: 

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 [26]:
interact(print_sum, a=(-10.0,10.0),b=(-8,8,2))


6.2

In [ ]:
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 [10]:
def print_string(s, length=False):
    """Print the string s and optionally its length."""
    print(s)
    if length==True:
        print(len(s))
    raise NotImplementedError()

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 [25]:
interact(print_string, s='Hello World!', length=True)


Hello World!
12
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-10-a08171c0b101> in print_string(s, length)
      4     if length==True:
      5         print(len(s))
----> 6     raise NotImplementedError()

NotImplementedError: 

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