3

I'm just starting TI-86 BASIC programming with the following guessing game:

:randInt(1,10)→X
:0→A
:Repeat A=X
:Disp "Guess the number"
:Input "between 1 and 10.", A
:End

My understanding of the Repeat statement is that the block will execute until the condition is true. In my case, I find that the block executes exactly once. That means the value of A that the user enters is always the same as the random value of X, which I find hard to believe.

Any idea what I'm doing wrong?

Steve
  • 8,066
  • 11
  • 70
  • 112
  • 2
    It works perfectly fine for me on the TI-84 SE+. Could you post your full code instead of just this excerpt? – user3932000 Dec 20 '15 at 17:16
  • This *is* the full code. The program is supposed to end when the guess is correct. The polish can come later, if at all. – Steve Dec 21 '15 at 04:09
  • Huh, that's very strange. Even on the TI-89 the equals operator is used to compare two objects. Maybe try `==`? – user3932000 Dec 22 '15 at 12:50
  • Yes. See my [answer](http://stackoverflow.com/a/34392266/584670). – Steve Dec 22 '15 at 13:04

2 Answers2

4

= is "equation-variable assignment", not equality testing

I don't have a TI-86, but I'm pretty sure this is right.

A less commonly used method of storing a value to a variable is with the "=" operator. The code

:A=45

does pretty much the same thing as [the store arrow], except that it makes A an "equation variable" (Which can be used in the equation solver) instead of a "real variable".

Source

Since A is being stored into, the expression A=X will return the new value of A; that is, X. Because TI-BASIC considers all nonzero numbers to be true, and X is always between 1 and 10, A=X assigns A to X and returns a true value, thus stopping the loop.

As OP said, use == instead for equality comparison.

lirtosiast
  • 592
  • 4
  • 15
  • Thanks, that works. I wish there was a less awkward way to compare two variables for equality. I was thinking of using the TI-86 as a distraction-free environment for my son to learn programming, but now I'm not so sure. – Steve Dec 21 '15 at 04:16
  • `While A/=X` (/= represents the not-equals sign) would also work, right? –  Dec 21 '15 at 07:33
1

Variables in the TI-86's version of TI-BASIC can be compared using the == operator. So the program becomes

:randInt(1,10)→X
:0→A
:Repeat A==X
:Disp "Guess the number"
:Input "between 1 and 10.", A
:End
Steve
  • 8,066
  • 11
  • 70
  • 112