Home > Software engineering >  What is the unexpected symbol near then in these screenshots?
What is the unexpected symbol near then in these screenshots?

Time:12-20

I'm using an android lua IDE program, and I can't seem to find the reason why it's giving me this error of "unexpected symbol near then" on line 27. please help it would be most appreciated[1]: https://i.stack.imgur.com/lcIsM.png

CodePudding user response:

A look in the Lua reference (I do not know the language):

if ...
    then
    do
        bal = subtract(bal, 1000)
        print("You lost")
    end
elseif ...
    then
        bal = add(bal, 2000)
        print("You won")
    do
    end
end

(The indentation is probably entirely wrong.)

CodePudding user response:

From your screenshot:

if v == 6 or 7 or 8 or 9 or 10
  then bal = subtract(bal,1000)
    then print("You lost the gamble! Your new balance is", bal")
elseif v == 1 or 2 or 3 or 4 or 5 
  then bal = add(bal, 2000)
    then print("You won the gable! Your new balance is", bal)
end

Problem 1: every number is a true value, hence no matter what v's value is, v== 6 or 7 or 8 or 9 or 10 will always resolve to true.

You need to write v == 6 or v == 7 or v == 8 or v == 9 or v == 10 or simply v >= 6 and v <= 10.

Same for v == 1 or 2 or 3 or 4 or 5.

Problem 2: the syntax of the conditional statement is if condition then body end. You cannot have extra thens.

Fixed code:

if v == 6 or v == 7 or v == 8 or v == 9 or v == 10 then
  bal = subtract(bal,1000)
  print("You lost the gamble! Your new balance is", bal")
elseif v == 1 or v == 2 or v == 3 or v == 4 or v == 5 then
  bal = add(bal, 2000)
  print("You won the gable! Your new balance is", bal)
end

Other issues:

Why would you have a subtract and add function? Replacing basic arithmetic operators with functions makes no sense. Just use bal = bal - 1000

As v seems to be an integer between 1 and 10, why not simply use v < 6 as your win condition? The rest can be done with an else. You don't need to explicitly check every case.

Also your game doesn't make too much sense. Assuming you're using a uniform distribution in your random number generator you win 2000 half the games and lose 1000 half the games. So over many games you are guaranteed to win more than you lose.

  • Related