-
|
How can I "continue" in a loop? For example, suppose I'm looping on asking the user for input. If the input is bad, I want to discontinue the loop and start it over again. Here's an example: #!/usr/bin/env janet
(defn get-user-input
[s]
(prin s)
(flush)
(string/trim (getline)))
(defn main
[args]
(while true
(def num (scan-number
(get-user-input "Enter a num: ")))
(when (< 100 num)
(print "Too high. Keep it under 100.")
(break)) # <-- how can I instead `continue` here?
(print "Ok, " num)))It was suggested to me to try (defn main
[args]
(loop [num :iterate (scan-number
(get-user-input "Num?: "))
:when (> 100 num)]
(print (string "Ok, you supplied " num "."))))but then how do I also print out the message asking the user to choose a number below 100? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
@uvtc How about using FWIW, I got the following sort of output in one trial: |
Beta Was this translation helpful? Give feedback.
-
|
So, after, while admittedly very tired, trying to play with |
Beta Was this translation helpful? Give feedback.
-
|
Thanks, @sogaiu and @yumaikas . In the chat, @bakpakin noted that I see that #!/usr/bin/env janet
(import ../janet-lib/john/io)
(defn main
[& args]
(while true
(label 'top
(def num (scan-number (io/get-user-input "Num? ")))
(when (< 100 num)
(print "Too high. Keep it under 100.")
(return 'top))
(print "ok. you entered " num))))As you (@sogaiu) pointed out in the chat, I see that |
Beta Was this translation helpful? Give feedback.
@uvtc How about using
promptwithreturn(I think this was another of @yumaikas' ideas) in the following manner:FWIW, I got the following sort of output in one trial: