Creating a Course with Subtasks
In this section:
- Introduction
- Creating a course
- Working with a task
- Working with a subtask
- Working with the third subtask
- Previewing project
Introduction
This guide is intended for the educators. In our course we will implement a simple class for the complex numbers. We’ll see how to use the subtasks feature.
Note: PyCharm Edu runs on Windows, Linux and Mac platforms. We used Windows to prepare this guide. On Linux and Mac some menu names or workflows may slightly differ.
Creating a course
Create a course as described in the section Creating a course. This course has the name Subtasks. To learn about the basic notions, read the section PyCharm EDU Basics.
Working with a task
Creating a task description
Prior to creating a task description, make sure that a task or test file is opened! Next, click the pencil button in the Task Description tool window and fill in the task text as follows:
<html>
Let's implement a simple class for the complex numbers
<br>
<br>
Implement __str__ method to print a complex number in the format:
<br>
<code>re + im * i</code>
<br>
</html>
Releasing the button results in showing the task description in the following format:
Creating a task and test
Open the file task.py
for editing (F4) and enter the following code:
class Complex:
def __init__(self, re, im):
self.re = re
self.im = im
def __str__(self, *args, **kwargs):
return "%d + %d*i" % (self.re, self.im)
if __name__ == '__main__':
print(Complex(2, 2))
Next, open the file tests.py
for editing and enter the following code:
from lesson1.task1.task import Complex
from test_helper import run_common_tests, get_file_output, failed, passed
if __name__ == '__main__':
run_common_tests()
if str(Complex(2, 3)) == "2 + 3*i":
passed()
else:
failed("Representation should be re + im*i")
So the task and test are ready. What’s next?
Creating an answer placeholder
Student’s task is to implement the method __str__
, so we’ll surround it with a placeholder:
In the Add Answer Placeholder dialog, enter the text that will appear in the educational version of your course:
Checking the task
Next, with the file task.py
having the focus, in the Task Description tool window, click the check button to make sure that the tests pass:
Really, you see the result of executing tests.py
in the Run tool window:
Working with a subtask
Adding a subtask
Let’s add a new subtask. To do that, make sure that the file task.py
has the focus, right-click the editor background and choose Add Subtask on the context menu. As a result, we have __str__
function greyed out, and Switch subtask link on top of the editor:
What does it mean?
- We have two subtasks now, our existing task being the first one.
- The link Switch subtask in the editor allows adding, selecting, or deleting a subtask.
- The greyed out
__str__
method doesn’t belong to the currently selected subtask.
Creating a task text for the current subtask
As you see, the text of the current subtask is shown in the Task Description tool window, and reads “Write your text here”. So, let us fill in the contents of the currently selected subtask. To do that, click in the Task Description tool window, and paste the following text:
<html>
Implement a function to calculate module
<br><br>
Use this code:
<pre><code>
def module(self):
pass
</code></pre>
You also should print module of 3 + 4*i to check that it equals 5
</html>
Modifying a task file for the current subtask
Open for editing the file task.py
and paste the following code:
import math
class Complex:
def __init__(self, re, im):
self.re = re
self.im = im
def __str__(self, *args, **kwargs):
return "%d + %d*i" % (self.re, self.im)
def module(self):
return math.sqrt(self.im * self.im + self.re * self.re)
if __name__ == '__main__':
print(Complex(2, 2))
print(Complex(3, 4).module())
Creating a test for the current subtask
Next, open for editing the file tests.py
(this is the test file for the current subtask) and paste the following code:
from lesson1.task1.task import Complex
from test_helper import run_common_tests, failed, passed, get_answer_placeholders
if __name__ == '__main__':
run_common_tests()
a = Complex(3, 4)
if a.module() == 5:
passed()
else:
failed("Wrong module for 3 + 4*i")
Adding placeholders for a subtask
We’re going add two new placeholders. To do that, select the fragment of code to be written by the student, and choose Add Answer Placeholder on the context menu. First, select the function module
, right-click the selection and choose Add Answer Placeholder on the context menu:
Next, select the print
statement you’ve added, right-click the selection and choose Add Answer Placeholder on the context menu:
Hiding answer placeholders
We actually don’t want our student to see these two placeholders when they work on the first subtask. So we’re going to hide them.
To do that, jump to the frame of an answer placeholder, right-click it, and then point to Answer Placeholder on the context menu. Then from the submenu, choose the command Hide for Previous Subtasks.
Hidden placeholders become grey. Try to switch to the first subtask to see that these two placeholders are not shown.
Checking a subtask
Click the check button in the Task Description tool window to make sure that tests for the second subtask have passed successfully:
Working with the third subtask
Adding the third subtask
Let’s now create the last subtask. Click the link Switch Subtasks and choose Add Subtask. In this subtask we’re going to ask our students to modify __str__
method to print module as well.
Click the pencil button in the Task Description tool window and enter the following task description:
<html>
Modify <code>__str__ </code> method to include module:
<br><br>
<b>3 + 4*i 5.0</b>
</html>
Typing in a hidden placeholder
We are going to type a new code instead of the __str__
method. However, this method is greyed out. How to deal with it?
Easily. Just right-click the grey method, point to Answer Placeholder, and then choose Activate:
The method becomes editable:
Now, you can type the correct answer:
def __str__(self, *args, **kwargs):
return "%d + %d*i %.1f" % (self.re, self.im, self.module())
Checking the third subtask
Next, open for editing the file tests.py
and enter the following code:
from lesson1.task1.task import Complex
from test_helper import run_common_tests, get_file_output, failed, passed
if __name__ == '__main__':
run_common_tests()
if str(Complex(3, 4)) == "3 + 4*i 5.0":
passed()
else:
failed("Representation should be re + im*i module")
Let’s make sure that the tests pass. To do that, in the Task Description tool window click the button and see the output:
Previewing project
Switch to the first subtask (click the link Switch subtask and choose subtask 1). Then on the main menu, choose :
and see how it will look for the students: