Don't know what's wrong with my code

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Alex3179
Posts: 1
Joined: Fri Oct 31, 2014 6:49 am

Don't know what's wrong with my code

Post by Alex3179 » Fri Oct 31, 2014 7:16 am

Hey, I'm making a game where you guess a number between 1 and 100. I wrote all the code for the button and tried to apply it, but this error message came up-
button "guessButton": compilation error at line 42 (if: missing 'end if'), char 1
Could someone please help me, because I don't know what the problem is.

Code: Select all

global varCompNumber
global varUserNumber
global varResult
on mouseUp
   put random(100) into varCompNumber
   put field "number" into varUserNumber
   if varCompNumber = varUserNumber then
      put "Correct" into varResult
   else
      if varUserNumber > varCompNumber-11 and varUserNumber < varCompNumber-11 then
         put "Very Close" into varResult
      else
         if varUserNumber > varCompNumber-21 and varUserNumber < varCompNumber-21 then
            put "Close" into varResult
         else
            if varUserNumber > varCompNumber-51 and varUserNumber < varCompNumber-51 then
               put "Off" into varResult
            else
               if varUserNumber > varCompNumber-101 and varUserNumber < varCompNumber-101 then
                  put "Way Off" into varResult
    end if
               
   if varCompNumber = varUserNumber then
      set visible of field "congrats" to true
      add one to field "timesGuessed"
      put varResult into field "closeness"
      put " " after line 1 of field "yourGuesses"
      put varUserNumber after line 1 of field "yourGuesses"
      put " (" after line 1 of field "yourGuesses"
      put varResult after line 1 of field "yourGuesses"
      put ")" after line 1 of field "yourGuesses"
   else
      add one to field "timesGuessed"
      put varUserNumber after line 1 of field "yourGuesses"
      put " " after line 1 of field "yourGuesses"
      put varUserNumber after line 1 of field "yourGuesses"
      put " (" after line 1 of field "yourGuesses"
      put varResult after line 1 of field "yourGuesses"
      put ")" after line 1 of field "yourGuesses"
   end if
   
end mouseUp

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Don't know what's wrong with my code

Post by [-hh] » Fri Oct 31, 2014 7:37 am

If you remove all line breaks between else and if (that is: write "else if") then the script compiles.

