# 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()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://auto-grade.joemazzone.net/python/parse-student-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
