Guess the number/With feedback: Difference between revisions

m (→‎{{header|Nim}}: fix bogus markup)
 
(34 intermediate revisions by 17 users not shown)
Line 19:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V (target_min, target_max) = (1, 100)
 
print("Guess my target number that is between #. and #. (inclusive).\n".format(target_min, target_max))
Line 41:
I answer > target {print(‘ Too high.’)}
 
print("\nThanks for playing.")</langsyntaxhighlight>
 
{{out}}
Line 76:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
BYTE x,n,min=[1],max=[100]
 
Line 94:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Guess_the_number_with_feedback.png Screenshot from Atari 8-bit computer]
Line 109:
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Discrete_Random;
with Ada.Text_IO;
procedure Guess_Number_Feedback is
Line 159:
end loop;
Guess_Number (Lower_Limit, Upper_Limit);
end Guess_Number_Feedback;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<langsyntaxhighlight lang="algol68"># simple guess-the-number game #
 
main:(
Line 209:
OD;
print( ( "That's correct!", newline ) )
)</langsyntaxhighlight>
{{out}}
<pre>
Line 235:
=={{header|AppleScript}}==
 
<langsyntaxhighlight AppleScriptlang="applescript">-- defining the range of the number to be guessed
property minLimit : 1
property maxLimit : 100
Line 273:
end try
end repeat
end run</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">n: random 1 10
while ø [
try? [
num: to :integer input "Guess the number: " n
case [num]
when? [=n][print "\tWell Guessed! :)", exit]
when? [>n][print "\tHigher than the target..."]
when? [<n][print "\tLess than the target..."]
else []
]
else -> print "\tInvalid input!"
]</syntaxhighlight>
 
{{out}}
 
<pre>Guess the number: 3
Less than the target...
Guess the number: 9
Higher than the target...
Guess the number: something
Invalid input!
Guess the number: 5
Less than the target...
Guess the number: 7
Well Guessed! :)</pre>
 
=={{header|AutoHotkey}}==
 
<langsyntaxhighlight AutoHotkeylang="autohotkey">MinNum = 1
MaxNum = 99999999999
 
Line 307 ⟶ 335:
}
TotalTime := Round((A_TickCount - TotalTime) / 1000,1)
MsgBox, 64, Correct, The number %RandNum% was guessed in %Tries% tries, which took %TotalTime% seconds.</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f GUESS_THE_NUMBER_WITH_FEEDBACK.AWK
BEGIN {
Line 339 ⟶ 367:
exit(0)
}
</syntaxhighlight>
</lang>
 
=={{header|BASIC}}==
 
==={{header|Applesoft BASIC}}===
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic">100 L% = 1
110 U% = 10
120 N% = RND(1) * (U% - L% + 1) + L%
Line 357 ⟶ 384:
210 Q = G% = N%
220 NEXT
230 PRINT "THE GUESS WAS EQUAL TO THE TARGET."</langsyntaxhighlight>
 
==={{header|BASIC256}}===
{{works with|BASIC256 }}
<langsyntaxhighlight lang="basic256">Min = 5: Max = 15
Min = 5: Max = 15
chosen = int(rand*(Max-Min+1)) + Min
print "Guess a whole number between "+Min+" and "+Max
Line 375 ⟶ 401:
if guess = chosen then print "Well guessed!"
end if
until guess = chosen</syntaxhighlight>
</lang>
Output:(example)
<pre>Guess a whole number between 5 and 15
<pre>
Guess a whole number between 5 and 15
Enter your number 10
Sorry, your number was too high
Line 385 ⟶ 409:
Sorry, your number was too low
Enter your number 8
Well guessed!</pre>
 
</pre>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 cls
110 nmax = 20
120 chosen = int(rnd(1)*nmax)+1
130 print "Guess a whole number between 1 a ";nmax;chr$(10)
140 do
150 input "Enter your number: ",guess
160 if guess < n or guess > nmax then
170 print "That was an invalid number"
180 exit do
190 else
200 if guess < chosen then print "Sorry, your number was too low"
210 if guess > chosen then print "Sorry, your number was too high"
220 if guess = chosen then print "Well guessed!"
230 endif
240 loop until guess = chosen
250 end</syntaxhighlight>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Randomize
Dim guess As Integer, max As Integer = 20
Dim n As Integer = Int(Rnd * max) + 1
Print "Guess which number I've chosen in the range 1 to "; max; Chr(10)
Do
Print " Your guess : "
Input guess
If guess > n And guess <= 20 Then
Print "Your guess is higher than the chosen number, try again"
Else If guess = n Then
Print "Correct, well guessed!"
Break
Else If guess < n And guess >= 1 Then
Print "Your guess is lower than the chosen number, try again"
Else
Print "Your guess is inappropriate, try again"
End If
Loop
 
End</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
<syntaxhighlight lang="qbasic">100 CLS
110 RANDOMIZE 1
120 L = 20
130 X = INT(RND(1)*L)+1
140 PRINT "Guess a whole number between 1 a ";L;CHR$(10)
150 'do
160 INPUT "Enter your number: ", N
170 IF N < N OR N > L THEN PRINT "That was an invalid number" : GOTO 230
180 'else
190 IF N < X THEN PRINT "Sorry, your number was too low"
200 IF N > X THEN PRINT "Sorry, your number was too high"
210 IF N = X THEN PRINT "Well guessed!"
220 IF N <> X THEN GOTO 150
230 END</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Guess.bas"
110 RANDOMIZE
120 LET UP=10:LET LO=1 ! Limits
Line 405 ⟶ 492:
240 PRINT "Well guessed! Numner of tips:";COUNT
250 END SELECT
260 LOOP UNTIL NR=GU</langsyntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{trans|Tiny Craft Basic}}
<syntaxhighlight lang="qbasic">100 RANDOMIZE
110 LET T = 0
120 LET N = 20
130 LET R = INT(RND*N)+1
140 PRINT "GUESS A WHOLE NUMBER BETWEEN 1 AND";N
150 LET T = T+1
160 PRINT "ENTER YOUR NUMBER ";
170 INPUT G
180 IF G <> R THEN 210
190 PRINT "YOU GUESSED IT IN";T;"TRIES."
200 GOTO 310
210 IF G > R THEN 240
220 IF G < 1 THEN 240
230 PRINT "SORRY, YOUR NUMBER WAS TOO LOW"
240 IF G < R THEN 270
250 IF G > 100 THEN 270
260 PRINT "SORRY, YOUR NUMBER WAS TOO HIGH"
270 IF G >= 1 THEN 300
280 IF G <= 100 THEN 300
290 PRINT "THAT WAS AN INVALID NUMBER"
300 IF G <> R THEN 140
310 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
<syntaxhighlight lang="qbasic">100 CLS
120 L = 20
130 X = INT(RND(1)*L)+1
140 PRINT "Guess a whole number between 1 a ";L;CHR$(10)
150 'do
160 INPUT "Enter your number: "; N
170 IF N < N OR N > L THEN PRINT "That was an invalid number" : GOTO 230
180 'else
190 IF N < X THEN PRINT "Sorry, your number was too low"
200 IF N > X THEN PRINT "Sorry, your number was too high"
210 IF N = X THEN PRINT "Well guessed!"
220 IF N <> X THEN GOTO 150
230 END</syntaxhighlight>
 
