# Distance between points (random generator)

I’m trying to randomly generate numbers for students to find the distance between points using the Pythagorean Theorem. Every variable will check correctly until I get to the final stage of taking the square root of g. Do you see a problem with my code?

r=randomGenerator(b19.pressCount)

x_1=r.int(-10,10)
y_1=r.int(-10,10)
x_2=r.int(-10,10)
y_2=r.int(-10,10)

c= numericValue("{y_2} - {y_1}")
d= numericValue("{x_2} - {x_1}")
e=numericValue("{c}^2") f=numericValue("{d}^2")
g=numericValue("{e}+{f}")

content:"Find the distance from ({x_1},{y_1}) to ({x_2},{y_2}).

${result}" result = when input19.submitted and input19.latex=("\sqrt${g}") “\n\n\Correct​:raised_hands:”
when input19.submitted “\n\n\Incorrect. Try again.”
otherwise “”

Latex matching isn’t the best and it’s hard to tell what you’re actual code is (since the forum reformats some things). If you use triple backticks for the first and last lines of your code in the forum it will show your intended code.

However, I’d guess the problem is in the sqrt g, which should be something like:

...input19.latex=\sqrt{${g}}...  Also, not sure if you’re using c, d, e, and f to assess anything, but you could define g in one shot: g= numericValue("(${y_2}-${y_1})^{2}+(${x_2}-\${x_1})^{2}")

1 Like

Daniel beat me to it - although, @Daniel_Grubbs, do you know if it’s a new thing that the site shows that you are replying in real time?

I agree, the latex matching is not great. You could try something like this:

Also of note - make sure you enclose values to be squared in parantheses:

-3^{2}= -9
but
(-3)^{2} = 9


Instead of matching the latex strings, it can be easier to define functions and evaluate them at the same points to see if you get equivalent outputs.

FnG=simpleFunction(g)
result=
when simpleFunction(input19.latex).evaluateAt(1)=FnG.evaluateAt(1) “:raised_hands:\n\n\Correct​:raised_hands:”
otherwise ""

2 Likes

Not new. I’ve seen it before. I saw you replying when I edited my comment as well.

1 Like

Yes, my square root was formatted incorrectly. Thank you!

1 Like

Thank you. This is very helpful.

1 Like

Cheryl, could you share your activity here? I’m trying to do the same thing. No sense in reinventing the wheel, right??