oTree Forum >

String Field with multiple possible answers and Form Field Validation

#1 by cswell

Hello,

I'm trying to do a form field validation and error message for a string form field where multiple different answers are possible. 

def error_message(player, values):
    solutions = dict(
        test1 = '3.14' and '3.15',
        test2 = '3.16'
    )

    error_messages = dict()

    for field_name in solutions:
        if values[field_name] != solutions[field_name]:
            error_messages[field_name] = 'Wrong answer'

    return error_messages
    
For example, in the first form, the subject could type in "3.14" or "3.15" and either would be a valid answer. But anything outside of these two possible answers would result in an error message.

So far if I code "'3.14' and '3.15'", only 3.15 results in a correct answer. If I code "'3.14' or '3.15'", then 3.14 is the correct answer. I know that I could instead do a multiple-choice question, but I wanted to know if this could be done in the first place.

#2 by BonnEconLab

I don’t think that you can use the logical operator “and” in the way you do.

The following might work (I can’t test it right now):


def error_message(player, values):
    solutions = dict(
        test1 = ['3.14', '3.15'],
        test2 = ['3.16'],
    )

    error_messages = dict()

    for field_name in solutions:
        if values[field_name] not in solutions[field_name]:
            error_messages[field_name] = 'Wrong answer'

    return error_messages

Write a reply

Set forum username