==={{header|QB64}}===
Note that <code>INPUT</code> only allows the user to type things that can fit in the variable it's storing to. Since we're storing to a byte, we don't have to worry about the user typing any non-numbers.
<langsyntaxhighlight lang="qbasic">DIM secretNumber AS _BYTE ' the secret number
DIM guess AS _BYTE ' the player's guess
 
Line 428 ⟶ 556:
END SELECT
LOOP UNTIL guess%% = secretNumber%%
PRINT "You won!"; secretNumber%%; "was the secret number!"</langsyntaxhighlight>
{{out}}
<pre>The computer has chosen a secret number between 1 and 10.
<pre>
The computer has chosen a secret number between 1 and 10.
What is your guess? -1
Please enter a number between 1 and 10!
Line 445 ⟶ 572:
Your guess is HIGHER than the target.
What is your guess? 2
You won! 2 was the secret number!</pre>
 
</pre>
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">CLS
RANDOMIZE TIMER ' set a seed based on system time
nmax = 20
chosen = INT(RND * nmax) + 1
 
PRINT "Guess a whole number between 1 a"; nmax; CHR$(10)
DO
INPUT "Enter your number"; guess
IF guess < n OR guess > nmax THEN
PRINT "That was an invalid number"
EXIT DO
ELSE
IF guess < chosen THEN PRINT "Sorry, your number was too low"
IF guess > chosen THEN PRINT "Sorry, your number was too high"
IF guess = chosen THEN PRINT "Well guessed!"
END IF
LOOP UNTIL guess = chosen
END</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="vb">nmin = 5
nmax = 15
chosen = int( rnd( 1) * (nmax-nmin+1)) +nmin
print "Guess a whole number between "; nmin; " and "; nmax
[loop]
input "Enter your number "; guess
if guess < nmin or guess > nmax then
print "That was an invalid number"
end
else
if guess < chosen then print "Sorry, your number was too low"
if guess > chosen then print "Sorry, your number was too high"
if guess = chosen then print "Well guessed!": end
end if
if guess <> chosen then [loop]</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">RANDOMIZE
LET nmax = 20
LET chosen = int(rnd*nmax)+1
PRINT "Guess a whole number between 1 a"; nmax; chr$(10)
DO
INPUT prompt "Enter your number ": guess
IF guess < n or guess > nmax then
PRINT "That was an invalid number"
EXIT DO
ELSE
IF guess < chosen then PRINT "Sorry, your number was too low"
IF guess > chosen then PRINT "Sorry, your number was too high"
IF guess = chosen then PRINT "Well guessed!"
END IF
LOOP until guess = chosen
END</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">nmin = 5
nmax = 15
chosen = int(ran(nmax-nmin+1)) + nmin
print "Guess a whole number between ", nmin, " and ", nmax
repeat
input "Enter your number " guess
if guess < nmin or guess > nmax then
print "That was an invalid number"
end
else
if guess < chosen print "Sorry, your number was too low"
if guess > chosen print "Sorry, your number was too high"
if guess = chosen print "Well guessed!"
fi
until guess = chosen</syntaxhighlight>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">@echo off
 
:A
Line 465 ⟶ 668:
echo You won! The number was %number%
pause>nul
goto A</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> Min% = 1
Max% = 10
chosen% = RND(Max%-Min%+1) + Min% - 1
Line 486 ⟶ 689:
ENDCASE
UNTIL FALSE
END</langsyntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
static $( randstate = ? $)
 
Line 531 ⟶ 734:
wrch('*N')
play(min, max, rand(min, max))
$)</langsyntaxhighlight>
{{out}}
<pre>Guess the number
Line 559 ⟶ 762:
The number range is hardcoded at the start of the program (<tt>1:"d"</tt> being 1 to 100), but can easily be changed.
 
<langsyntaxhighlight lang="befunge">1:"d">>048*"neewteb rebmun a sseuG">:#,_$\:.\"d na",,\,,:.55+,\-88+0v
<*2\_$\1+%+48*">",,#v>#+:&#>#5-:!_0`00p0"hgih"00g>_0"wol"00g!>_48vv1?
\1-:^v"d correctly!"<^,,\,+55,". >"$_,#!>#:<"Your guess was too"*<>+>
:#,_@>"ess" >"eug uoY"></langsyntaxhighlight>
 
{{out}}
Line 576 ⟶ 779:
 
=={{header|Brat}}==
<langsyntaxhighlight lang="brat">number = random 10
 
p "Guess a number between 1 and 10."
Line 592 ⟶ 795:
}
}
}</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <time.h>
Line 609 ⟶ 812:
 
printf( "Guess the number between %d and %d: ", lower_limit, upper_limit );
fflush(stdout); // Flush the output buffer
 
while( scanf( "%d", &guess ) == 1 ){
Line 619 ⟶ 823:
 
return 0;
}</langsyntaxhighlight>
 
Demonstration:
Line 638 ⟶ 842:
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
 
class Program
Line 677 ⟶ 881:
}
}
</syntaxhighlight>
</lang>
Output:
<pre>The number is between 1 and 10. Make a guess: 1
Line 696 ⟶ 900:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdlib>
#include <ctime>
Line 723 ⟶ 927:
 
return 0;
}</langsyntaxhighlight>
Output:
<pre>Enter lower limit: 1
Line 743 ⟶ 947:
 
=={{header|Caché ObjectScript}}==
<langsyntaxhighlight Cachélang="caché ObjectScriptobjectscript">GUESSNUM
; get a random number between 1 and 100
set target = ($random(100) + 1) ; $r(100) gives 0-99
Line 771 ⟶ 975:
write !!,"You guessed the number in "_tries_" attempts."
quit</langsyntaxhighlight>
 
{{out}}<pre>SAMPLES>do ^GUESSNUM^
Line 796 ⟶ 1,000:
In you module.ceylon file put import ceylon.random "1.3.1";
 
<langsyntaxhighlight lang="ceylon">import ceylon.random {
DefaultRandom
}
Line 832 ⟶ 1,036:
}
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn guess-run []
(let [start 1
end 100
Line 850 ⟶ 1,054:
:else true)
(println "Correct")
(recur (inc i)))))))</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">read_number = proc (prompt: string) returns (int)
po: stream := stream$primary_output()
pi: stream := stream$primary_input()
Line 908 ⟶ 1,112:
secret: int := min + random$next(max - min + 1)
play_game(min, max, secret)
end start_up</langsyntaxhighlight>
{{out}}
<pre>Guess the number
Line 931 ⟶ 1,135:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Guess-With-Feedback.
 
Line 960 ⟶ 1,164:
GOBACK
.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun guess-the-number-feedback (&optional (min 1) (max 100))
(let ((num-guesses 0)
(num (+ (random (1+ (- max min))) min))
Line 979 ⟶ 1,183:
(= guess num)))
(format t "You got the number correct on the ~:r guess!~%" num-guesses)))
</syntaxhighlight>
</lang>
Output:
<pre>CL-USER> (guess-the-number-feedback 1 1024)
Line 1,001 ⟶ 1,205:
You got the number correct on the eighth guess!
</pre>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">let n = int(rnd * 100) + 1
 
do
 
let t = t + 1
 
input "guess the number between 1 and 100", g
 
if g < n then
 
alert "guess higher"
 
endif
 
if g > n then
 
alert "guess lower"
 
endif
 
loop g <> n
 
alert "you guessed the number in ", t, " tries"</syntaxhighlight>
 
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">number = rand(1..10)
 
puts "Guess the number between 1 and 10"
Line 1,022 ⟶ 1,251:
puts "Please enter an integer."
end
end</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.typecons, std.conv, std.string,
std.range;
 
Line 1,056 ⟶ 1,285:
writeln(answer < target ? " Too low." : " Too high.");
}
}</langsyntaxhighlight>
Sample game:
<pre>Guess my target number that is between 1 and 100 (inclusive).
Line 1,080 ⟶ 1,309:
 
=={{header|DCL}}==
<langsyntaxhighlight DCLlang="dcl">$ rnd = f$extract( 21, 2, f$time() )
$ count = 0
$ loop:
Line 1,094 ⟶ 1,323:
$ if guess .gt. rnd then $ write sys$output "too large"
$ if guess .ne. rnd then $ goto loop
$ write sys$output "it only took you ", count, " guesses"</langsyntaxhighlight>
{{out}}
<pre>$ @guess
Line 1,116 ⟶ 1,345:
=={{header|Delphi}}==
 
<langsyntaxhighlight Delphilang="delphi">program GuessTheNumber;
 
{$APPTYPE CONSOLE}
Line 1,201 ⟶ 1,430:
 
end.
</syntaxhighlight>
</lang>
 
=={{header|EasyLang}}==
<syntaxhighlight>
<lang>print "Guess a number between 1 and 100!"
print "Guess a number between 1 and 100!"
n = random 100 + 1
n = randint 100
repeat
g = number input
write g
if error = 1
print "You must enter a number!"
elif g > n
print " is too high"
elif g < n
print " is too low"
.
until g = n
.
print " is correct"</lang>
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="lisp">
;;(read <default-value> <prompt>) prompts the user with a default value using the browser dialog box.
;; we play sounds to make this look like an arcade game
Line 1,239 ⟶ 1,470:
(play-sound 'ok )
" 🔮 Well played!! 🍒 🍇 🍓")
</syntaxhighlight>
</lang>
 
=={{header|Ela}}==
 
<langsyntaxhighlight lang="ela">open string datetime random core monad io
 
guess () = do
Line 1,278 ⟶ 1,509:
ask ()
 
guess () ::: IO</langsyntaxhighlight>
 
=={{header|Elena}}==
ELENA 46.x :
<langsyntaxhighlight lang="elena">import extensions;
public program()
{
int randomNumber := randomGenerator.evalnextInt(1,10);
console.printLine("I'm thinking of a number between 1 and 10. Can you guess it?");
bool numberCorrect := false;
Line 1,307 ⟶ 1,538:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,321 ⟶ 1,552:
=={{header|Elixir}}==
{{works with|Elixir|1.2}}
<langsyntaxhighlight lang="elixir">defmodule GuessingGame do
def play(lower, upper) do
play(lower, upper, Enum.random(lower .. upper))
Line 1,340 ⟶ 1,571:
end
GuessingGame.play(1, 100)</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">(let* ((min 1)
<lang Lisp>
(let ((num (1+ (randommax 100))))
(number (+ (random (1+ (- max min))) min))
(princ "Guess the no: ")
guess done)
(loop
(message "Guess the number between %d and %d" min max)
(setq guess (read))
(while (condnot done)
((not (numberpsetq guess)) (printread-number "Please enter a number "))
(cond
((eq guess num)
((< guess number)
(progn (princ-list "Guess was right! " num)
(returnmessage "Too low!")))
((> guess numnumber)
(printmessage "Too Highhigh!"))
((<= guess numnumber)
(printsetq "Too low!"))) )done t)</lang>
(message "Well guessed!")))))</syntaxhighlight>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(guess_number).
-export([main/0]).
Line 1,386 ⟶ 1,618:
guess(N)
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>1> c(guess_number).
Line 1,406 ⟶ 1,638:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">include get.e
 
constant lower_limit = 0, upper_limit = 100
Line 1,424 ⟶ 1,656:
puts(1,"You guessed to low.\nTry again: ")
end if
end while</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
open System
 
Line 1,458 ⟶ 1,690:
0
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING:
formatting
fry
Line 1,486 ⟶ 1,718:
[ unparse "Number in range %s, your guess?\n" printf flush ]
[ random '[ _ game-step ] loop ]
bi ;</langsyntaxhighlight>
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 1,527 ⟶ 1,759:
}
}
</syntaxhighlight>
</lang>
 
Sample game:
Line 1,555 ⟶ 1,787:
 
=={{header|FOCAL}}==
<langsyntaxhighlight FOCALlang="focal">01.01 S T=0
01.02 A "LOWER LIMIT",L
01.03 A "UPPER LIMIT",H
Line 1,569 ⟶ 1,801:
01.16 T "TOO LOW!",!;G 1.1
01.17 T "CORRECT! GUESSES",%4,T,!;Q
01.18 T "TOO HIGH!",!;G 1.1</langsyntaxhighlight>
 
{{out}}
Line 1,592 ⟶ 1,824:
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">program Guess_a_number
implicit none
Line 1,618 ⟶ 1,850:
end if
end do
end program</langsyntaxhighlight>
Output
<pre>I have chosen a number bewteen 1 and 100 and you have to try to guess it.
Line 1,636 ⟶ 1,868:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Randomize
Line 1,657 ⟶ 1,889:
End If
Loop
End</langsyntaxhighlight>
 
Sample input/output
Line 1,673 ⟶ 1,905:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">// Guess a Number with feedback.
target = random[1,100] // Min and max are both inclusive for the random function
guess = 0
Line 1,693 ⟶ 1,925:
println["$guess is correct! Well guessed!"]
}
</syntaxhighlight>
</lang>
{{out}}
Including an example with a non-integer entered.
Line 1,704 ⟶ 1,936:
34 is correct! Well guessed!
</pre>
 
=={{header|FTCBASIC}}==
<syntaxhighlight lang="basic">rem Hilo number guessing game
rem compile with FTCBASIC to 704 bytes com program file
 
use time.inc
use random.inc
 
define try = 0, tries = 0, game = 0
 
gosub systemtime
 
let randseed = loworder
let randlimit = 100
 
do
 
gosub intro
gosub play
gosub continue
 
loop try = 0
 
end
 
sub intro
 
bell
cls
print "Hilo game"
crlf
 
return
 
sub play
 
gosub generaterand
 
+1 rand
0 tries
let game = 1
 
print "Guess the number between 1 and 100"
crlf
 
do
 
+1 tries
 
input try
crlf
 
if try = rand then
 
let game = 0
 
print "You guessed the number!"
print "Tries: " \
print tries
 
endif
 
if game = 1 then
 
if try > rand then
 
print "Try lower..."
 
endif
 
if try < rand then
 
print "Try higher..."
 
endif
 
endif
 
crlf
 
loop game = 1
 
return
 
sub continue
 
print "Enter 0 to play again or 1 to EXIT to DOS"
input try
 
return</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
Short n // We'll just do 1..10 to get the idea
 
void local fn BuildInterface
Short i
window 1, @"Guess the number", ( 0, 0, 340, 120 )
for i = 1 to 10
button i,,, fn StringWithFormat(@"%d", i), ( 26 * i, 60, 40, 22 )
ButtonSetBordered( i, No )
next
button 11,,, @"Quit", ( 38, 10, 95, 32 )
button 12,,, @"Again", ( 200, 10, 95, 32 )
textlabel 13, @"Guess the number:", ( 112, 85, 200, 22 )
textlabel 14,, ( 158, 30, 100, 22 ) // Hints here
filemenu 1 : menu 1, -1, No // Build but disable File menu
editmenu 2 : menu 2, -1, No // Build but disable Edit menu
end fn
 
void local fn newGame
CFRange r = fn RangeMake( 1, 10 )
ControlRangeSetEnabled( r, Yes ) // Enable number buttobns
button 11, No // Disable Quit button
button 12, No // Disable Again button
n = rnd( 10 ) // Choose a random number
textlabel 14, @"🔴" // Not found yet
end fn
 
void local fn DoDialog( evt as Long, tag as Long )
CFRange r = fn RangeMake( 1, 10 )
select evt
case _btnClick
button tag, No
select tag
case 11 : end // Quit
case 12 : fn newGame // Again
case n : textlabel 14, @"🟢" // Found
ControlRangeSetEnabled( r, No )
button 11, Yes
button 12, Yes
case < n : textlabel 14, @"➡️"
case > n : textlabel 14, @"⬅️"
end select
case _windowWillClose : end
end select
end fn
 
fn buildInterface
fn newGame
on dialog fn DoDialog
handleevents
 