But your script cannot work from another reason: The math.
You check if a number Z is greater than N and is also smaller than N. This is always false (because it's an impossible outcome).

Here is a replacement for the first part of your script, I presume this is (hopefully), what you intend to have

Code: Select all

  put random(100) into varCompNumber
  put field "number" into varUserNumber
  put abs(varUserNumber - varCompNumber) into myDiff
  if myDiff= 0 then put "Correct" into varResult
  else if myDiff < 11 then put "Very Close" into varResult
  else if myDiff < 21 then put "Close" into varResult
  else if myDiff < 51 then put "Off" into varResult
  else put "Way Off" into varResult
  if myDiff = 0 then
    set visible of field "congrats" to true ...
Note: abs(X-Y) < N checks the same as (-N < X-Y and X-Y < N)
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Don't know what's wrong with my code

Post by dunbarx » Fri Oct 31, 2014 4:09 pm

Hi.

Always do what Hermann says.

But now take just a little time to learn how to rewrite your handler with a "switch" construction, as opposed to the "if" construction. Especially in multi-level or many-layer tasks, "switch" is so much more readable and manageable, and far more forgiving of closing those "end if" bugaboos. It will become your favorite method.

Craig Newman

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Don't know what's wrong with my code

Post by [-hh] » Fri Oct 31, 2014 8:42 pm

Craig.

Of course, as always, you are generally right with preferring 'switch' to long 'if/else-if' structures.
But I wonder how you would do that here where the check is not against single values but against ranges of values (0,1-10,11-20,21-50, > 51)?
Even if you use "switch (myDiff div 10)", what is hard for a beginner, you get at least the same number of cases. What am I missing?

Hermann
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Don't know what's wrong with my code

Post by dunbarx » Sat Nov 01, 2014 3:10 am

Hermann.

In the original nested if constrution:

Code: Select all

switch
  case varUserNumber > varCompNumber-11 and varUserNumber < varCompNumber-11
    put "Very Close" into varResult
    break
  case
    etc...
I just find this much easier to read and manage. But you know this. What am *I* missing?

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Don't know what's wrong with my code

Post by [-hh] » Sat Nov 01, 2014 8:15 am

Craig, how good that you are here!
You'll remain my first teacher (also 'teacher number one') even when I'll have left primary LC school ...

I didn't read the dictonary entry to "switch" in detail and used it only for non-empty switchExpressions. That's what I was missing and learned from your example: Usage of an empty switchExpression.
Rewriting the "else-if" code from below yields:

Code: Select all

    put random(100) into varCompNumber
    put field "number" into varUserNumber
    put abs(varUserNumber - varCompNumber) into myDiff
    switch
      case myDiff= 0; put "Correct" into varResult; break
      case myDiff < 11; put "Very Close" into varResult; break
      case myDiff < 21; put "Close" into varResult; break
      case myDiff < 51; put "Off" into varResult; break
      default; put "Way Off" into varResult
    end switch
    if myDiff = 0 then
      set visible of field "congrats" to true ...
I find it now easier to manage than my "else-if" code below. But I find it easier to read, rating also clarity, only if I'm using semicolons as above (and getting severe criticism from all grand masters/ladies of LC for that).

Have here again the "else-if" variant of my previous post for easier comparison.

Code: Select all

  put random(100) into varCompNumber
  put field "number" into varUserNumber
  put abs(varUserNumber - varCompNumber) into myDiff
  if myDiff= 0 then put "Correct" into varResult
  else if myDiff < 11 then put "Very Close" into varResult
  else if myDiff < 21 then put "Close" into varResult
  else if myDiff < 51 then put "Off" into varResult
  else put "Way Off" into varResult
  if myDiff = 0 then
    set visible of field "congrats" to true ...
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Don't know what's wrong with my code

Post by dunbarx » Sat Nov 01, 2014 6:57 pm

Hi.

You are going to love this new toy.

And thank you for reminding me that there are cases where the semicolon as a line continue gadget makes a lot of sense. I must try to remember to start using that, at least when there is only one line that "follows".

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Don't know what's wrong with my code

Post by jacque » Sun Nov 02, 2014 5:03 pm

dunbarx wrote:Hi.

You are going to love this new toy.

And thank you for reminding me that there are cases where the semicolon as a line continue gadget makes a lot of sense. I must try to remember to start using that, at least when there is only one line that "follows".

Craig
I hope if you post code to the forums that you will remove them in any examples here. The format is not standard and, like omitting" the" before property names, it could be picked up by new users who will think this is how it should work.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Don't know what's wrong with my code

Post by [-hh] » Sun Nov 02, 2014 8:55 pm

This is my opinion:
The main thing for beginners is, that they see example code that works.
The style ("format") is secondary. Some prefer to be rather wordy, some to be rather compact. Each and every style has his own set of (dis-)advantages, just like different styles of writing and speaking in real life.

I really can't imagine, that anybody feels to be influenced in his style from code examples because he/she thinks this would be "standard". It's the content that influences, the "wow-that's-working". For example:
I'm now nearly two years with LC and have my own (sometimes criticised) style, although most scripts I saw at my beginning were from Craig, who is, in a positive sense, rather a purist.
shiftLock happens

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Don't know what's wrong with my code

Post by jacque » Sun Nov 02, 2014 10:03 pm

Yes, I understand it's a personal preference and I think it will work fine for you. But I have an example. For many years I never saw anyone omit "the" before built-in properties except for one developer I know well, who had always done it. It works fine for him, because he was experienced and he knew when he needed to write "the" and when he could omit it. But when new users saw his code they emulated it because he was obviously knowledgeable, and now it has become fairly common. (Until recently, the dictionary did not mention that "the" was optional.) When their code breaks they can't understand why they need "the" before custom propeties but not before built-in ones.

The semicolon convention was part of the original MetaCard engine, implemented for use in the MC message box where there was no multi-line pane. It was handy there (and still is, usually) though I never saw Dr Raney use it in any actual IDE scripts. I've found that semicolons often work in LC's one-line message box, but sometimes they do not. If there are occasions where it breaks in scripts too, confusion can result.

Again, this is personal preference. Everyone has their own style, and I recognize I'm being picky. But I think new users benefit from consistency, at least at first.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Don't know what's wrong with my code

Post by dunbarx » Sun Nov 02, 2014 10:41 pm

I keep it wordy, if not verbose, at least in the beginners forum, as a duty of the helpers.I always use "the", and never use a semicolon.

For the same reason I usually write three or four lines of code that could readily be condensed to one, just to make the flow of coding clear:

Code: Select all

find tText in fld 1
put the foundLine into tFound
put word 2 of tFound into tLine
put the length of line tline of fld 1 into lineLength
Could this be done with a one liner? Yes, but it would be something like:

Code: Select all

   put the length of line word 2 of the foundlIne of fld 1 into lineLength
Never mind it is always good practice to lock down such functions as "the foundLine" right away. Never mind it is often quite useful storing those intermediate values, not appreciated until later. Anyway, that is something new users should be shielded from while learning. They can get clever as they learn.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Don't know what's wrong with my code

Post by [-hh] » Mon Nov 03, 2014 1:38 am

@jacque.
You haven't convinced me until now.
Because I always use "the" when *getting* a property I tried again without and had to learn, that "the" is indeed needed in some cases when getting a custom property.
But please give ONE example of a line of code that breaks because of a missing "the" when *setting* a custom property and I'll write always "the" also in this case and will propagate it.

A semicolon is in script lines equivalent to a CR.
What's wrong with using it? Nobody has difficulties to understand this. One can use it for example to indirectly emphasize things that start a new line (see the switch part of the script above).
Also a message box code works with a semicolon if and only if it works with a CR; that is as if you would give it in two lines, entered one after the other in single-line-mode. No danger from that for scripts of objects.

@Craig.
I don't think that beginners should be shielded before working constructs simply because they are not "main stream". Just the opposite: That's one of the most important properties of LC, to have such a lot of possible correct ways to go.
But I think beginners should be shielded before severe logical mistakes like "the number of items of empty is 0", or setting "empty is among the items of empty" to false. Such logically wrong results are given by the engine although they are known to be wrong and they are still preserved from some obscure reasons.
This leads (not to seldom) to confusion, rather than using a semicolon instead of a CR.
shiftLock happens

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10057
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Don't know what's wrong with my code

Post by FourthWorld » Mon Nov 03, 2014 3:09 am

I'm with -hh on the semi-colons. A far more serious stylistic offense is the dangling"then" :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Don't know what's wrong with my code

Post by jacque » Mon Nov 03, 2014 7:45 pm

This isn't really as big a deal to me as this thread would imply. I'm happy to let people code in whatever manner they prefer, but I am always aware that in these forums we have many new users who are struggling to learn the language, and that beginners need simple, clear, consistent rules until they become proficient enough to understand when they can branch out. This comes from years of teaching LC beginner classes, and watching newcomers (especially those coming from other languages) as they struggle to adapt to LC's different way of doing things. So here in the forums, I prefer to see consistent examples that contain as few exceptions as possible.

English is a very difficult language to learn because of all the exceptions it contains: "I" before "e" except after "c" or when it's pronounced with a long "a" sound. Or, these words are all pronounced differently: through, tough, though. English has many of these. Fortunately Livecode doesn't have to.

A beginner can remember this easily: "All properties should use "the" before the property name."
It is more difficult to remember: "The" is optional when getting or setting a property value, except for "target" which gives different values depending on "the" and except for custom properties which always require "the".

I have seen user confusion here in the forums where a user does not understand why they can't omit "the" when getting a custom property.

It is easy for a new user to remember: Each line of code ends in a carriage return.
It is more confusing for a new user to understand why substituting semicolons doesn't always work in the message box. Here's one:

Code: Select all

repeat with x = 1 to 10;add 1 to n;end repeat;put n
This doesn't error but it doesn't give the result you want either. It can be corrected by initializing "n". But a new user would have to know that.

This is all pretty picky stuff, and like I said, I'm not extremely invested in restraining people's coding and posting styles. Do whatever you want. But it is always in the back of my mind that many newcomers are here, and we want LC to be easily learned with as few roadblocks as possible.

Richard: disassociated "then"s make me crazy too.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Don't know what's wrong with my code

Post by [-hh] » Tue Nov 04, 2014 1:00 am

First of all: The two lines (containing the 'dangling' then)

Code: Select all

if myDiff = 0 then
   set visible of field "congrats" to true ...
are not from me but a copy of OP's code that should indicate his original code could go on after my 'insertion/correction'.

==[Use of "the"]

You write:
All properties should use "the" before the property name.

I write/always wrote: You can use safely
get the <property> of <object>
set <property> of <object> to <value>


I believe this is a reasonable burden for a beginner.

==[Use of semicolon]

Your message box example is somehow misleading as it is one that doesn't depend on using semicolons or not. It depends on the scope of the variables used in the one-line message box (that's why you write about initializing n, what would make n global). The two lines

Code: Select all

repeat 10; add 1 to n; end repeat; put n
-- or
do "repeat 10" &cr& "add 1 to n" &cr& "end repeat" &cr& "put n"
yield the same (funny) result "true" from the one-line message box and yield the correct result 10 when used within an object's handler.


I can understand your longing for a good, unified phrasing style. But this battle of you and some others was lost long before I joined LC.

What's much more important are the impact you have and advices you can give with your structural style (how to split tasks of a handler, when to use a group's script depending on being a background or not, when to use a behavior-script, hints to seldom used handlers, ...).
You should, it's a plea, strengthen that, not only for beginners.
shiftLock happens

Post Reply