> For the complete documentation index, see [llms.txt](https://auto-grade.joemazzone.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://auto-grade.joemazzone.net/python/parse-student-code.md).

# Parse Student Code

Often you just want to make sure students successfully used a particular function/command/structure.  The simplest way I have found to do this is just parse the student's code file and make sure what you are looking for is there.

## Example 1 - Used \_\_\_\_ function \_\_\_\_ times

For example, maybe you want to make sure the student used the input function 4 times.  We can parse the student's code file and count how many times `"input("` is present in their code.

This test assumes the student's file name is `example.py`.

**Grading Test:**

```python
import unittest

count = 0

class CodingRoomsUnitTests(unittest.TestCase):

    def test_default_case(self):
        global count
        f = open("example.py", "r")
        lines = f.readlines()
        f.close()
        
        for line in lines:
            line = line.strip().split()
            for words in line:
                if words.find("input(") != -1:
                    count += 1
                    
        self.assertTrue(count == 4)
        

if __name__ == '__main__':
    unittest.main()
```

Notice that the test asserts that the count is equal to 4. Of course you can change this condition to anything you want. For example, if you want to make sure they have at least 1 `input()` you could use `count >= 1`.

**Sample Solution:**

{% code title="example.py" %}

```python
name = input("What is your name?")
age = input("What is your age?")
hair = input("What color is your hair?")
place = input("Where do you live?")
```

{% endcode %}

## Example 2 - Used \_\_\_\_ thing \_\_\_\_ times

Sometimes if you are checking for a specific character/symbol it is better to use the `.count()` method so you do not miss an instance of the character/symbol.

This example checks that the student used the `+` addition at least 2 times. &#x20;

**Grading Test:**

```python
import unittest

count = 0

class CodingRoomsUnitTests(unittest.TestCase):

    def test_default_case(self):
        global count
        f = open("example.py", "r")
        lines = f.readlines()
        f.close()
        
        for line in lines:
            line = line.strip().split()
            for words in line:
                if words.find("+") != -1:
                    count += words.count("+")

        self.assertTrue(count >= 2)

if __name__ == '__main__':
    unittest.main()
```

## Example 3 - Used \_\_\_\_ function with these exact arguments

Since spaces can be used between arguments, to check if a function is called with exact arguments, you want to check each line of the student's code and remove all spaces.  Then find the function call you are looking for.&#x20;

This example checks that there is at least 1 call of `randint(501,1000)` in the student's code.

**Grading Test:**

```python
import unittest

count = 0

class CodingRoomsUnitTests(unittest.TestCase):

    def test_default_case(self):
        global count
        f = open("example.py", "r")
        lines = f.readlines()
        f.close()
        
        for line in lines:
            line = line.strip().replace(" ", "")
            if line.find("randint(501,1000)") != -1:
                count += 1
                
        self.assertTrue(count >= 1)

if __name__ == '__main__':
    unittest.main()
```