</syntaxhighlight>
 
 
=={{header|Genie}}==
<langsyntaxhighlight lang="genie">[indent=4]
/*
Number guessing with feedback, in Genie
Line 1,756 ⟶ 2,133:
init
var game = new NumberGuessing(1, 100)
game.start()</langsyntaxhighlight>
 
{{out}}
Line 1,794 ⟶ 2,171:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,822 ⟶ 2,199:
}
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Based on the Java implementation
<langsyntaxhighlight lang="groovy">
def rand = new Random() // java.util.Random
def range = 1..100 // Range (inclusive)
Line 1,847 ⟶ 2,224:
}
}
</syntaxhighlight>
</lang>
Example:
<syntaxhighlight lang="text">
The number is in 1..100
Guess the number: ghfvkghj
Line 1,865 ⟶ 2,242:
Guess the number: 92
Your guess is spot on!
</syntaxhighlight>
</lang>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">
import Control.Monad
import System.Random
Line 1,892 ⟶ 2,269:
putStrLn "Try to guess my secret number between 1 and 100."
ask `until_` answerIs ans
</syntaxhighlight>
</lang>
 
=={{header|HolyC}}==
<langsyntaxhighlight lang="holyc">U8 n, *g;
U8 min = 1, max = 100;
 
Line 1,914 ⟶ 2,291:
if (Str2I64(g) > n)
Print("Your guess was too high.\nTry again: ");
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
 
<syntaxhighlight lang="icon">
<lang Icon>
procedure main()
smallest := 5
Line 1,939 ⟶ 2,316:
}
end
</syntaxhighlight>
</lang>
 
Output:
Line 1,957 ⟶ 2,334:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">require 'misc'
game=: verb define
assert. y -: 1 >. <.{.y
Line 1,967 ⟶ 2,344:
smoutput (*x-n){::'You win.';'Too high.';'Too low.'
end.
)</langsyntaxhighlight>
 
Note: in computational contexts, J programmers typically avoid loops. However, in contexts which involve progressive input and output and where event handlers are too powerful (too complicated), loops are probably best practice.
Line 1,973 ⟶ 2,350:
Example use:
 
<syntaxhighlight lang="text"> game 100
Guess my integer, which is bounded by 1 and 100
Guess: 64
Line 1,984 ⟶ 2,361:
Too low.
Guess: 44
You win.</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight Javalang="java">import java.util.Random;
import java.util.Scanner;
public class Main
Line 2,014 ⟶ 2,391:
} while (guessedNumber != randomNumber);
}
}</langsyntaxhighlight>
Demonstration:
<pre>The number is between 1 and 100.
Line 2,031 ⟶ 2,408:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="html4strict"><p>Pick a number between 1 and 100.</p>
<form id="guessNumber">
<input type="text" name="guess">
Line 2,037 ⟶ 2,414:
</form>
<p id="output"></p>
<script type="text/javascript"></langsyntaxhighlight>
<langsyntaxhighlight lang="javascript">var number = Math.ceil(Math.random() * 100);
function verify() {
Line 2,058 ⟶ 2,435:
}
 
document.getElementById('guessNumber').onsubmit = verify;</langsyntaxhighlight>
<syntaxhighlight lang ="html4strict"></script></langsyntaxhighlight>
 
=== Spidermonkey Version ===
<langsyntaxhighlight lang="javascript">#!/usr/bin/env js
 
function main() {
Line 2,106 ⟶ 2,483:
 
main();
</syntaxhighlight>
</lang>
 
Example session:
Line 2,122 ⟶ 2,499:
Your guess: 57
You got it in 6 tries.
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
 
In the following, a time-based PRNG is used as that is sufficient for
the task. For a different PRNG, see e.g. [[Guess_the_number#jq]]
<syntaxhighlight lang=jq>
# $min and $max can be specified on the command line but for illustrative purposes:
def minmax: [0, 20];
 
# low-entropy PRNG in range($a;$b) i.e. $a <= prng < $b
# (no checking)
def prng($a;$b): $a + ((now * 1000000 | trunc) % ($b - $a) );
 
def play:
# extract a number if possible
def str2num:
try tonumber catch null;
 
minmax as [$min, $max]
| prng($min; $max) as $number
| "The computer has chosen a whole number between \($min) and \($max) inclusive.",
"Please guess the number or type q to quit:",
(label $out
| foreach inputs as $guess (null;
if $guess == "q" then null, break $out
else ($guess|str2num) as $g
| if $g == null or $g < 0 then "Please enter a non-negative integer or q to quit"
elif $g < $min or $g > $max then "That is out of range. Try again"
elif $g > $number then "Too high"
elif $g < $number then "Too low"
else true, break $out
end
end)
| if type == "string" then .
elif . == true then "Spot on!", "Let's start again.\n", play
else empty
end );
 
play
</syntaxhighlight>
'''Transcript:'''
<pre>
$ jq -nRr -f guess-the-number-with-feedback.jq
The computer has chosen a whole number between 0 and 20 inclusive.
Please guess the number or type q to quit:
10
Too low
16
Too high
14
Too high
12
Too high
11
Spot on!
Let's start again.
 
The computer has chosen a whole number between 0 and 20 inclusive.
Please guess the number or type q to quit:
19
Too high
q
Please enter a non-negative integer or q to quit
q
$</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function guesswithfeedback(n::Integer)
number = rand(1:n)
print("I choose a number between 1 and $n\nYour guess? ")
Line 2,140 ⟶ 2,584:
end
 
guesswithfeedback(10)</langsyntaxhighlight>
 
{{out}}
Line 2,152 ⟶ 2,596:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="kotlin">import kotlin.random.Random
 
fun main() {
Line 2,167 ⟶ 2,611:
}
}
}</langsyntaxhighlight>
Sample inout/output
{{out}}
Line 2,184 ⟶ 2,628:
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
<lang Scheme>
{def game
 
Line 2,206 ⟶ 2,650:
 
{game {pow 2 32}} // 2**32 = 4294967296
</syntaxhighlight>
</lang>
 
Sample inout/output
Line 2,247 ⟶ 2,691:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">#!/usr/bin/lasso9
 
local(
Line 2,279 ⟶ 2,723:
#status = true
}
}</langsyntaxhighlight>
 
With range value 8 and 73. Correct number 13
Line 2,295 ⟶ 2,739:
 
=={{header|LFE}}==
<langsyntaxhighlight lang="lisp">
(defmodule guessing-game
(export (main 0)))
Line 2,317 ⟶ 2,761:
(: random uniform 10)
(get-player-guess)))
</syntaxhighlight>
</lang>
 
From the LFE REPL (assuming the above code was saved in the file "guessing-game.lfe"):
 
<langsyntaxhighlight lang="lisp">
> (slurp '"guessing-game.lfe")
#(ok guessing-game)
Line 2,336 ⟶ 2,780:
ok
>
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
[start]
target = int( rnd( 1) * 100) +1
Line 2,356 ⟶ 2,800:
if c >target then print " Your guess was too high."
wend
</syntaxhighlight>
</lang>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">command guessTheNumber lowN highN
local tNumber, tguess, tmin, tmax
if lowN is empty or lowN < 1 then
Line 2,387 ⟶ 2,831:
end if
end repeat
end guessTheNumber</langsyntaxhighlight>
Test
<langsyntaxhighlight LiveCodelang="livecode">command testGuessNumber
guessTheNumber --defaults to 1-10
guessTheNumber 9,12
end testGuessNumber</langsyntaxhighlight>
 
=={{header|Locomotive Basic}}==
 
<langsyntaxhighlight lang="locobasic">10 CLS:RANDOMIZE TIME
20 PRINT "Please specify lower and upper limits":guess=0
30 INPUT " (must be positive integers) :", first, last
Line 2,408 ⟶ 2,852:
110 INPUT "That's correct! Another game (y/n)? ", yn$
120 IF yn$="y" THEN 20
</syntaxhighlight>
</lang>
 
Output:
Line 2,416 ⟶ 2,860:
=={{header|Logo}}==
{{trans|UNIX Shell}}
<langsyntaxhighlight lang="logo">to guess [:max 100]
local "number
make "number random :max
Line 2,441 ⟶ 2,885:
]
end
</syntaxhighlight>
</lang>
 
Sample run:<pre>? guess
Line 2,458 ⟶ 2,902:
=={{header|Lua}}==
 
<langsyntaxhighlight Lualang="lua">math.randomseed(os.time())
me_win=false
my_number=math.random(1,10)
Line 2,480 ⟶ 2,924:
end
end
</syntaxhighlight>
</lang>
 
<pre>
Line 2,498 ⟶ 2,942:
=={{header|M2000 Interpreter}}==
{{trans|BASIC256}}
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module GuessNumber {
Read Min, Max
Line 2,526 ⟶ 2,970:
}
GuessNumber 5, 15
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,532 ⟶ 2,976:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">GuessANumber := proc(low, high)
local number, input;
randomize():
Line 2,548 ⟶ 2,992:
end if;
end do:
end proc:</langsyntaxhighlight>
<syntaxhighlight lang Maple="maple">GuessANumber(2,5);</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">guessnumber[min_, max_] :=
Module[{number = RandomInteger[{min, max}], guess},
While[guess =!= number,
Line 2,561 ⟶ 3,005:
ToString@max <> "."]]];
CreateDialog[{"Well guessed!", DefaultButton[]}]];
guessnumber[1, 10]</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
{{untested|Octave}}
Tested in MATLAB. Untested in Octave.
<langsyntaxhighlight MATLABlang="matlab">function guess_a_number(low, high)
 
if nargin < 1 || ~isnumeric(low) || length(low) > 1 || isnan(low)
Line 2,591 ⟶ 3,035:
gs = '';
end
end</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
<lang MAXScript>
Range = [1,100]
randomNumber = (random Range.x Range.y) as integer
Line 2,610 ⟶ 3,054:
)
)
</syntaxhighlight>
</lang>
 
Output:
<syntaxhighlight lang="maxscript">
<lang MAXSCRIPT>
Enter a number between 1 and 100: 5
Too low!
Line 2,627 ⟶ 3,071:
Well guessed!
OK
</syntaxhighlight>
</lang>
 
=={{header|Mirah}}==
<langsyntaxhighlight lang="mirah">def getInput:int
s = System.console.readLine()
Integer.parseInt(s)
Line 2,655 ⟶ 3,099:
puts "Please enter an integer."
end
end</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE guessf;
 
IMPORT InOut, Random, NumConv, Strings;
Line 2,697 ⟶ 3,141:
InOut.WriteString ("Thank you for playing; have a nice day!");
InOut.WriteLn
END guessf.</langsyntaxhighlight>
<pre>I have chosen a number below 1000; please try to guess it.
Enter your guess : 500
Line 2,712 ⟶ 3,156:
 
=={{header|Nanoquery}}==
<langsyntaxhighlight Nanoquerylang="nanoquery">import Nanoquery.Util
 
random = new(Random)
Line 2,748 ⟶ 3,192:
end
 
println "\nThanks for playing."</langsyntaxhighlight>
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">using System;
using System.Console;
 
Line 2,777 ⟶ 3,221:
} while (guess != secret)
}
}</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,842 ⟶ 3,286:
 
return
</syntaxhighlight>
</lang>
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">; guess-number-feedback.lsp
; oofoe 2012-01-19
; http://rosettacode.org/wiki/Guess_the_number/With_feedback
Line 2,872 ⟶ 3,316:
(println "\nWell guessed! Congratulations!")
 
(exit)</langsyntaxhighlight>
 
Sample output:
Line 2,890 ⟶ 3,334:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import random, strutils
 
randomize()
Line 2,912 ⟶ 3,356:
else: echo " Ye-Haw!!"
 
echo "Thanks for playing."</langsyntaxhighlight>
 
=={{header|NS-HUBASIC}}==
<langsyntaxhighlight NSlang="ns-HUBASIChubasic">10 NUMBER=RND(10)+1
20 INPUT "I'M THINKING OF A NUMBER BETWEEN 1 AND 10. WHAT IS IT? ",GUESS
30 IF GUESS>NUMBER THEN PRINT "MY NUMBER IS LOWER THAN THAT.": GOTO 20
40 IF GUESS<NUMBER THEN PRINT "MY NUMBER IS HIGHER THAN THAT.": GOTO 20
50 PRINT "THAT'S THE CORRECT NUMBER."</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">use IO;
 
bundle Default {
Line 2,956 ⟶ 3,400:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let rec _read_int() =
try read_int()
with _ ->
Line 2,994 ⟶ 3,438:
loop ()
in
loop ()</langsyntaxhighlight>
 
Playing the game:
Line 3,027 ⟶ 3,471:
 
=={{header|Octave}}==
<langsyntaxhighlight Octavelang="octave">function guess_a_number(low,high)
% Guess a number (with feedback)
% http://rosettacode.org/wiki/Guess_the_number/With_feedback
Line 3,053 ⟶ 3,497:
end
end
disp('Well guessed!')</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">import: console
 
: guessNumber(a, b)
Line 3,067 ⟶ 3,511:
g n == ifTrue: [ "You found it !" .cr return ]
g n < ifTrue: [ "Less" ] else: [ "Greater" ] . "than the target" .cr
again ;</langsyntaxhighlight>
 
=={{header|Ol}}==
<syntaxhighlight lang="ol">
<lang ol>
(import (otus random!))
 
Line 3,097 ⟶ 3,541:
((= guess number)
(print "Well guessed!")))))
</syntaxhighlight>
</lang>
 
=={{header|ooRexx}}==
Line 3,107 ⟶ 3,551:
entering ? shows the number we are looking for
This program should, of course, also work with all other Rexxes
<syntaxhighlight lang="oorexx">
<lang ooRexx>
/*REXX program that plays the guessing (the number) game. */
low=1 /*lower range for the guessing game.*/
Line 3,164 ⟶ 3,608:
 
