Two possible solutions

Hi there. I’m really new to using the computational layer and am slowly figuring things out. Here is one I am currently stuck on. I want to be able to check inputs for correctness. I understand the code for checking a particular value, but what if there are two or more correct solutions. For instance, 1/2 or 0.5 or 50% could all be correct answers. How do I code for this? Thank you.

For numerical values, 1/2 and 0.5 are considered equivalent (it’s actually a little harder if you want a specific one). If you type “50%” into a math input (in a table as well), it will autofill to look like this "50% of ", basically prompting you for 100 in this case. But if you wanted to accept “50% of 100” then…
correct= input.numericValue=0.5 or input.numericValue=50

Students might also type “50% of {insert problem total here}”. Or, you could just have a prompt or warning to give an answer as a fraction or decimal. Or, you could add a suffix to the input or table cell if you specifically wanted as a percentage, and just accept 50. It depends on your learning goal I suppose.

1 Like

In general, you can use Boolean operators for conditional statements. and and or are valid to use. You can also do combinations with parentheses. For example:

correct = (condition1 and condition4) or (condition2 and condition3)

Thank you for your quick response @Daniel_Grubbs.

The “or” doesn’t seem to be working. By example. Let’s assume I have this bit of code:
correct: linearTwo = 9 and linearThree = 12 and exponentialTwo = 12 and exponentialThree = 24

What if I wanted to accept two answer choices here where I am defining the correct value for several variables?

Would this work?
correct: linearTwo = 9 or 3 and linearThree = 12 or 72 and exponentialTwo = 12 or 16 and exponentialThree = 24 or 15

Admittedly, my answer choices don’t make any sense. I’m thinking more about the syntax. Do I need to add parentheses anywhere?

You’re on the right track, but you need to attach the variable to the standalone numbers, i.e. linearTwo = 9 or linearTwo = 3. I tested it with and without parenthesis and they both seemed to work the same, but it never hurts to add them when you have multiple conditionals to make sure the computer is thinking the same way you are. In the example @Daniel_Grubbs gave earlier, I think the computer would look for condition4 or condition2 first if the parenthesis were omitted.

That’s exactly the advice I needed. Thank you!

thank you for this answer!! this was so helpful for me for how to code multiple answers on a math input

Hi Daniel_Grubbs, could you help me with decimals and fractions on a table?
My code for checking correctness on a table has: table1.cellContent(2,3)=“0.5”, but students can enter 1/2 on the table. However, I cannot get it to accept fraction form as well.
Please help! Thank you

You want to use table1.cellNumericValue(2,3)= 0.5 instead. This will check the value rather than the text. Although that also means an expression equivalent to 0.5 could also be accepted. You can add some pattern matching to allow general forms, or to allow a specific answer…

p=patterns
#checks the value is 0.5 and that the content is either a number or fraction
form= p.anyOf(p.number, p.fraction(p.number, p.number))
checkForm= table1.cellNumericValue(2,3)=0.5 and form.matches(table1.cellContent(2,3)
#uses your initial way to check OR checks a fraction with numerator 1 and denominator 2
specific= p.fraction(p.number.satisfies(`x=1`), p.number.satisfies(`x=2`))
checkSpecific= table1.cellContent(2,3)= "0.5" or specific.matches(table1.cellContent(2,3))

This worked perfectly! Thank you :grinning: