Jump to content

Limiting the input value of a variable


aloy

Recommended Posts

How can I limit the input value for a variable?.

I tried the following lisp unsuccessfully:

 
(defun c:limit()
 (setq s 
(while (and (< s 2.5) (> s 7)) (setq s(getreal "\nGive value between 2.5 and 7:")))
)

Thanking in advance.

Aloy

Link to comment
Share on other sites

Those conditions cannot be met simultaneously:

(defun c:limit()
(setq s 
(while ([color=red]or[/color] (< s 2.5)
           (> s 7.0))
 (setq s (getreal "\nGive value between 2.5 and 7.0: "))
)
)

Link to comment
Share on other sites

(defun c:limit (/ in)
 (while
   (not
     (or (< (setq in (getreal "\n Give value between 2.5 and 7:"))
        2.5
     )
     (> in 7)
     )
   )
    (princ "\n ** Must be Less than 2.5 or Biggar than 7.0 **")
 )
 (princ)
)

Link to comment
Share on other sites

Tharwat, I'm afraid that your code is in fact rejecting the values it should accept and vice versa; there is also a conflict among prompter, respectively error message:

(defun c:limit (/ in)
 (while
   (not
     ([color=red]and[/color] ([color=red]>=[/color] (setq in (getreal "\n Give value between 2.5 and 7:"))
        2.5
     )
     ([color=red]<=[/color] in 7)
     )
   )
    (princ "\n ** Must be Less than 2.5 or Bigg[color=red]e[/color]r than 7.0 **")[color=red]   ;?!?[/color]
 )
 (princ)
)

Edited by MSasu
Fixed code to accept limits of interval too
Link to comment
Share on other sites

My understanding of the Op goal is to accept the values between 2.5 to 7.0 and that's what my code does .

 

I don't get what conflict you are talking about !

 

I got not error message with Cad 2013

Link to comment
Share on other sites

My understanding of the Op goal is to accept the values between 2.5 to 7.0 and that's what my code does .

That's true, "between"! Your routine accept only values outside this interval...

 

I got not error message with Cad 2013

Sorry, but mathematical logic isn't influenced by AutoCAD's version.

Link to comment
Share on other sites

Hi Tharwat,

Thanks for the code. It works; needs a correction to the string to give the intended meaning.

Regards,

Aloy

Link to comment
Share on other sites

RenderMan,

I thought 'INITGET' functions accept certain values, but not values within a defined range.

Regards,

Aloy

Link to comment
Share on other sites

Sorry for any confusion; perhaps I can clarify:

 

Technically, you're only presenting the user an illusion of limiting the value at the time of input, when in fact any real number input is accepted, and then checked to ensure the entered value falls within the desired range prior to continuing with your function.

 

INITGET is useful at 'Limiting the input value of a variable' (this thread's title), and not at ensuring that the user's real number input falls within a certain range after being entered, which is what is being done in the specific case you're after here.

 

It's use (INITGET) here would only be useful if you were attempting to prevent non-Nil, or even negative values being entered. Given the completeness of verifying that the user's input falls within the specified range, this is not necessary.

 

In any event, HTH

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...