ser: say; say '*** error ! ***'; say arg(1); say; return
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">guess_the_number(N=10)={
a=random(N);
print("guess the number between 0 and "N);
Line 3,181 ⟶ 3,625:
);
print("You guessed it correctly")
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 3,187 ⟶ 3,631:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">sub prompt {
my $prompt = shift;
while (1) {
Line 3,205 ⟶ 3,649:
while ($_ = $tgt <=> prompt "Your guess");
 
print "Correct! You guessed it after $tries tries.\n";</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Guess_the_number3.exw
Line 3,246 ⟶ 3,690:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
<?php
 
Line 3,300 ⟶ 3,744:
</body>
</html>
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
{{trans|PureBasic}}
<langsyntaxhighlight PicoLisplang="picolisp">(de guessTheNumber ()
(use (Low High Guess)
(until
Line 3,322 ⟶ 3,766:
"Your guess is too "
(if (> Number Guess) "low" "high")
"." ) ) ) ) )</langsyntaxhighlight>
Output:
<pre>: (guessTheNumber)
Line 3,335 ⟶ 3,779:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">The low number is 1.
 
The high number is 100.
Line 3,362 ⟶ 3,806:
Convert the string to the number.
If the number is less than the low number, write " Guess can't be lower than " then the low number then "." on the console; repeat.
If the number is greater than the high number, write " Guess can't be higher than " then the high number then "." on the console; repeat.</langsyntaxhighlight>
{{out}}
<pre>
Line 3,390 ⟶ 3,834:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-Guess
{
Line 3,436 ⟶ 3,880:
 
Write-Host ("The number was {0} and it took {1} guesses to find it." -f $answer.Number, $answer.Guesses.Count)
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,461 ⟶ 3,905:
{{works with|SWI-Prolog|6}}
 
<langsyntaxhighlight lang="prolog">main :-
play_guess_number.
 
Line 3,497 ⟶ 3,941:
; Guess > N -> writeln('Your guess is too high.')
; Guess =:= N -> writeln("Correct!")
).</langsyntaxhighlight>
 
 
Input in the standard Prolog top level is terminated with a `.`: E.g.,
 
