Modules and imports · 13 / 16 Exercise Medium

Dynamic import

Objective

The import statement needs the module name written in the code. When the name is only known while the program runs, you use importlib.import_module.

Instructions

  • Read n and then n lines with the format module function arg1 arg2 ....
  • Import the module with importlib.import_module(name) and get the function with getattr(module, function_name, None).
  • Convert the arguments to int if possible and, if not, to float, and show the result of calling the function with them.
  • If the module does not exist (ModuleNotFoundError) show Module not found. If the module exists but does not have that function, show Function not found.
Input
3
math sqrt 16
operator add 2 3
math gcd 12 18
Output
4.0
5
6

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise