datacamp / pythonwhat

Verify Python code submissions and auto-generate meaningful feedback messages.
http://pythonwhat.readthedocs.io/
GNU Affero General Public License v3.0
67 stars 31 forks source link

Issues with test_for_loop + test_if_else: BLOCK BETA TESTING 9/7 #70

Closed hugobowne closed 7 years ago

hugobowne commented 7 years ago

Hi! See this exercise: https://campus.datacamp.com/courses/1533/3850?ex=12

Code here: https://github.com/datacamp/HBA-python-problem-playground/blame/master/chapter1.md#L740

I am having a number of issues with this:

  1. I need to have a for loop over a column of a DataFrame df['lang'] and test_for_loop() doesn't seem to work if i try
def test_for_iter():
    test_object("df['lang']")

I have set col=df['lang'] as a workaround but this isn't ideal at all: any ideas?

  1. I use
# Test: for loop iterable
def test_for_iter():
    test_object("col")

However, when I submit for entry in col1:, it throws me a different error and doesn't tell me that i should use col instead of col1. I have searched through documentation/wiki and can't find the solution. Any ideas?

  1. I have tried many different ways to test an if-else statement within a for loop. I can't seem to figure it out. Any ideas? you can see I have tried test_student_typed() but this will have many problems as the students need to type two nearly identical things in different places!
  2. test_student_typed() wasn't working as I expected: it did NOT accept the solution code UNTIL I used pattern=False. Can you explain why? I could not understand from documentation/wiki.

thanks!

In the exercise that follows, I need to do this in a user-defined function also so fingers crossed!

filipsch commented 7 years ago

Hey Hugo, I'd use the following SCT:

test_import("pandas")
test_function("pandas.read_csv")
test_object("df")

test_object("col")
def test_for_iter():
    test_expression_result()

def test_for_body():
    def test_test():
        test_function("langs_count.keys")
    def test_body():
        test_student_typed("\+=\s*1")
    def test_orelse():
        test_student_typed("=\s*1")
    test_if_else(index = 1,
                 test = test_test,
                 body = test_body,
                 orelse = test_orelse)

test_for_loop(
    index=1,
    for_iter=test_for_iter,
    body=test_for_body
)

test_object("langs_count")
test_function("print")

success_msg("Great work!")

Notice that:

  1. I threw a test_if_else() in there. I use test_function("langs_count.keys")
  2. test_student_typed() takes a regex by default, unless you set pattern = False, than it takes the string literally.

Some other notes:

  1. test_object("df['lang']") will not work; you can only specify actual names of objects inside test_object().

Another option, that's possible better than the one above:

test_import("pandas")
test_function("pandas.read_csv")
test_object("df")

test_object("col")
def test_for_iter():
    test_expression_result()

def test_for_body():
    test_object_after_expression("langs_count",
                                 extra_env = {'langs_count': {"en": 1}},
                                 context_vals = ['et'])
    test_object_after_expression("langs_count",
                                 extra_env = {'langs_count': {"en": 1}},
                                 context_vals = ['en'])

test_for_loop(
    index=1,
    for_iter=test_for_iter,
    body=test_for_body
)

test_object("langs_count")
test_function("print")

success_msg("Great work!")

Here, I'm not using a test_if_else() construct. Rather, I'm using test_object_after_expression twice. Once for the case where the context val (entry in this example) is et and et is not yet in a manually defined langs_count list, and once where the entry is en and en is already in the langs_count list. This will indirectly check whether the student correctly updated the expression, you see?

I'd go with the second version. I had to do some tweaks to pythonwhat, so it's possible that this won't perfectly work before deployment on September 5th.

filipsch commented 7 years ago

https://github.com/datacamp/pythonwhat/wiki/test_object_after_expression

hugobowne commented 7 years ago

thanks, @filipsch , i'll check this out after deployment.