aloy Posted July 21, 2012 Share Posted July 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
MSasu Posted July 21, 2012 Share Posted July 21, 2012 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: ")) ) ) Quote Link to comment Share on other sites More sharing options...
Tharwat Posted July 21, 2012 Share Posted July 21, 2012 (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) ) Quote Link to comment Share on other sites More sharing options...
MSasu Posted July 21, 2012 Share Posted July 21, 2012 (edited) 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 July 22, 2012 by MSasu Fixed code to accept limits of interval too Quote Link to comment Share on other sites More sharing options...
BlackBox Posted July 21, 2012 Share Posted July 21, 2012 Also consider the INITGET function. Quote Link to comment Share on other sites More sharing options...
Tharwat Posted July 21, 2012 Share Posted July 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
MSasu Posted July 21, 2012 Share Posted July 21, 2012 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. Quote Link to comment Share on other sites More sharing options...
aloy Posted July 21, 2012 Author Share Posted July 21, 2012 Hi Msasu, Thanks very much. It works fine. Aloy Quote Link to comment Share on other sites More sharing options...
aloy Posted July 21, 2012 Author Share Posted July 21, 2012 Hi Tharwat, Thanks for the code. It works; needs a correction to the string to give the intended meaning. Regards, Aloy Quote Link to comment Share on other sites More sharing options...
aloy Posted July 21, 2012 Author Share Posted July 21, 2012 RenderMan, I thought 'INITGET' functions accept certain values, but not values within a defined range. Regards, Aloy Quote Link to comment Share on other sites More sharing options...
BlackBox Posted July 22, 2012 Share Posted July 22, 2012 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 Quote Link to comment Share on other sites More sharing options...
aloy Posted July 22, 2012 Author Share Posted July 22, 2012 RenderMan, You are correct. Thanks, Aloy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.