<langsyntaxhighlight lang="prolog">?- main.
Guess an integer between 1 and 10.
Guess the number: a.
Invalid input.
Guess the number: 3.
Your guess is too low.</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">OpenConsole()
 
Repeat
Line 3,529 ⟶ 3,973:
PrintN("Your guess is to high.")
EndIf
ForEver</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import random
 
inclusive_range = (1, 100)
Line 3,557 ⟶ 4,001:
if answer > target: print(" Too high.")
 
print("\nThanks for playing.")</langsyntaxhighlight>
 
'''Sample Game'''
Line 3,591 ⟶ 4,035:
I don't understand your input of 'Howdy' ?
Your guess(4): </pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ say "Guess the number (1-100 inclusive.)"
cr cr
100 random 1+
[ $ "Your guess... " input
trim reverse trim reverse
$->n
not iff
[ drop
say "That is not a number." cr ]
again
2dup != while
over < iff
[ say "Too small." cr ]
again
say "Too large." cr
again ]
say "Well done! "
echo say " is correct. " cr
drop ] is guess-the-number ( --> )</syntaxhighlight>
 
{{out}}
 
In the Quackery shell.
 
<pre>/O> guess-the-number
...
Guess the number (1-100 inclusive.)
 
Your guess... eleventy thruppence
That is not a number.
Your guess... 50
Too small.
Your guess... 75
Too small.
Your guess... 83
Too large.
Your guess... 79
Too large.
Your guess... 77
Too large.
Your guess... 76
Well done! 76 is correct.
 
Stack empty.
 
/O></pre>
 
=={{header|R}}==
Line 3,597 ⟶ 4,090:
This solution works on the assumption that the number to be found is an integer and also assumes that the upper and lower bounds are distinct integers used inclusively. For example, this means that low=4 and high=5 should be a solvable case, but low=high=4 will throw an error. See [[Talk:Guess the number/With feedback|Talk page]] entry dated 1st June 2020.
 
<langsyntaxhighlight lang="rsplus">guessANumber <- function(low, high)
{
boundryErrorCheck(low, high)
Line 3,629 ⟶ 4,122:
while(!is.numeric(guess) || as.integer(guess) != guess){guess <- type.convert(readline("That wasn't an integer! Try again "))}
as.integer(guess)
}</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define (guess-number min max)
Line 3,650 ⟶ 4,143:
 
(guess-number 1 100)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" perl6line>my $maxnum = prompt("Hello, please give me an upper boundary: ");
until 0 < $maxnum < Inf {
say "Oops! The upper boundary should be > 0 and not Inf";
Line 3,677 ⟶ 4,170:
}
}
say "Great you guessed right after $count attempts!";</langsyntaxhighlight>
 
<pre>Hello, please give me an upper boundary: 10
Line 3,689 ⟶ 4,182:
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">: high|low ( gn-g$ )
over > [ "high" ] [ "low" ] if ;
 
Line 3,704 ⟶ 4,197:
think [ getToken toNumber checkGuess ] while
"You got it!\n" puts ;
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
To make the program more engaging, randomized words for the hint are used.
<langsyntaxhighlight lang="rexx">/*REXX program plays guess the number game with a human; the computer picks the number*/
low= 1 /*the lower range for the guessing game*/
high= 100 /* " upper " " " " " */
Line 3,742 ⟶ 4,235:
/*stick a fork in it, we're all done. */
if try==1 then say 'Gadzooks!!! You guessed the number right away!'
else say 'Congratulations!, you guessed the number in ' try " tries."</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">fr = 1 t0 = 10
while true
see "Hey There,
Line 3,775 ⟶ 4,268:
d = random(e)
if d >= s return d ok
end</langsyntaxhighlight>
 
=={{header|Ruby}}==
{{trans|Mirah}}
<langsyntaxhighlight lang="ruby">number = rand(1..10)
 
puts "Guess the number between 1 and 10"
Line 3,797 ⟶ 4,290:
puts "Please enter an integer."
end
end</langsyntaxhighlight>
 
=={{header|Rust}}==
{{libheader|rand}}
<langsyntaxhighlight lang="rust">use rand::Rng;
use std::cmp::Ordering;
use std::io;
Line 3,838 ⟶ 4,331:
}
}
}</langsyntaxhighlight>
<pre>I have chosen my number between 1 and 100. Guess the number!
Please input your guess.
Line 3,874 ⟶ 4,367:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import java.util.Random
import java.util.Scanner
 
Line 3,890 ⟶ 4,383:
else if (guessedNumber < randomNumber) println("Your guess is too low!")
else println("You got it!")
} while (guessedNumber != randomNumber)</langsyntaxhighlight>
 
=={{header|Scheme}}==
{{works with|Chicken Scheme}}
{{works with|Guile}}
<langsyntaxhighlight lang="scheme">(define maximum 5)
(define minimum -5)
(define number (+ (random (- (+ maximum 1) minimum)) minimum))
Line 3,912 ⟶ 4,405:
(if (< guess number)
(display "Too low!\n> ")))))
(display "Correct!\n")</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
const integer: lower_limit is 0;
Line 3,941 ⟶ 4,434:
writeln("You gave up!");
end if;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">var number = rand(1..10);
say "Guess the number between 1 and 10";
 
Line 3,954 ⟶ 4,447:
default { say "Too high" }
}
}</langsyntaxhighlight>
 
=={{header|Small Basic}}==
<langsyntaxhighlight Smalllang="small Basicbasic">number=Math.GetRandomNumber(10)
TextWindow.WriteLine("I just thought of a number between 1 and 10. What is it?")
While guess<>number
Line 3,968 ⟶ 4,461:
EndIf
EndWhile
TextWindow.WriteLine("You win!")</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
Line 3,978 ⟶ 4,471:
To play: run "GuessingGame playFrom: 1 to: 100" on a Workspace (Tools -> Workspace)
 
