Author Topic: Yes or No < Yes > :  (Read 4169 times)

0 Members and 1 Guest are viewing this topic.

Oak3s

  • Bull Frog
  • Posts: 321
Yes or No < Yes > :
« on: August 03, 2006, 05:50:07 PM »
ive searched and cant find an answer. how do you go about asking a yes or no question and only accept yes, no, y, or n?

"Freedom is not defined by safety. Freedom is Defined by the ability of citizens to live without government interference"
-- Ron Paul

T.Willey

  • Needs a day job
  • Posts: 5241
Re: Yes or No < Yes > :
« Reply #1 on: August 03, 2006, 05:53:34 PM »
Code: [Select]
(initget "Yes No")
(if (/= (setq Opt (getkword "\n Select option [<Y>es/No]: ")) "No")
 (setq Opt "Yes")
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Oak3s

  • Bull Frog
  • Posts: 321
Re: Yes or No < Yes > :
« Reply #2 on: August 03, 2006, 06:01:56 PM »
thanks for the reply. im going to look closer at it. i was just about to post what i had.
Code: [Select]
(defun c:Yesno (/ tmp)
(initget 1 "Yes No")
(setq tmp (getkword "Did you get it < Yes or No > "))
)

"Freedom is not defined by safety. Freedom is Defined by the ability of citizens to live without government interference"
-- Ron Paul

T.Willey

  • Needs a day job
  • Posts: 5241
Re: Yes or No < Yes > :
« Reply #3 on: August 03, 2006, 06:22:19 PM »
I like being able to hit enter, so I just test of the other option usually.  If it's not the other option, in this case "No", then it must be yes.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Oak3s

  • Bull Frog
  • Posts: 321
Re: Yes or No < Yes > :
« Reply #4 on: August 03, 2006, 06:46:28 PM »
Tim
what im seeing with your code is that if you hit n, or no you get nil. if you enter you get "yes".
but if you use the code below you can get yes or no...depending on your response. how would you go about getting yes or no...rather than nil when using a default yes as enter
Code: [Select]
(defun c:Yesno (/ tmp)
(initget 0 "Yes No")
(setq tmp (getkword "\nAre you sure? (Yes or No) < Yes >: "))

)

"Freedom is not defined by safety. Freedom is Defined by the ability of citizens to live without government interference"
-- Ron Paul

Oak3s

  • Bull Frog
  • Posts: 321
Re: Yes or No < Yes > :
« Reply #5 on: August 03, 2006, 06:48:32 PM »
ive tried this but cant seem to get it working
Code: [Select]
(defun c:Yesno (/ tmp)
(initget 0 "Yes No")
(setq tmp (getkword "\nAre you sure? (Yes or No) < Yes >: "))
(if (null tmp)
(setq tmp "Yes"))
)

"Freedom is not defined by safety. Freedom is Defined by the ability of citizens to live without government interference"
-- Ron Paul

T.Willey

  • Needs a day job
  • Posts: 5241
Re: Yes or No < Yes > :
« Reply #6 on: August 03, 2006, 06:55:56 PM »
ive tried this but cant seem to get it working
Code: [Select]
(defun c:Yesno (/ tmp)
(initget 0 "Yes No")
(setq tmp (getkword "\nAre you sure? (Yes or No) < Yes >: "))
(if (null tmp)
(setq tmp "Yes"))
)
Works here.
Quote

Command: yesno

Are you sure? (Yes or No) < Yes >:
"Yes"

Command:
Command: (c:yesno)

Are you sure? (Yes or No) < Yes >:
"Yes"
Well, if you hit "N" or any no response, it will return nil.  If you want to change that, then you will have to return the variable tmp.  Like
Code: [Select]
(defun c:Yesno (/ tmp)
(initget 0 "Yes No")
(setq tmp (getkword "\nAre you sure? (Yes or No) < Yes >: "))
(if (null tmp)
(setq tmp "Yes"))
tmp
)
Quote
Are you sure? (Yes or No) < Yes >: n
"No"

Command:
Command: yesno

Are you sure? (Yes or No) < Yes >:
"Yes"

Command:
Command: yesno

Are you sure? (Yes or No) < Yes >: y
"Yes"
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Oak3s

  • Bull Frog
  • Posts: 321
Re: Yes or No < Yes > :
« Reply #7 on: August 03, 2006, 07:12:55 PM »
ive tried this but cant seem to get it working
Code: [Select]
(defun c:Yesno (/ tmp)
(initget 0 "Yes No")
(setq tmp (getkword "\nAre you sure? (Yes or No) < Yes >: "))
(if (null tmp)
(setq tmp "Yes"))
)
Works here.
Quote

Command: yesno

Are you sure? (Yes or No) < Yes >:
"Yes"

Command:
Command: (c:yesno)

Are you sure? (Yes or No) < Yes >:
"Yes"
Well, if you hit "N" or any no response, it will return nil.....<snip>
also if you hit "y" or even "yes"...the only way i can get a "Yes" is if i hit enter. any other response is giving me nil.

"Freedom is not defined by safety. Freedom is Defined by the ability of citizens to live without government interference"
-- Ron Paul

T.Willey

  • Needs a day job
  • Posts: 5241
Re: Yes or No < Yes > :
« Reply #8 on: August 03, 2006, 07:31:23 PM »
I told you why in my other post, maybe you missed it.  The things with defuns, are they return the last thing (lack of better word) evaluated. So in yours, the last thing evaluated is the if statement.  If the statement was true
"No" would be returned, if it was false, then nil was returned.  That is why I said you have to pass your variable "tmp" at the end of the function.

Is that clear.

No grumpy tone implied, sorry if it comes off that way.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Yes or No < Yes > :
« Reply #9 on: August 03, 2006, 07:33:04 PM »
Perhaps try something like  this
.. note also the square brackets around the options ...  try a right click to display the popup options menu when the command is running, you'll see the options displayed .. which you can also click ..
Quote
(DEFUN c:Yesno (/ return)
    (INITGET 0 "Yes No")
    (OR (SETQ return (GETKWORD "\nAre you sure? [Yes/No] < Yes >: "))
        (SETQ return "Yes")
    )
    return
)
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

MP

  • Seagull
  • Posts: 17662
  • Have thousands of dwgs to process? Contact me.
Re: Yes or No < Yes > :
« Reply #10 on: August 03, 2006, 10:51:55 PM »
Just for fun a different spin ...

I originally came from a BASIC programming background (shoot me now), so when I took on LISP I found it annoying there wasn't an equivelent to the INKEY$ function (read a keyboard character from the keyboard buffer without requiring the user to press [Enter] and without echoing it to the screen).

What to do but write my own.

This is a simple variant that doesn't faithfully replicate said function (the original function left the character in the buffer and could deal with extended keyboard codes etc. IIRC) but it's good enough for a simple Yes/No application. If you execute the function it will wait until you press one of the standard keyboard keys: a-z, A-Z, 0-9, Spacebar, Enter, Tab keys etc. but not extended codes: <F1> key etc., returning the corresponding string, like "a", "A", "0", " ", "\n" or "\t" etc.

Anyway, without further blather --

Code: [Select]
(defun GetKey ( / result )
    (while
        (null
            (eq 2
                (car
                    (setq result (grread))
                )
            )
        )
    )
    (chr (cadr result))
)

How to use? An simple example proggy --

Code: [Select]
(defun c:Foo ( / response )

    (princ "Do you really want to do it? Y/N: ")
   
    (while
        (null
            (member     
                (setq response (strcase (GetKey)))
               '("Y" "N")
            )
        )
    )
   
    (princ
        (strcat "User indicated <"
            (if (eq "Y" response)
                "Yes"
                "No"
            )
            ">."
        )
    )   

    (princ)

)

FWIW, cheers.
Engineering Technologist CAD Specialist Programmer Analyst
Design Drafting Document Control Automation.
cadanalyst@fastmail.com https://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10388
Re: Yes or No < Yes > :
« Reply #11 on: August 04, 2006, 12:15:13 AM »
And one more:
Code: [Select]
(defun c:yesno (/ return)
  (initget "Yes No")
  (cond ((getkword "\nQuestion: Yes/No/<Yes>:"))
        ("Yes")
  )
)
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10388
Re: Yes or No < Yes > :
« Reply #12 on: August 04, 2006, 12:19:12 AM »
OK just one more. :-)
Code: [Select]
(defun c:yesno (/ return)
  (initget "Yes No")
  (if (setq return (getkword "\nAre you sure? [Yes/No] < Yes >: "))
    return
    "Yes"
  )
)
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Serge J. Gianolla

  • Guest
Re: Yes or No < Yes > :
« Reply #13 on: August 04, 2006, 01:01:35 AM »
OK then, more versatile; one more from the ancient Maximising AutoCAD Rel. 10 the UserKeyword function:

Code: [Select]
;;;Copyright 1987 - Present New Riders Publications
;;;
;;;Needs bit, keyword(s), message and default.
      (defun ukword (bit kwd msg def / inp)
(if (and def (/= def "")) ;test for both nil and null string
  (setq msg (strcat "\n" msg "<" def ">: ") ;string'em  with default
bit (* 2 (fix (/ bit 2))) ;a default and no null bit code conflict so
  ) ;this reduces bit by 1 if odd, to allow null
  (if (= " " (substr msg (strlen msg) 1))
    (setq
      msg (strcat "\n" (substr msg 1 (1- (strlen msg))) ": ")
    )
    (setq msg (strcat "\n" msg ": ")) ;without default
  )
) ;if
(initget bit kwd) ;initialize the key words
(setq inp (getkword msg)) ;and use the GET command
(if inp
  inp
  def
) ;compare the results, return appropriate value
      );;;defun
use:
Code: [Select]
(setq
  answer (ukword 1
"Corner Merged Tee"
"Enter an option? [Corner/Merged cross/Tee] "
"Corner"
)
)

Edit by MP: I don't know if this code should be posted but I do know the Maximizing AutoCAD series of books was published by New Riders Publications starting in the late 80s, so I added their copyright note, though I guessed on the date. Attempting to find out if there was a "No posting" clause in the original code ...
« Last Edit: August 04, 2006, 09:15:05 AM by MP »

MP

  • Seagull
  • Posts: 17662
  • Have thousands of dwgs to process? Contact me.
Re: Yes or No < Yes > :
« Reply #14 on: August 04, 2006, 09:39:36 AM »
Fun variant Didge.

Is it possible to ask a different question, doesn't seem swamp worthy to me.

Edit: Thank you Didge, really appreciate it.

« Last Edit: August 04, 2006, 09:44:50 AM by MP »
Engineering Technologist CAD Specialist Programmer Analyst
Design Drafting Document Control Automation.
cadanalyst@fastmail.com https://cadanalyst.slack.com http://linkedin.com/in/cadanalyst