gsc Posted July 6, 2020 Posted July 6, 2020 Hi, I want to run this condition (nextstep) continuously if "Y" is true until "N" is true. (initget 1 "Y y N n") (setq nextstep (strcase (getkword "\nDo you want to continue with next step? [Y/N]?: "))) (cond ((= nextstep "Y") (initget 1 "P p N n") (setq nextpos (strcase (getkword "\nContinue after Previous position or start at New postition? [P/N]?: "))) (cond ((= nextpos "P") ;piece of code ) ((= nextpos "N") ;piece of code ) ) ) ((= nextstep "N") (princ "Drawing is finished!") ) ) How do I do that? Quote
Steven P Posted July 6, 2020 Posted July 6, 2020 This might help: https://www.afralisp.net/autolisp/tutorials/program-looping.php Use 'while' (while (= nextstep "Y") …. ) Quote
Lee Mac Posted July 6, 2020 Posted July 6, 2020 (edited) As Steven correctly indicates, a while loop is the way to go, however, since both prompts offer a binary choice, you can encode a default option with no additional code, e.g.: (while (progn (initget "Yes No") (/= "No" (getkword "\nDo you want to continue with the next step? [Yes/No] <Yes>: ")) ) (initget 1 "Previous New") (if (= "Previous" (getkword "\nContinue after the Previous position or start at a New postition? [Previous/New] <New>: ")) (princ "\nUser selected Previous.") (princ "\nUser selected New.") ) ) Edited July 7, 2020 by Lee Mac 1 Quote
gsc Posted July 7, 2020 Author Posted July 7, 2020 (edited) That is a nice one Lee! Where do I put the different lisp codes for New and Previous location then? I also figured out a way, but not so efficient. (while (or (initget 1 "Y y N n") (= (setq nextstep (strcase (getkword "\nDo you want to continue with next step? [Y/N]?: "))) "Y") ) (cond ((= nextstep "Y") (initget 1 "P p N n") (setq nextpos (strcase (getkword "\nContinue after Previous position or start at New postition? [P/N]?: "))) (cond ((= nextpos "P") (progn (princ "Previous Position is selected!") ) ) ((= nextpos "N") (progn (princ "New Position is selected!") ) ) ) ) ((= nextstep "N") (princ "Drawing is finished!") ) ) ) Edited July 7, 2020 by gsc Quote
Lee Mac Posted July 7, 2020 Posted July 7, 2020 (edited) 14 hours ago, gsc said: That is a nice one Lee! Where do I put the different lisp codes for New and Previous location then? Thank you - you would perform the relevant operations here: (while (progn (initget "Yes No") (/= "No" (getkword "\nDo you want to continue with the next step? [Yes/No] <Yes>: ")) ) (initget 1 "Previous New") (if (= "Previous" (getkword "\nContinue after the Previous position or start at a New postition? [Previous/New] <New>: ")) (progn ;; Do something if the user selects Previous ) (progn ;; Do something if the user selects New ) ) ) Edited July 7, 2020 by Lee Mac Quote
marko_ribar Posted July 7, 2020 Posted July 7, 2020 I don't know if I am right or wrong, but I suppose that if you stated in (initget 1 "Previous New") the following (getkword) will honor "Previous" and not just "P"... So I believe that it should actually be : (while (progn (initget "Yes No") (/= "No" (getkword "\nDo you want to continue with the next step? [Yes/No] <Yes>: ")) ) (initget 1 "Previous New") (if (= "Previous" (getkword "\nContinue after the Previous position or start at a New postition? [Previous/New] <New>: ")) (progn ;; Do something if the user selects Previous ) (progn ;; Do something if the user selects New ) ) ) Quote
Lee Mac Posted July 7, 2020 Posted July 7, 2020 (edited) 2 hours ago, marko_ribar said: I don't know if I am right or wrong, but I suppose that if you stated in (initget 1 "Previous New") the following (getkword) will honor "Previous" and not just "P"... It was my mistake - it should have been (= "Previous" ...) in my earlier posts (I have since corrected the code in my earlier posts) - thank you for bringing this to my attention. (initget 1 ...) will force the user to specify an option rather than being able to press Enter to accept the default option. Edited July 7, 2020 by Lee Mac Quote
gsc Posted July 8, 2020 Author Posted July 8, 2020 Thanx, this is working. I have only one other question. If I want to print a message after the user has selected "No", where (and how) do I write that? (progn (princ "The drawing is finished!") ) I have tried several positions, but the message always appears, even if Yes is selected Quote
BIGAL Posted July 8, 2020 Posted July 8, 2020 (edited) For me it may be easier to call a defun as next, rather than having a very deep multi cond running and trying to have lots of code inside the cond to balance. (defun yesyes () (initget 1 "P p N n") (setq nextpos (strcase (getkword "\nContinue after Previous position or start at New postition? [P/N]?: "))) (cond ((= nextpos "P")(princ "Previous Position is selected!")) ; call defun "previous" ((= nextpos "N") (princ "New Position is selected!")) ; call defun "newpos" ) ) ; starts here (initget "Yes No") (setq nextstep (getkword "\nDo you want to continue with the next step? [Yes/No] <Yes>: ")) (cond ((= nextstep "Yes")(yesyes)) ((= nextstep "No")(Alert "Drawing is finished")) ) or using ifs (initget "Yes No") (setq nextstep (getkword "\nDo you want to continue with the next step? [Yes/No] <Yes>: ")) (if (= nextstep "Yes") (progn (initget 1 "P p N n") (setq nextpos (strcase (getkword "\nContinue after Previous position or start at New postition? [P/N]?: "))) (if (= nextpos "P") (progn (princ "Previous Position is selected!") do your thing here ) (progn (princ "New Position is selected!") do your other thing here ) ) ) (Alert "Drawing is finished") ) Edited July 8, 2020 by BIGAL Quote
Lee Mac Posted July 8, 2020 Posted July 8, 2020 15 hours ago, gsc said: I have only one other question. If I want to print a message after the user has selected "No", where (and how) do I write that? (progn (princ "The drawing is finished!") ) I have tried several positions, but the message always appears, even if Yes is selected You would place this outside of the while loop entirely. Quote
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.