<syntaxhighlight lang="smalltalk">
<lang Smalltalk>
'From Pharo7.0.3 of 12 April 2019 [Build information: Pharo-7.0.3+build.158.sha.0903ade8a6c96633f07e0a7f1baa9a5d48cfdf55 (64 Bit)] on 30 October 2019 at 4:24:17.115807 pm'!
Object subclass: #GuessingGame
Line 4,076 ⟶ 4,569:
play! !
 
</syntaxhighlight>
</lang>
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="sparkling">printf("Lower bound: ");
let lowerBound = toint(getline());
 
Line 4,105 ⟶ 4,598:
break;
}
}</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">import Cocoa
 
var found = false
Line 4,130 ⟶ 4,623:
println("Good try but the number is less than that!")
}
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">set from 1
set to 10
set target [expr {int(rand()*($to-$from+1) + $from)}]
Line 4,152 ⟶ 4,645:
break
}
}</langsyntaxhighlight>
Sample output:
<pre>
Line 4,173 ⟶ 4,666:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
PRINT "Find the luckynumber (7 tries)!"
Line 4,194 ⟶ 4,687:
IF (round==7) PRINT/ERROR "You've lost: luckynumber was: ",luckynumber
ENDLOOP
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,221 ⟶ 4,714:
{{works with|Public Domain Korn SHell}}
{{works with|Z SHell}}
<langsyntaxhighlight lang="sh">function guess {
[[ -n $BASH_VERSION ]] && shopt -s extglob
[[ -n $ZSH_VERSION ]] && set -o KSH_GLOB
Line 4,246 ⟶ 4,739:
fi
done
}</langsyntaxhighlight>
 
Sample run:
Line 4,265 ⟶ 4,758:
=={{header|Ursa}}==
{{trans|Python}}
<langsyntaxhighlight lang="ursa">decl int high low
set low 0
set high 100
Line 4,294 ⟶ 4,787:
end while
 
out endl "Thanks for playing." endl console</langsyntaxhighlight>
 
=={{header|Vala}}==
<langsyntaxhighlight lang="vala">
void main(){
const int from = 1;
Line 4,329 ⟶ 4,822:
}//while
} // main
</syntaxhighlight>
</lang>
 
Shorter but no error checking
<langsyntaxhighlight lang="vala">int main() {
int guess, x = Random.int_range(1, 10);
stdout.printf("Make a guess (1-10): ");
Line 4,340 ⟶ 4,833:
stdout.printf("Got it!\n");
return 0;
}</langsyntaxhighlight>
 
=={{header|VBA Excel}}==
The Application.InputBox display a message when input is inappropriate.
<langsyntaxhighlight lang="vb">Sub GuessTheNumberWithFeedback()
Dim Nbc&, Nbp&, m&, n&, c&
 
Line 4,361 ⟶ 4,854:
Loop
MsgBox "Well guessed!" & vbCrLf & "You find : " & Nbc & " in " & c & " guesses!"
End Sub</langsyntaxhighlight>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Dim max,min,secretnum,numtries,usernum
max=100
Line 4,391 ⟶ 4,884:
End If
Loop
</syntaxhighlight>
</lang>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import rand.seed
import rand
import os
const (
lower = 1
upper = 100
)
fn main() {
rand.seed(seed.time_seed_array(2))
n := rand.intn(upper-lower+1) or {0} + lower
for {
guess := os.input("Guess integer number from $lower to $upper: ").int()
if guess < n {
println("Too low. Try again: ")
} else if guess > n {
println("Too high. Try again: ")
} else {
println("Well guessed!")
return
}
}
}</syntaxhighlight>
 
=={{header|VTL-2}}==
<syntaxhighlight lang="vtl2">10 ?="Minimum? ";
20 L=?
30 ?="Maximum? ";
40 H=?
50 #=L<H*80
60 ?="Minimum must be lower than maximum."
70 #=10
80 S='/(H-L+1)*0+L+%
90 T=0
100 ?="Guess? ";
110 G=?
120 #=G>L*(H>G)*150
130 ?="Guess must be in between minimum and maximum."
140 #=100
150 T=T+1
160 #=G=S*220
170 #=G<S*200
180 ?="Too high."
190 #=100
200 ?="Too low."
210 #=100
220 ?="Correct!"
230 ?="Tries: ";
240 ?=T</syntaxhighlight>
{{out}}
<pre>Minimum? 10
Maximum? 90
Guess? 91
Guess must be in between minimum and maximum.
Guess? 9
Guess must be in between minimum and maximum.
Guess? 50
Too low.
Guess? 70
Too high.
Guess? 60
Too high.
Guess? 55
Correct!
Tries: 4</pre>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">import "io" for Stdin, Stdout
import "random" for Random
 
Line 4,414 ⟶ 4,976:
break
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,439 ⟶ 5,001:
 
=={{header|XLISP}}==
<langsyntaxhighlight lang="lisp">(defun guessing-game (a b)
; minimum and maximum, to be supplied by the user
(defun prompt ()
Line 4,464 ⟶ 5,026:
(display ". Try to guess it!")
(newline)
(prompt))</langsyntaxhighlight>
{{out}}
<pre>[1] (guessing-game 19 36)
Line 4,486 ⟶ 5,048:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
int Lo, Hi, C, Guess, Number;
 
Line 4,509 ⟶ 5,071:
CrLf(0);
until Guess = Number;
]</langsyntaxhighlight>
 
Example output:
Line 4,527 ⟶ 5,089:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">r:=(0).random(10)+1;
while(1){
n:=ask("Num between 1 & 10: ");
Line 4,533 ⟶ 5,095:
if(n==r){ println("Well guessed!"); break; }
println((n<r) and "small" or "big");
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,550 ⟶ 5,112:
=={{header|Zoomscript}}==
For typing:
<langsyntaxhighlight Zoomscriptlang="zoomscript">var randnum
var guess
randnum & random 1 10
Line 4,566 ⟶ 5,128:
endif
endwhile
print "Correct number. You win!"</langsyntaxhighlight>
 
For importing:
Line 4,573 ⟶ 5,135:
 
=={{header|ZX Spectrum Basic}}==
<langsyntaxhighlight lang="zxbasic">ZX Spectrum Basic has no [[:Category:Conditional loops|conditional loop]] constructs, so we have to emulate them here using IF and GO TO.
1 LET n=INT (RND*10)+1
2 INPUT "Guess a number that is between 1 and 10: ",g: IF g=n THEN PRINT "That's my number!": STOP
3 IF g<n THEN PRINT "That guess is too low!": GO TO 2
4 IF g>n THEN PRINT "That guess is too high!": GO TO 2</langsyntaxhighlight>
305

edits