cancel
Showing results for 
Search instead for 
Did you mean: 
GRB43
Level 3
35 27 1 4
Message 1 of 17
1,011
Flag Post

Trouble with the IF-THEN- PRINT command

HP Recommended
HP-Prime
Linux

I've had some success in programing the HP-Prime.  I created a litte Dice program that works nicely and I wanted to try to expand it into sort of a game with the IF-THEN- PRINT command as is found in Eddie Shore's HP Prime Programming Tutorial-2018.

His program example is as follows:

EXPORT QROOTS(A,B,C)
BEGIN
LOCAL D;
PRINT();
HComplex:=1;
D:=B^2-4*A*C;
IF D≥0 THEN
PRINT("Roots are real.");
ELSE
PRINT("Roots are complex.");
END;
PRINT((-B+√D)/(2*A));
PRINT((-B-√D)/(2*A));
END;

 

My program is as follows:

EXPORT Dice( )

BEGIN

LOCAL X;

LOCAL Y;

{X:=RANDINT(1,6),{Y:=RANDINT(1,6)};

END;

which works just fine and reports the results of rolling two virtual Game Die.

I tried to add the following without success:

 

EXPORT Dice( )

BEGIN

LOCAL X;

LOCAL Y;

{X:=RANDINT(1,6),{Y:=RANDINT(1,6)};

IF X+Y=12 THEN

PRINT ("You Win!");

END;

but something is wrong with the syntax of the IF THEN statement.  I have tried many vairiations of X+Y=12 without success,  Such as X+Y:=12.  (X+Y):=12,  (X+Y)=12, (X+Y=12),  (X)+(Y)=12),  X(1)+Y(1):=12, and even many more variations, which all end in a syntax error.

 

 

 

0 Kudos
16 REPLIES 16
Joe_Horn
Level 8
622 609 117 206
Message 2 of 17
Flag Post
HP Recommended

Every IF/THEN must have its own END command.  The END in your program is paired with its BEGIN.  Try inserting another END command to close the IF/THEN structure.  Please let us know if that works.

 

Happy Programming!

 

Disclaimer: I don't work for HP or anybody else.

-Joe-
0 Kudos
Maké
Level 9
3,551 3,490 180 541
Message 3 of 17
Flag Post
HP Recommended

Hi!, @GRB43 :

Dice.PNG

Kind Regards !.
Have a nice day !.
@Maké (Technical Advisor Premium - HP Program Top Contributor).
Provost in HP Spanish Public Forum ... https://h30467.www3.hp.com/
0 Kudos
cyrille
Level 6
Level 6
252 251 42 77
Message 4 of 17
Flag Post
HP Recommended

Hello,

 

I do not know what you think that you are doing with this statement:

{X:=RANDINT(1,6),{Y:=RANDINT(1,6)};

 

but let me tell you what you are actually doing 🙂

What you are doing is creating a 2 element List (hence the {} with the 2 expressions separated by a ,)

Each of these expressions doing a variable store of a value (the randint)...

If what you want is JUST to return 2 random numbers: {randint(1,6),randint(1,6)} would sufice (actually, randint(2,1,6) would also work.

If what you want to do is store 2 random numbers in X and Y,

local X= RANDINT(1,6), Y:=RANDINT(1,6);

or

local X, Y;

X:= RANDINT(1,6);

Y:=RANDINT(1,6);

 

would work also.

 

But creating them as a list is a waste of CPU time and memory.

 

Cyrille

I am an HP Employee
0 Kudos
GRB43
Author
Level 3
35 27 1 4
Message 5 of 17
Flag Post
HP Recommended

" ~ creating them as a list is a waste of CPU time and memory.

Cyrille"

 

I'm still working on it, but after a lot of experimentation a list seems to be the only thing that works because if you don't have two items in a list sometimes only a single Die is reported back. I don't know why that is, because as I understand it the limit is between 1 & 6, so I don't know why one would drop out occasionally.

 

So far I haven't been successful in having it print the "You're a Winner!" statement even if it rolls a combination of  "12"


 

0 Kudos
GRB43
Author
Level 3
35 27 1 4
Message 6 of 17
Flag Post
HP Recommended

@Joe_Horn wrote:

"Every IF/THEN must have its own END command.  The END in your program is paired with its BEGIN.  Try inserting another END command to close the IF/THEN structure.  Please let us know if that works."

 

I got it working, but I would now like to have two IF   THEN   ELSE statements

 

IF   (x+y≠12) THEN

RETURN (x+"   "+y);

ELSE

PRINT ("6   6 = Box Cars    You Win!");

 

and the second being:

 

IF   (x+y=2) THEN

PRINT ("1  1 = Snake Eyes   You Loose!");

ELSE

RETURN (x+"   "+y);

 

but so far only the first works and when  1   1  is reported back there is no message  and the program will repeat as many times as you hit <Enter>

 

In other words only the first IF   THEN   ELSE statement responds and the second is apparently ignored.

 

I have three END: statements at the end.

 

Disclaimer:  I don't work for HP, and  I'm not a computer programer or mathematician, and have extemely limited knowledge as it relates to the subject of advanced calculators.


0 Kudos
cyrille
Level 6
Level 6
252 251 42 77
Message 7 of 17
Flag Post
HP Recommended

hello,

try:

 

export dices()
begin
  local x= randint(1,6), y= randint(1,6); // Create 2 variables and init them with random numbers between 1 and 6
  if x+y==12 then // if 2 6, then print you win
    print("you win");
  else
    print(x+" "+y+" you loose"); // else, print the 2 number and you loose
  end;
  return {x, y}; // return a list made of the 2 numbers
end; // end of program

 

I am an HP Employee
0 Kudos
GRB43
Author
Level 3
35 27 1 4
Message 8 of 17
Flag Post
HP Recommended

See the post above.  It is now working with one statement but not two.

 

Thanks for the effort.

0 Kudos
Maké
Level 9
3,551 3,490 180 541
Message 9 of 17
Flag Post
HP Recommended

Hi!, @GRB43 :

You can see, this very important examples from Eddie Shore, from ... https://drive.google.com/file/d/184JoIhaaOXoEEyfdrY-nOdCVppQ3L9sU/view

Kind Regards !.
Have a nice day !.
@Maké (Technical Advisor Premium - HP Program Top Contributor).
Provost in HP Spanish Public Forum ... https://h30467.www3.hp.com/
0 Kudos
GRB43
Author
Level 3
35 27 1 4
Message 10 of 17
Flag Post
HP Recommended

That's where I got my example from.   I put his sample program in my first post.  The one I'm working on is a variation of his combination lock, but with the IF, THEN, ELSE statements added.   I like the way you add comments after each command statement.  It's very helpful.

0 Kudos
† The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation