如果在公共Lisp程序中输入,则忽略某些字符

如果在公共Lisp程序中输入,则忽略某些字符,lisp,common-lisp,Lisp,Common Lisp,*更新了代码的格式。 早上好 我在自学Common Lisp,我遇到了一个程序实际运行的问题,除了如果输入了某个字符(“,”)作为用户输入。代码(非常简短)是: 如果我更新代码以捕获错误,如: (defun welcome () (format t "Agent logged in. Status: Active, License to Kill: Active.~%~%")) (defun getchoice3 () (let ((choice 1))

*更新了代码的格式。

早上好

我在自学Common Lisp,我遇到了一个程序实际运行的问题,除了如果输入了某个字符(“,”)作为用户输入。代码(非常简短)是:

如果我更新代码以捕获错误,如:


(defun welcome ()
  (format t "Agent logged in. Status: Active, License to Kill: Active.~%~%"))


(defun getchoice3 ()
  (let ((choice  1))
  (format t  "~%Enter your password:  ")
   (let ((response (read)))
      (cond ((equal response '007)
          (if (#'symbolp (response))
          (getchoice3))
      (format t "~%~%Welcome James Bond, 007 ~%")(welcome))
      (t (format t "~%~%Incorrect Response!~%~%") (getchoice3))))))
           
(getchoice3)


它仍然有效,并将返回(getchoice3)处理除“,”之外的任何符号。 如果我将其编码为显式查找符号:


(defun welcome ()
  (format t "Agent logged in. Status: Active, License to Kill: Active.~%~%"))


(defun getchoice3 ()
  (let ((choice  1))
    (format t  "~%Enter your password:  ")
    (let ((response (read)))
      (cond ((equal response '007)
         (if (/, (response))
         (getchoice3))
      (format t "~%~%Welcome James Bond, 007 ~%")(welcome))
      (t (format t "~%~%Incorrect Response!~%~%") (getchoice3))))))
           
(getchoice3)

它仍然在“,”符号上失败,并告诉我符号没有反引号

最终我希望程序接受任何字母或数字,但不接受任何符号,如果输入了,就忽略它并请求输入


有什么想法吗?谢谢大家!

您正在阅读文本,而不是Lisp代码/数据,因此您应该使用 而不是
.

读取用户输入的代码:

(format ...)
...
(read)                ; input would be a Lisp data, using Lisp syntax
应该是这样的:

(format ...)
...
(finish-output ...)  ; make sure output happens before waiting for input
(read-line ...)      ; input would be text, returned as a string
(这个答案有一个明显的错误,@ad absurdum发现了这个错误-非常感谢!)

(更新-新手错误现已纠正-再次感谢@ad荒谬!)

我感谢你的帮助,尤其是“荒谬广告”和“sds”

如果其他人需要查看正确的代码,只需进行一次更改,将(读取)更改为(读取行),正如他们建议的那样:


(defun welcome ()
  (format t "~% Status: Logged In   Agent ID: 007   License to Kill:Active     Messages from M: 0~%~%"))


(defun getchoice3 ()
  (let ((choice 1))
    (format t  "~%Enter your password:  ")
    (let ((response (read-line)))
      (cond ((string-equal response "007")
         (format t "~%~%Welcome James Bond, 007 ~%")(welcome))
        (t (format t "~%Incorrect Reponse.~%") (getchoice3))))))

(getchoice3)


再次感谢您的帮助

请修正你的缩进。您的代码现在无法读取。请不要在此处使用
READ
。除非您确切知道自己在做什么,否则使用
read
会让自己面临可怕的代码注入攻击<“代码>阅读”几乎从来都不是处理用户输入的正确方法。@我理解你的意思。我已经纠正了压痕。希望这样更好(正如我说的,我还在学习!)。另外,谢谢你们指出我应该使用的是阅读线——也谢谢你们只是在正确的方向上轻推我。我更喜欢这个答案,而不是代码@谢谢!我希望将用户输入限制为字母或数字,因为我注意到“,”符号导致程序崩溃。不过,谢谢你对里德的提醒。我会记住的!否,请使用Emacs自动缩进或读取为什么要使用
read
读取用户输入密码?这是个糟糕的主意,自找麻烦<代码>读取行是正确的解决方案,除非您还有其他未说明的目标。@adabsurdum我的目标是让程序接受用户输入,但如果输入了诸如“,”之类的字符,则不会崩溃。我知道,如果程序可以崩溃,它可以用来产生外壳和造成其他恶作剧。即使输入了“、或”或其他类型的代码,我发布的代码也会保持运行。我将删除以前的邮件,并改用“读取行”重试。谢谢。
readline
如果您输入逗号,您的程序不会“崩溃”;它用于获取一行输入,并返回一个字符串<代码>响应在您发布的代码中是一个字符串,因此您的比较应该类似于
(字符串等于响应“007”)

(defun welcome ()
  (format t "~% Status: Logged In   Agent ID: 007   License to Kill:Active     Messages from M: 0~%~%"))


(defun getchoice3 ()
  (let ((choice 1))
    (format t  "~%Enter your password:  ")
    (let ((response (read-line)))
      (cond ((string-equal response "007")
         (format t "~%~%Welcome James Bond, 007 ~%")(welcome))
        (t (format t "~%Incorrect Reponse.~%") (getchoice3))))))

(getchoice3)