简体   繁体   English

整数值不是Common Lisp中的数字?

[英]Integer Value is Not a Number in Common Lisp?

When I execute the following Common Lisp program by calling (play) , I get the error: Argument X is not a NUMBER: Guess 当我通过调用(play)执行以下Common Lisp程序时,我得到错误: Argument X is not a NUMBER: Guess

;;;; number-game.lisp
;;;;
;;;; Andrew Levenson
;;;; 10/25/2010
;;;;
;;;; Simple number guessing game. User has
;;;; five guesses to determine a number between
;;;; one and one hundred, inclusive (1-100).

;;; Set global variable for the target number:
(defparameter *target* nil) 

;;; Set the iterator so we may check the number of guesses
(defparameter *number-of-guesses* 0)

;;; Welcome the user
(defun welcome-user ()
    (format t "Welcome to the number guessing game!~%"))

;;; Prompt for a guess
(defun prompt-for-guess ()
    (format t "Please enter your guess (1-100): ")
    (finish-output nil) ; nil directs finish-output to standard IO
    (check-guess 'read-guess))

;;; Read in a guess
(defun read-guess ()
    (let ((guess (parse-integer(read-line *query-io*))))
        (if (numberp guess) ; If true, return guess. Else, call prompt-for-guess
            (progn
                (setq *number-of-guesses* (+ *number-of-guesses* 1))
                'guess)
            (prompt-for-guess))))

;;; Check if the guess is higher than, lower than, or equal to, the target
(defun check-guess (fn)
    (let ((guess (funcall fn)))
        (if (= guess *target*)
            (equal-to)
            (if (> guess *target*)
                (greater-than (guess))
                (if (< guess *target*)
                    (less-than (guess)))))))

;;; If the guess is equal to the target, the game is over
(defun equal-to ()
    (format t "Congratulations! You have guessed the target number, ~a!~%" *target*)
    (y-or-n-p "Play again? [y/n] "))

;;; If the guess is greater than the target, inform the player.
(defun greater-than (guess)
    (format t "Sorry, ~a is greater than the target.~%" guess)
    (if (< *number-of-guesses* 6)
        (prompt-for-guess)
        (game-over)))

;;; If the guess is less than the target, inform the player.
(defun less-than (guess)
    (format t "Sorry, ~a is less than the target.~%" guess)
    (if (< *number-of-guesses* 6)
        (prompt-for-guess)
        (game-over)))

;;; If the player has run out of guesses, give them the option
;;; of playing the game again.
(defun game-over ()
    (y-or-n-p "You have run out of guesses. Play again? [y/n] "))


;;; Play the game
(defun play ()
    ;; If it's their first time playing this session,
    ;; make sure to greet the user.
    (unless (> *number-of-guesses* 0)
        (welcome-user))
    ;; Reset their remaining guesses
    (setq *number-of-guesses* 0)
    ;; Set the target value
    (setq *target*
        ;; Random can return float values,
        ;; so we must round the result to get
        ;; an integer value.
        (round
            ;; Add one to the result, because
            ;; (random 100) yields a number between
            ;; 0 and 99, whereas we want a number
            ;; from 1 to 100 inclusive.
            (+ (random 100) 1)))
    (if (equal (prompt-for-guess) "y")
        (play)
        (quit)))

What am I doing wrong that guess is not a number value in check-guess ? 我猜错了guess不是check-guess数值?

Your read-guess function returns the symbol GUESS by using 'guess rather than the value of guess (ie without the single quote). read-guess函数返回符号GUESS通过'guess ,而不是价值guess (即不带单引号)。

I think you also have other issues, for example (greater-than (guess)) evaluates guess as a function. 我认为你还有其他问题,例如(greater-than (guess))评估猜测作为一个函数。 You will need to fix these also. 您还需要解决这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM