1

As the image shows, If doesn't return "Fail" if the condition didn't succeed, but works when conditions match. Why did that happen and how can I fix it?

image: enter image description here

creidhne
  • 5,055
  • 4
  • 20
  • 28
David
  • 93
  • 5
  • 3
    try If[First @ prefix === P, "Correct", "Fail"]? – kglr Dec 12 '20 at 07:11
  • 2
    also If[First@prefix == P, "Correct", "Fail", "Fail"]? – kglr Dec 12 '20 at 07:11
  • You helped me again! Thank you! – David Dec 12 '20 at 07:21
  • 1
    For future purposes it is better to include the code in such a way that it is easy for other members to copy and paste it in a mathematica notebook :-) –  Dec 12 '20 at 10:50
  • For more (and important) information, Equal, SameQ, and PossibleZeroQ are discussed under "Background and Context" in the docs for Equal. There are also contrasting examples illustrating some of the points shown under "Properties & Relations." – Michael E2 Dec 12 '20 at 17:09

1 Answers1

1

We should use SameQ (===) to force the comparison to evaluate here:

If[First @ prefix === P, "Correct", "Fail"]
MarcoB
  • 67,153
  • 18
  • 91
  • 189
David
  • 93
  • 5