# Guess the number/With feedback

Guess the number/With feedback
You are encouraged to solve this task according to the task description, using any language you may know.

Write a game (computer program) that follows the following rules:

• The computer chooses a number between given set limits.
• The player is asked for repeated guesses until the the target number is guessed correctly
• At each guess, the computer responds with whether the guess is:
• higher than the target,
• equal to the target,
• less than the target,   or
• the input was inappropriate.

## 11l

Translation of: Python
```V (target_min, target_max) = (1, 100)

print("Guess my target number that is between #. and #. (inclusive).\n".format(target_min, target_max))
V target = random:(target_min..target_max)
V (answer, i) = (target_min - 1, 0)
i++
V txt = input(‘Your guess(#.): ’.format(i))
X.try
X.catch ValueError
print(‘  I don't understand your input of '#.' ?’.format(txt))
L.continue
print(‘  Out of range!’)
L.continue
print(‘  Ye-Haw!!’)
L.break
I answer < target {print(‘  Too low.’)}
I answer > target {print(‘  Too high.’)}

print("\nThanks for playing.")```
Output:
```Guess my target number that is between 1 and 100 (inclusive).

I don't understand your input of 'kk' ?
Out of range!
Too high.
Out of range!
Too high.
Too high.
Too low.
Too low.
Too low.
Too low.
Too low.
Ye-Haw!!

Thanks for playing.
```

## Action!

```PROC Main()
BYTE x,n,min=[1],max=[100]

PrintF("Try to guess a number %B-%B: ",min,max)
x=Rand(max-min+1)+min
DO
n=InputB()
IF n<min OR n>max THEN
Print("The input is incorrect. Try again: ")
ELSEIF n<x THEN
Print("My number is higher. Try again: ")
ELSEIF n>x THEN
Print("My number is lower. Try again: ")
ELSE
PrintE("Well guessed!")
EXIT
FI
OD
RETURN```
Output:
```Try to guess a number 1-100: 50
My number is lower. Try again: 25
My number is lower. Try again: 12
My number is higher. Try again: 19
My number is lower. Try again: 15
My number is lower. Try again: 14
Well guessed!
```

```with Ada.Numerics.Discrete_Random;
procedure Guess_Number_Feedback is
function Get_Int (Prompt : in String) return Integer is
begin
loop
declare
Response : constant String := Ada.Text_IO.Get_Line;
begin
if Response /= "" then
return Integer'Value (Response);
end if;
exception
when others =>
Ada.Text_IO.Put_Line ("Invalid response, not an integer!");
end;
end loop;
end Get_Int;
procedure Guess_Number (Lower_Limit : Integer; Upper_Limit : Integer) is
subtype Number is Integer range Lower_Limit .. Upper_Limit;
package Number_RNG is new Ada.Numerics.Discrete_Random (Number);
Generator  : Number_RNG.Generator;
My_Number  : Number;
Your_Guess : Integer;
begin
Number_RNG.Reset (Generator);
My_Number := Number_RNG.Random (Generator);
loop
Your_Guess := Get_Int ("Your guess: ");
exit when Your_Guess = My_Number;
if Your_Guess > My_Number then
else
end if;
end loop;
end Guess_Number;
Lower_Limit : Integer;
Upper_Limit : Integer;
begin
loop
Lower_Limit := Get_Int  ("Lower Limit: ");
Upper_Limit := Get_Int  ("Upper Limit: ");
exit when Lower_Limit < Upper_Limit;
Ada.Text_IO.Put_Line ("Lower limit must be lower!");
end loop;
Guess_Number (Lower_Limit, Upper_Limit);
end Guess_Number_Feedback;
```

## ALGOL 68

Works with: ALGOL 68G version Any - tested with release 2.8.win32
```# simple guess-the-number game #

main:(

BOOL valid input := TRUE;

# register an error handler so we can detect and recover from #
# invalid input #
on value error( stand in
, ( REF FILE f )BOOL:
BEGIN
valid input := FALSE;
TRUE
END
);

# construct a random integer between 1 and 100                #
INT   number = 1 + ROUND ( random * 99 );

print( ( "I'm thinking of a number between 1 and 100", newline ) );

# read the user's guesses until they guess correctly          #
# we give feedback so they can find the number using interval #
# halving                                                     #
WHILE
print( ( "What do you think it is? " ) );
INT guess;
WHILE
valid input := TRUE;
read( ( guess, newline ) );
NOT valid input
DO
print( ( "Please guess a number between 1 and 100", newline ) )
OD;
number /= guess
DO
IF number > guess
THEN
# the user's guess was too small #
print( ( newline, "My number is higher than that", newline ) )
ELSE
# the user's guess was too large #
print( ( newline, "My number is lower than that",  newline ) )
FI
OD;
print( ( "That's correct!", newline ) )
)```
Output:
```I'm thinking of a number between 1 and 100
What do you think it is? f
Please guess a number between 1 and 100
q
Please guess a number between 1 and 100
50

My number is higher than that
What do you think it is? 75

My number is lower than that
What do you think it is? 67

My number is higher than that
What do you think it is? 72

My number is higher than that
What do you think it is? 73
That's correct!
```

## AppleScript

```-- defining the range of the number to be guessed
property minLimit : 1
property maxLimit : 100

on run
-- define the number to be guessed
set numberToGuess to (random number from minLimit to maxLimit)
-- prepare a variable to store the user's answer
set guessedNumber to missing value
-- prepare a variable for feedback
set tip to ""
-- start a loop (will be exited by using "exit repeat" after a correct guess)
repeat
-- ask the user for his/her guess, the variable tip contains text after first guess only
set usersChoice to (text returned of (display dialog "Guess the number between " & minLimit & " and " & maxLimit & " inclusive" & return & tip default answer "" buttons {"Check"} default button "Check"))
-- try to convert the given answer to an integer and compare it the number to be guessed
try
set guessedNumber to usersChoice as integer
if guessedNumber is greater than maxLimit or guessedNumber is less than minLimit then
-- the user guessed a number outside the given range
set tip to "(Tipp: Enter a number between " & minLimit & " and " & maxLimit & ")"
else if guessedNumber is less than numberToGuess then
-- the user guessed a number less than the correct number
set tip to "(Tipp: The number is greater than " & guessedNumber & ")"
else if guessedNumber is greater than numberToGuess then
-- the user guessed a number greater than the correct number
set tip to "(Tipp: The number is less than " & guessedNumber & ")"
else if guessedNumber is equal to numberToGuess then
-- the user guessed the correct number and gets informed
display dialog "Well guessed! The number was " & numberToGuess buttons {"OK"} default button "OK"
-- exit the loop (quits this application)
exit repeat
end if
on error
-- something went wrong, remind the user to enter a numeric value
set tip to "(Tipp: Enter a number between " & minLimit & " and " & maxLimit & ")"
end try
end repeat
end run
```

## Arturo

```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!"
]
```
Output:
```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! :)```

## AutoHotkey

```MinNum = 1
MaxNum = 99999999999

Random, RandNum, %MinNum%, %MaxNum%
Loop
{
InputBox, Guess, Number Guessing, Please enter a number between %MinNum% and %MaxNum%:,, 350, 130,,,,, %Guess%
If ErrorLevel
ExitApp
If Guess Is Not Integer
{
MsgBox, 16, Error, Invalid guess.
Continue
}
If Guess Not Between %MinNum% And %MaxNum%
{
MsgBox, 16, Error, Guess must be a number between %MinNum% and %MaxNum%.
Continue
}
If A_Index = 1
TotalTime = %A_TickCount%
Tries = %A_Index%
If Guess = %RandNum%
Break
If Guess < %RandNum%
MsgBox, 64, Incorrect, The number guessed (%Guess%) was too low.
If Guess > %RandNum%
MsgBox, 64, Incorrect, The number guessed (%Guess%) was too high.
}
TotalTime := Round((A_TickCount - TotalTime) / 1000,1)
MsgBox, 64, Correct, The number %RandNum% was guessed in %Tries% tries, which took %TotalTime% seconds.
```

## AWK

```# syntax: GAWK -f GUESS_THE_NUMBER_WITH_FEEDBACK.AWK
BEGIN {
L = 1
H = 100
srand()
n = int(rand() * (H-L+1)) + 1
printf("I am thinking of a number between %d and %d. Try to guess it.\n",L,H)
while (1) {
getline ans
if (ans !~ /^[0-9]+\$/) {
print("Your input was not a number. Try again.")
continue
}
if (n == ans) {
print("Well done you.")
break
}
if (n > ans) {
continue
}
if (n < ans) {
continue
}
}
exit(0)
}
```

## BASIC

### Applesoft BASIC

```100 L% = 1
110 U% = 10
120 N% = RND(1) * (U% - L% + 1) + L%
130 PRINT "A NUMBER FROM " L%;
140 PRINT " TO " U%;
150 PRINT ", CALLED A TARGET, HAS BEEN RANDOMLY CHOSEN."
160 FOR Q = 0 TO 1 STEP 0
170     INPUT "GUESS THE TARGET NUMBER: "; G%
180     IF G% < L% OR G% > U% THEN PRINT "THE INPUT WAS INAPPROPRIATE."
190     IF G% > N% THEN PRINT "THE GUESS WAS HIGHER THAN THE TARGET."
200     IF G% < N% THEN PRINT "THE GUESS WAS LESS THAN THE TARGET."
210     Q = G% = N%
220 NEXT
230 PRINT "THE GUESS WAS EQUAL TO THE TARGET."```

### BASIC256

Works with: BASIC256
```Min = 5: Max = 15
chosen = int(rand*(Max-Min+1)) + Min
print "Guess a whole number between "+Min+" and "+Max
do
input "Enter your number " ,guess
if guess < Min OR guess > Max 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 if
until guess = chosen```

Output:(example)

```Guess a whole number between 5 and 15
Sorry, your number was too high
Sorry, your number was too low
Well guessed!```

### Chipmunk Basic

Works with: Chipmunk Basic version 3.6.4
```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
```

### Gambas

Translation of: FreeBASIC
```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
```

### GW-BASIC

Works with: PC-BASIC version any
Works with: BASICA
```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
```

### IS-BASIC

```100 PROGRAM "Guess.bas"
110 RANDOMIZE
120 LET UP=10:LET LO=1 ! Limits
130 PRINT "I'm thinking of a number between";LO;"and";UP
140 LET COUNT=0:LET NR=RND(UP-LO+1)+LO
150 DO
160   LET COUNT=COUNT+1
170   INPUT PROMPT "Guess a number: ":GU
180   SELECT CASE GU
190   CASE IS>NR
200     PRINT "My number is lower that."
210   CASE IS<NR
220     PRINT "My number is higher that."
230   CASE ELSE
240     PRINT "Well guessed! Numner of tips:";COUNT
250   END SELECT
260 LOOP UNTIL NR=GU```

### Minimal BASIC

Translation of: Tiny Craft Basic
```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
```

### MSX Basic

Works with: MSX BASIC version any
```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
```

### QB64

Note that `INPUT` 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.

```DIM secretNumber AS _BYTE ' the secret number
DIM guess AS _BYTE ' the player's guess

RANDOMIZE TIMER ' set a seed based on system time
secretNumber%% = INT(RND * 10) + 1 ' a random value between 1 and 10
PRINT "The computer has chosen a secret number between 1 and 10."
DO
INPUT guess%%
SELECT CASE guess%%
CASE IS < 1, IS > 10
PRINT "Please enter a number between 1 and 10!"
_CONTINUE
CASE IS < secretNumber%%
PRINT "Your guess is LOWER than the target."
CASE IS > secretNumber%%
PRINT "Your guess is HIGHER than the target."
END SELECT
LOOP UNTIL guess%% = secretNumber%%
PRINT "You won!"; secretNumber%%; "was the secret number!"
```
Output:
```The computer has chosen a secret number between 1 and 10.
Please enter a number between 1 and 10!
Please enter a number between 1 and 10!
Your guess is LOWER than the target.
Your guess is HIGHER than the target.
Your guess is HIGHER than the target.
Your guess is HIGHER than the target.
You won! 2 was the secret number!```

### QBasic

Works with: QBasic version 1.1
Works with: QuickBasic version 4.5
```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
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
```

### Run BASIC

Works with: Just BASIC
Works with: Liberty BASIC
```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]
```

### True BASIC

Translation of: FreeBASIC
```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
```

### Yabasic

```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
```

## Batch File

```@echo off

:A
set /a number=%random% %% 10 + 1
:B
set /p guess=Choose a number between 1 and 10:
if %guess equ %number% goto win
if %guess% gtr 10 msg * "Number must be between 1 and 10."
if %guess% leq 0 msg * "Number must be between 1 and 10."
if %guess% gtr %number% echo Higher!
if %guess% lss %number% echo Lower!
goto B
:win
cls
echo You won! The number was %number%
pause>nul
goto A
```

## BBC BASIC

```      Min% = 1
Max% = 10
chosen% = RND(Max%-Min%+1) + Min% - 1
REPEAT
PRINT "Guess a number between "; Min% " and " ;Max% ;
INPUT ": " guess%
CASE TRUE OF
WHEN guess% < Min% OR guess% > Max%:
PRINT "That was an invalid number"
WHEN guess% < chosen%:
PRINT "Sorry, your number was too low"
WHEN guess% > chosen%:
PRINT "Sorry, your number was too high"
OTHERWISE:
PRINT "Well guessed!"
EXIT REPEAT
ENDCASE
UNTIL FALSE
END
```

## BCPL

```get "libhdr"
static \$( randstate = ? \$)

let rand(min,max) = valof
\$(  let x = ?
let range = max-min
\$(  randstate := random(randstate)
x := (randstate >> 6) & mask
\$) repeatuntil 0 <= x < range
resultis x + min
\$)

\$(  let x = ?
writes("Guess? ")
if min <= x < max then resultis x
writes("Invalid input.*N")
\$) repeat

let play(min,max,secret) be
\$(  let tries, guess = 0, ?
if guess < secret then writes("Too low!*N")
if guess > secret then writes("Too high!*N")
tries := tries + 1
\$) repeatuntil guess = secret
writef("Correct! You guessed it in %N tries.*N", tries)
\$)

let start() be
\$(  let min, max = ?, ?
writes("Guess the number*N*N")
writes("Random seed? ") ; randstate := readn()
\$(  writes("Lower bound? ") ; min := readn()
writes("Upper bound? ") ; max := readn() + 1
test max-1 > min break or writes("Invalid bounds.*N")
\$) repeat
wrch('*N')
play(min, max, rand(min, max))
\$)```
Output:
```Guess the number

Random seed? 12345
Lower bound? 1
Upper bound? 100

Guess? 0
Invalid input.
Guess? 101
Invalid input.
Guess? 50
Too low!
Guess? 75
Too high!
Guess? 62
Too high!
Guess? 56
Too high!
Guess? 53
Too high!
Guess? 52
Correct! You guessed it in 6 tries.```

## Befunge

The number range is hardcoded at the start of the program (1:"d" being 1 to 100), but can easily be changed.

```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">
```
Output:
```Guess a number between 1 and 100
> 50
> 25
> 37
> 31
You guessed correctly!```

## Brat

```number = random 10

p "Guess a number between 1 and 10."

until {

true? (guess.null? || { guess > 10 || guess < 1 })
{ p "Please guess a number between 1 and 10."}
{ true? guess == number
{ p "Correct!"; true }
{ true? guess < number
{ p "Too low!" }
{ p "Too high!" }
}
}
}```

## C

```#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define lower_limit 0
#define upper_limit 100

int main(){
int number, guess;

srand( time( 0 ) );
number = lower_limit + rand() % (upper_limit - lower_limit + 1);

printf( "Guess the number between %d and %d: ", lower_limit, upper_limit );
fflush(stdout);  // Flush the output buffer

while( scanf( "%d", &guess ) == 1 ){
if( number == guess ){
printf( "You guessed correctly!\n" );
break;
}
printf( "Your guess was too %s.\nTry again: ", number < guess ? "high" : "low" );
}

return 0;
}
```

Demonstration:

```Guess the number between 0 and 100: 50
Try again: 75
Try again: 63
Try again: 57
Try again: 60
Try again: 58
Try again: 59
You guessed correctly!```

## C#

```using System;

class Program
{
static void Main(string[] args)
{
const int from = 1;
const int to = 10;

int randomNumber = new Random().Next(from, to);
int guessedNumber;

Console.Write("The number is between {0} and {1}. ", from, to);
while (true)
{
Console.Write("Make a guess: ");
{
if (guessedNumber == randomNumber)
{
Console.WriteLine("You guessed the right number!");
break;
}
else
{
Console.WriteLine("Your guess was too {0}.", (guessedNumber > randomNumber) ? "high" : "low");
}
}
else
{
Console.WriteLine("Input was not an integer.");
}
}

Console.WriteLine();
Console.WriteLine("Press any key to exit.");
}
}
```

Output:

```The number is between 1 and 10. Make a guess: 1
Make a guess: 9
Make a guess: 5
Make a guess: 6
Make a guess: hello, world
Input was not an integer.
Make a guess: 7
You guessed the right number!

Press any key to exit.
```

## C++

```#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
std::srand(std::time(0));
int lower, upper, guess;
std::cout << "Enter lower limit: ";
std::cin >> lower;
std::cout << "Enter upper limit: ";
std::cin >> upper;
int random_number = lower + std::rand() % ((upper + 1) - lower);

do
{
std::cout << "Guess what number I have: ";
std::cin >> guess;
if (guess > random_number)
std::cout << "Your guess is too high\n";
else if (guess < random_number)
std::cout << "Your guess is too low\n";
else
std::cout << "You got it!\n";
} while (guess != random_number);

return 0;
}
```

Output:

```Enter lower limit: 1
Enter upper limit: 100
Guess what number I have: 50
Guess what number I have: 40
Guess what number I have: 25
Guess what number I have: 10
Guess what number I have: 2
Guess what number I have: 4
Guess what number I have: 3
You got it!```

## Caché ObjectScript

```GUESSNUM
; get a random number between 1 and 100
set target = (\$random(100) + 1)    ; \$r(100) gives 0-99

; loop until correct
set tries = 0
for {
write !,"Guess the integer between 1 and 100: "
read "",guess    ; gets input following write

; validate input
if (guess?.N) && (guess > 0) && (guess < 101) {
; increment attempts
set tries = tries + 1

; evaluate the guess
set resp = \$select(guess < target:"too low.",guess > target:"too high.",1:"correct!")

; display result, conditionally exit loop
quit:resp="correct!"
}
else {
write !,"Please enter an integer between 1 and 100."
}
}    ; guess loop

write !!,"You guessed the number in "_tries_" attempts."
quit```
Output:
```SAMPLES>do ^GUESSNUM^
Guess the integer between 1 and 100: 50
Guess the integer between 1 and 100: 75
Guess the integer between 1 and 100: 60
Guess the integer between 1 and 100: 65
Guess the integer between 1 and 100: 70
Guess the integer between 1 and 100: 73
Guess the integer between 1 and 100: 74

You guessed the number in 7 attempts.```

## Ceylon

In you module.ceylon file put import ceylon.random "1.3.1";

```import ceylon.random {
DefaultRandom
}

shared void run() {
value random = DefaultRandom();
value range = 1..10;
while(true) {
value chosen = random.nextElement(range);
print("I have chosen a number between ``range.first`` and ``range.last``.
while(true) {
value guess = Integer.parse(line.trimmed);

if(is ParseException guess) {
print(guess.message);
continue;
}

switch(guess <=> chosen)
case (larger) {
print("Too high!");
}
case (smaller) {
print("Too low!");
}
case (equal) {
print("You got it!");
break;
}
} else {
}
}
}
}
```

## Clojure

```(defn guess-run []
(let [start 1
end 100
target (+ start (rand-int (inc (- end start))))]
(printf "Guess a number between %d and %d" start end)
(loop [i 1]
(if (cond
(not (number? ans)) (println "Invalid format")
(or (< ans start) (> ans end)) (println "Out of range")
(< ans target)    (println "too low")
(> ans target)    (println "too high")
:else             true)
(println "Correct")
(recur (inc i)))))))
```

## CLU

```read_number = proc (prompt: string) returns (int)
po: stream := stream\$primary_output()
pi: stream := stream\$primary_input()
while true do
stream\$puts(po, prompt)
return(int\$parse(stream\$getl(pi)))
stream\$putl(po, "Invalid number.")
end
end

read_limits = proc () returns (int,int)
po: stream := stream\$primary_output()
while true do
min: int := read_number("Lower limit? ")
max: int := read_number("Upper limit? ")
if min >= 0 cand min < max then return(min,max) end
stream\$putl(po, "Invalid limits, try again.")
end

read_guess = proc (min, max: int) returns (int)
po: stream := stream\$primary_output()
while true do
if min <= guess cand max >= guess then return(guess) end
stream\$putl(po, "Guess must be between "
|| int\$unparse(min) || " and " || int\$unparse(max) || ".")
end

play_game = proc (min, max, secret: int)
po: stream := stream\$primary_output()
guesses: int := 0
while true do
guesses := guesses + 1
if guess = secret then break
elseif guess < secret then stream\$putl(po, "Too low!")
elseif guess > secret then stream\$putl(po, "Too high!")
end
end
stream\$putl(po, "Correct! You got it in " || int\$unparse(guesses) || " tries.")
end play_game

start_up = proc ()
po: stream := stream\$primary_output()
d: date := now()
random\$seed(d.second + 60*(d.minute + 60*d.hour))

stream\$putl(po, "Guess the number\n----- --- ------\n")
secret: int := min + random\$next(max - min + 1)
play_game(min, max, secret)
end start_up```
Output:
```Guess the number
----- --- ------

Lower limit? 1
Upper limit? 100
Guess? 50
Too high!
Guess? 25
Too high!
Guess? 12
Too low!
Guess? 18
Too low!
Guess? 22
Too high!
Guess? 20
Too low!
Guess? 21
Correct! You got it in 7 tries.```

## COBOL

```       IDENTIFICATION DIVISION.
PROGRAM-ID. Guess-With-Feedback.

DATA DIVISION.
LOCAL-STORAGE SECTION.
01  Seed       PIC 9(8).
01  Random-Num PIC 99.
01  Guess      PIC 99.

PROCEDURE DIVISION.
ACCEPT Seed FROM TIME
COMPUTE Random-Num =
FUNCTION REM(FUNCTION RANDOM(Seed) * 1000, 10) + 1

DISPLAY "Guess a number between 1 and 10:"

PERFORM FOREVER
ACCEPT Guess

IF Guess > Random-Num
DISPLAY "Your guess was too high."
ELSE IF Guess < Random-Num
DISPLAY "Your guess was too low."
ELSE
DISPLAY "Well guessed!"
EXIT PERFORM
END-PERFORM

GOBACK
.
```

## Common Lisp

```(defun guess-the-number-feedback (&optional (min 1) (max 100))
(let ((num-guesses 0)
(num (+ (random (1+ (- max min))) min))
(guess nil))
(format t "Try to guess a number from ~:d to ~:d!~%" min max)
(loop do (format t "Guess? ")
(incf num-guesses)
(format t "Your guess is ~[not a number.~;too small.~;too large.~;correct!~]~%"
(cond ((not (numberp guess)) 0)
((< guess num) 1)
((> guess num) 2)
((= guess num) 3)))
until (and (numberp guess)
(= guess num)))
(format t "You got the number correct on the ~:r guess!~%" num-guesses)))
```

Output:

```CL-USER> (guess-the-number-feedback 1 1024)
Try to guess a number from 1 to 1,024!
Guess? 512
Guess? 768
Guess? 896
Guess? 832
Guess? 864
Guess? 848
Guess? 840
Guess? 836
You got the number correct on the eighth guess!
```

## Craft 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

endif

if g > n then

endif

loop g <> n

alert "you guessed the number in ", t, " tries"
```

## Crystal

Translation of: Ruby
```number = rand(1..10)

puts "Guess the number between 1 and 10"

loop do
begin
user_number = gets.to_s.to_i
if user_number == number
puts "You guessed it."
break
elsif user_number > number
puts "Too high."
else
puts "Too low."
end
rescue ArgumentError
end
end
```

## D

Translation of: Python
```import std.stdio, std.random, std.typecons, std.conv, std.string,
std.range;

void main() {
immutable interval = tuple(1, 100);
writefln("Guess my target number that is between " ~
"%d and %d (inclusive).\n", interval[]);
immutable target = uniform!"[]"(interval[]);

foreach (immutable i; sequence!q{n}) {
writef("Your guess #%d: ", i + 1);

try {
} catch (ConvException e) {
writefln("  I don't understand your input '%s'", txt);
continue;
}
writeln("  Out of range!");
continue;
}
writeln("  Well guessed.");
break;
}
writeln(answer < target ? "  Too low." : "  Too high.");
}
}
```

Sample game:

```Guess my target number that is between 1 and 100 (inclusive).

Too high.
Too low.
Out of range!
Too low.
Too low.
Too low.
Too high.
Too high.
Well Guessed.```

## DCL

```\$ rnd = f\$extract( 21, 2, f\$time() )
\$ count = 0
\$ loop:
\$ inquire guess "guess what number between 0 and 99 inclusive I am thinking of"
\$ guess = f\$integer( guess )
\$ if guess .lt. 0 .or. guess .gt. 99
\$ then
\$  write sys\$output "out of range"
\$  goto loop
\$ endif
\$ count = count + 1
\$ if guess .lt. rnd then \$ write sys\$output "too small"
\$ 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"
```
Output:
```\$ @guess
guess what number between 0 and 99 inclusive I am thinking of: 50
too small
guess what number between 0 and 99 inclusive I am thinking of: 999
out of range
guess what number between 0 and 99 inclusive I am thinking of: 75
too small
guess what number between 0 and 99 inclusive I am thinking of: 82
too small
guess what number between 0 and 99 inclusive I am thinking of: 91
too small
guess what number between 0 and 99 inclusive I am thinking of: 96
too small
guess what number between 0 and 99 inclusive I am thinking of: 98
too large
guess what number between 0 and 99 inclusive I am thinking of: 97
it only took you 7 guesses```

## Delphi

```program GuessTheNumber;

{\$APPTYPE CONSOLE}

uses
SysUtils,
Math;

const
// Set min/max limits
min: Integer = 1;
max: Integer = 10;

var
last,
val,
inp: Integer;
s: string;

// Initialise new game
procedure NewGame;
begin
// Make sure this number isn't the same as the last one
repeat
val := RandomRange(min,max);
until
val <> last;

// Save this number
last := val;
Writeln('Guess a number between ', min, ' and ', max, ' [Answer = ', val, ']');
end;

begin
// Initialise the random number generator with a random value
Randomize;

// Initialise last number
last := 0;

// Initialise user input
s := '';

// Start game
NewGame;

// Loop
repeat
// User input

// Validate - checxk if input is a number
if TryStrToInt(s,inp) then
begin
// Is it the right number?
if (inp = val) then
begin
// Yes - request a new game
Writeln('Correct! Another go? Y/N');
if SameText(s,'Y') then
// Start new game
NewGame
else
Exit;
end
else
// Input too low/high
if (inp < val) then
Writeln('Too low! Try again...')
else
if (inp > val) then
Writeln('Too high! Try again...');
end
else
// Input invalid
if not SameText(s,'bored') then
Writeln('Invalid input! Try again...');

until
SameText(s,'bored');

end.
```

## EasyLang

```print "Guess a number between 1 and 100!"
n = random 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"
```

## EchoLisp

```;;(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
(lib 'web) ; (play-sound) is defined in web.lib

(define (guess-feed (msg " 🔮 Enter a number in [0...100], -1 to stop.") (n (random 100)) (user 0))
(play-sound 'ko)
(unless (eq? n user ) ; user is the last user answer
(guess-feed
(cond ;; adapt prompt according to condition
((not (integer? user)) "❌ Please, enter an integer")
(( < user 0) (error "🌵 - It was:" n)) ; exit to top level
((> n user) "Too low ...")
((< n user) "Too high ..."))
n user))
(play-sound 'ok )
" 🔮 Well played!!  🍒 🍇 🍓")
```

## Ela

```open string datetime random core monad io

guess () = do
putStrLn "What's the upper bound?"
main ub
where main ub
| ub < 0 = "Bound should be greater than 0."
| else = do
putStrLn \$ format "Guess a number from 1 to {0}" ub
dt <- datetime.now
guesser (rnd (milliseconds \$ dt) 1 ub)
guesser v = do
if x == v then
cont ()
else if x < v then
do putStrLn "Too small!"
guesser v
else
do putStrLn "Too big!"
guesser v
cont () = do
putStrLn "Correct! Do you wish to continue (Y/N)?"
if a == "y" || a == "Y" then
guess ()
else if a == "n" || a == "N" then
do putStrLn "Bye!"
else
do putStrLn "Say what?"

guess () ::: IO```

## Elena

ELENA 6.x :

```import extensions;

public program()
{
int randomNumber := randomGenerator.nextInt(1,10);
console.printLine("I'm thinking of a number between 1 and 10.  Can you guess it?");
bool numberCorrect := false;
until(numberCorrect)
{
console.print("Guess: ");
if (randomNumber == userGuess)
{
numberCorrect := true;
console.printLine("Congrats!!  You guessed right!")
}
else if (randomNumber < userGuess)
{
}
else
{
}
}
}```
Output:
```I'm thinking of a number between 1 and 10.  Can you guess it?
Guess: 5
Guess: 8
Guess: 6
Congrats!!  You guessed right!
```

## Elixir

Works with: Elixir version 1.2
```defmodule GuessingGame do
def play(lower, upper) do
play(lower, upper, Enum.random(lower .. upper))
end
defp play(lower, upper, number) do
guess = Integer.parse(IO.gets "Guess a number (#{lower}-#{upper}): ")
case guess do
{^number, _} ->
IO.puts "Well guessed!"
{n, _} when n in lower..upper ->
IO.puts if n > number, do: "Too high.", else: "Too low."
play(lower, upper, number)
_ ->
IO.puts "Guess not in valid range."
play(lower, upper, number)
end
end
end

GuessingGame.play(1, 100)
```

## Emacs Lisp

```(let* ((min 1)
(max 100)
(number (+ (random (1+ (- max min))) min))
guess done)
(message "Guess the number between %d and %d" min max)
(while (not done)
(cond
((< guess number)
(message "Too low!"))
((> guess number)
(message "Too high!"))
((= guess number)
(setq done t)
(message "Well guessed!")))))
```

## Erlang

```% Implemented by Arjun Sunel
-module(guess_number).
-export([main/0]).

main() ->
L = 1 ,	 	% Lower Limit
U = 100, 	  % Upper Limit

io:fwrite("Guess my number between ~p and ", [L]),
io:fwrite("and ~p until you get it right.\n", [U]),
N = random:uniform(100),
guess(N).

guess(N) ->
{ok, [K]} = io:fread("Guess the number :  ","~d"),
if
K=:=N ->
io:format("Well guessed!!\n");
true ->
if
K > N ->
true ->
end,
guess(N)
end.
```
Output:
```1> c(guess_number).
{ok,guess_number}
2> guess_number:main().
Guess my number between 1 and and 100 until you get it right.
Guess the number :  50
Guess the number :  70
Guess the number :  85
Guess the number :  75
Guess the number :  73
Well guessed!!
ok
```

## Euphoria

```include get.e

constant lower_limit = 0, upper_limit = 100

integer number, guess
number = rand(upper_limit-lower_limit+1)+lower_limit

printf(1,"Guess the number between %d and %d: ", lower_limit & upper_limit)
while 1 do
guess = floor(prompt_number("", lower_limit & upper_limit))
if number = guess then
puts(1,"You guessed correctly!\n")
exit
elsif number < guess then
puts(1,"You guessed too high.\nTry again: ")
else
puts(1,"You guessed to low.\nTry again: ")
end if
end while```

## F#

```open System

[<EntryPoint>]
let main argv =
let aim =
let from = 1
let upto = 100
let rnd = System.Random()
Console.WriteLine("Hi, you have to guess a number between {0} and  {1}",from,upto)
rnd.Next(from,upto)

let mutable correct = false
while not correct do

let guess =
| true, number -> Some(number)
| false,_ -> None

match guess with
| Some(number) ->
match number with
| number when number > aim ->   Console.WriteLine("You guessed to high!")
| number when number < aim ->   Console.WriteLine("You guessed to low!")
| _                        ->   Console.WriteLine("You guessed right!")
correct <- true
| None -> Console.WriteLine("Error: You didn't entered a parsable number!")

0
```

## Factor

```USING:
formatting
fry
io
kernel
math math.parser math.ranges
prettyprint
random ;
IN: guessnumber

: game-step ( target -- ? )
[
2dup =
[ 2drop f "Correct!" ]
[ < "high" "low" ? "Guess too %s, try again." sprintf t swap ]
if
]
[ drop t "Invalid guess." ]
if* print flush ;

: main ( -- )
99 [1,b]
[ unparse "Number in range %s, your guess?\n" printf flush ]
[ random '[ _ game-step ] loop ]
bi ;
```

## Fantom

```class Main
{
public static Void main ()
{
Int lowerLimit := 1
Int higherLimit := 100

Int target := (lowerLimit..higherLimit).random
Int guess
while (guess != target)
{
echo ("Enter a guess: ")
try
{
// read in a line of input, and try to interpret as an Int
if (guess == target)
{
echo ("Well guessed!")
}
else if (guess < target)
{
echo ("Failed - your guess is too small")
}
else // if (guess > target)
{
echo ("Failed - your guess is too large")
}
}
catch (Err e)
{
echo ("Your guess must be an integer")
}
}
}
}```

Sample game:

```Enter a guess:
50
Failed - your guess is too small
Enter a guess:
75
Failed - your guess is too large
Enter a guess:
67
Failed - your guess is too large
Enter a guess:
60
Failed - your guess is too large
Enter a guess:
55
Failed - your guess is too large
Enter a guess:
53
Failed - your guess is too small
Enter a guess:
54
Well guessed!
```

## FOCAL

```01.01 S T=0
01.02 A "LOWER LIMIT",L
01.03 A "UPPER LIMIT",H
01.04 I (H-L)1.05,1.05,1.06
01.05 T "INVALID RANGE",!;G 1.02
01.06 S S=FITR(L+FRAN()*(H-L+1))
01.10 A "GUESS",G
01.11 I (H-G)1.13,1.12,1.12
01.12 I (G-L)1.13,1.14,1.14
01.13 T "OUT OF RANGE",!;G 1.1
01.14 S T=T+1
01.15 I (G-S)1.16,1.17,1.18
01.16 T "TOO LOW!",!;G 1.1
01.17 T "CORRECT! GUESSES",%4,T,!;Q
01.18 T "TOO HIGH!",!;G 1.1```
Output:
```LOWER LIMIT:1
UPPER LIMIT:100
GUESS:0
OUT OF RANGE
GUESS:101
OUT OF RANGE
GUESS:50
TOO LOW!
GUESS:75
TOO LOW!
GUESS:87
TOO LOW!
GUESS:94
TOO HIGH!
GUESS:90
CORRECT! GUESSES=    5```

## Fortran

Works with: Fortran version 95 and later
```program Guess_a_number
implicit none

integer, parameter :: limit = 100
integer :: guess, number
real :: rnum

write(*, "(a, i0, a)") "I have chosen a number between 1 and ", limit, &
" and you have to try to guess it."
write(*, "(a/)")  "I will score your guess by indicating whether it is higher, lower or the same as that number"

call random_seed
call random_number(rnum)
number = rnum * limit + 1
do
write(*, "(a)", advance="no") "Enter quess: "
if(guess < number) then
write(*, "(a/)") "That is lower"
else if(guess > number) then
write(*, "(a/)") "That is higher"
else
write(*, "(a)") "That is correct"
exit
end if
end do
end program
```

Output

```I have chosen a number bewteen 1 and 100 and you have to try to guess it.
I will score your guess by indicating whether it is higher, lower or the same as that number.

Enter guess: 50
That is lower

Enter guess: 75
That is higher

Enter guess: 62
That is higher

Enter guess: 57
That is correct```

## FreeBASIC

```' FB 1.05.0 Win64

Randomize
Dim n As Integer = Int(Rnd * 20) + 1
Dim guess As Integer

Print "Guess which number I've chosen in the range 1 to 20"
Print
Do
Input " Your guess : "; guess
If guess > n AndAlso guess <= 20 Then
Print "Your guess is higher than the chosen number, try again "
ElseIf guess = n Then
Print "Correct, well guessed!"
Exit Do
ElseIf guess < n AndAlso 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
```

Sample input/output

Output:
``` Your guess : ? 14
Your guess is lower than the chosen number, try again
Your guess is lower than the chosen number, try again
Your guess is lower than the chosen number, try again
Correct, well guessed!
```

## Frink

```// Guess a Number with feedback.
target = random[1,100] // Min and max are both inclusive for the random function
guess = 0
println["Welcome to guess a number! I've picked a number between 1 and 100. Try to guess it!"]
while guess != target
{
guessStr = input["What is your guess?"]
guess = parseInt[guessStr]
if guess == undef
{
println["\$guessStr is not a valid guess. Please enter a number from 1 to 100."]
next
}
if guess < target
println["My number is higher than \$guess."]
if guess > target
println["My number is lower than \$guess."]
if guess == target
println["\$guess is correct! Well guessed!"]
}```
Output:

Including an example with a non-integer entered.

```Welcome to guess a number! I've picked a number between 1 and 100. Try to guess it!
My number is lower than 50.
My number is higher than 25.
ABC is not a valid guess. Please enter a number from 1 to 100.
My number is lower than 37.
My number is higher than 31.
34 is correct! Well guessed!
```

## FTCBASIC

```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
```

## 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
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
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```

## Genie

```[indent=4]
/*
Number guessing with feedback, in Genie

valac numberGuessing.gs
./numberGuessing
*/
class NumberGuessing

prop min:int
prop max:int

construct(m:int, n:int)
self.min = m
self.max = n

def start()
try_count:int = 0
number:int = Random.int_range(min, max)

stdout.printf("Welcome to Number Guessing!\n\n")
stdout.printf("I have thought up a number between %d and %d\n", min, max)
stdout.printf("which you have to guess. I will give hints as we go.\n\n")

while true
stdout.printf("Try #%d, ", ++try_count)
stdout.printf("please enter a number between %d and %d: ", min, max)
if line is null
stdout.printf("bailing...\n")
break

input:int64 = 0
unparsed:string = ""
converted:bool = int64.try_parse(line, out input, out unparsed)
if not converted or line is unparsed
stdout.printf("Sorry, input seems invalid\n")
continue

guess:int = (int)input
if number is guess
stdout.printf("Congratulations! You win.\n")
break
else
stdout.printf("Try again. The number in mind is %s than %d.\n",
number > guess ? "greater" : "less", guess)

init
var game = new NumberGuessing(1, 100)
game.start()```
Output:
```prompt\$ valac numberGuessing.gs
prompt\$ ./numberGuessing
Welcome to Number Guessing!

I have thought up a number between 1 and 100
which you have to guess. I will give hints as we go.

Try #1, please enter a number between 1 and 100: 50
Try again. The number in mind is greater than 50.
Try #2, please enter a number between 1 and 100: abc
Sorry, input seems invalid
Try #3, please enter a number between 1 and 100: 75
Try again. The number in mind is greater than 75.
Try #4, please enter a number between 1 and 100: 88
Try again. The number in mind is less than 88.
Try #5, please enter a number between 1 and 100: 81
Try again. The number in mind is greater than 81.
Try #6, please enter a number between 1 and 100: 84
Try again. The number in mind is less than 84.
Try #7, please enter a number between 1 and 100: 82
Try again. The number in mind is greater than 82.
Try #8, please enter a number between 1 and 100: 83
Congratulations! You win.

prompt\$ ./numberGuessing
Welcome to Number Guessing!

I have thought up a number between 1 and 100
which you have to guess. I will give hints as we go.

Try #1, please enter a number between 1 and 100: 50
Try again. The number in mind is greater than 50.
Try #2, please enter a number between 1 and 100: bailing...```

## Go

```package main

import (
"fmt"
"math/rand"
"time"
)

const lower, upper = 1, 100

func main() {
fmt.Printf("Guess integer number from %d to %d: ", lower, upper)
rand.Seed(time.Now().Unix())
n := rand.Intn(upper-lower+1) + lower
for guess := n; ; {
switch _, err := fmt.Scan(&guess); {
case err != nil:
fmt.Println("\n", err, "So, bye.")
return
case guess < n:
fmt.Print("Too low. Try again: ")
case guess > n:
fmt.Print("Too high. Try again: ")
default:
fmt.Println("Well guessed!")
return
}
}
}
```

## Groovy

Based on the Java implementation

```def rand = new Random() // java.util.Random
def range = 1..100 // Range (inclusive)
def number = rand.nextInt(range.size()) + range.from // get a random number in the range

println "The number is in \${range.toString()}" // print the range

def guess
while (guess != number) { // keep running until correct guess
try {
print 'Guess the number: '
switch (guess) { // check the guess against number
case { it < number }: println 'Your guess is too low'; break
case { it > number }: println 'Your guess is too high'; break
default:              println 'Your guess is spot on!'; break
}
} catch (NumberFormatException ignored) { // catches all input that is not a number
}
}
```

Example:

```The number is in 1..100
Guess the number: ghfvkghj
Guess the number: 50
Guess the number: 75
Guess the number: 83
Guess the number: 90
Guess the number: 95
Guess the number: 92
```

```import Control.Monad
import System.Random

-- Repeat the action until the predicate is true.
until_ act pred = act >>= pred >>= flip unless (until_ act pred)

case compare ans guess of
LT -> putStrLn "Too high. Guess again." >> return False
EQ -> putStrLn "You got it!" >> return True
GT -> putStrLn "Too low. Guess again." >> return False

-- Repeatedly read until the input *starts* with a number.  (Since
ask = do line <- getLine
((num,_):_) -> return num

main = do
ans <- randomRIO (1,100) :: IO Int
putStrLn "Try to guess my secret number between 1 and 100."
```

## HolyC

```U8 n, *g;
U8 min = 1, max = 100;

n = min + RandU16 % max;

Print("Guess the number between %d and %d: ", min, max);

while(1) {
g = GetStr;

if (Str2I64(g) == n) {
Print("You guessed correctly!\n");
break;
}

if (Str2I64(g) < n)
Print("Your guess was too low.\nTry again: ");
if (Str2I64(g) > n)
Print("Your guess was too high.\nTry again: ");
}```

## Icon and Unicon

```procedure main()
smallest := 5
highest := 25
n := smallest-1 + ?(1+highest-smallest)
repeat {
writes("Pick a number from ", smallest, " through ", highest, ": ")

if n = numeric(guess)
then {
write ("Well guessed!")
exit ()
}
else if n < numeric(guess)
then write ("Your guess is too high")
else if n > numeric(guess)
then write ("Your guess is too low")
else write ("Did you enter a number?")
}
end
```

Output:

```\$ ./guess-feedback
Pick a number from 5 through 25: 10
Pick a number from 5 through 25: 16
Pick a number from 5 through 25: 20
Pick a number from 5 through 25: 23
Pick a number from 5 through 25: 22
Well guessed!
```

## J

```require 'misc'
game=: verb define
assert. y -: 1 >. <.{.y
n=: 1 + ?y
smoutput 'Guess my integer, which is bounded by 1 and ',":y
whilst. -. x -: n do.
x=. {. 0 ". prompt 'Guess: '
if. 0 -: x do. 'Giving up.' return. end.
smoutput (*x-n){::'You win.';'Too high.';'Too low.'
end.
)
```

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.

Example use:

```   game 100
Guess my integer, which is bounded by 1 and 100
Guess: 64
Too high.
Guess: 32
Too low.
Guess: 48
Too high.
Guess: 40
Too low.
Guess: 44
You win.
```

## Java

```import java.util.Random;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Random random = new Random();
long from = 1;
long to = 100;
int randomNumber = random.nextInt(to - from + 1) + from;
int guessedNumber = 0;

System.out.printf("The number is between %d and %d.\n", from, to);

do
{
System.out.print("Guess what the number is: ");
guessedNumber = scan.nextInt();
if (guessedNumber > randomNumber)
else if (guessedNumber < randomNumber)
else
System.out.println("You got it!");
} while (guessedNumber != randomNumber);
}
}
```

Demonstration:

```The number is between 1 and 100.
Guess what the number is: 50
Guess what the number is: 25
Guess what the number is: 17
Guess what the number is: 10
Guess what the number is: 5
Guess what the number is: 7
You got it!```

## JavaScript

```<p>Pick a number between 1 and 100.</p>
<form id="guessNumber">
<input type="text" name="guess">
<input type="submit" value="Submit Guess">
</form>
<p id="output"></p>
<script type="text/javascript">
```
```var number = Math.ceil(Math.random() * 100);

function verify() {
var guess = Number(this.elements.guess.value),
output = document.getElementById('output');

if (isNaN(guess)) {
output.innerHTML = 'Enter a number.';
} else if (number === guess) {
output.innerHTML = 'You guessed right!';
} else if (guess > 100) {
output.innerHTML = 'Your guess is out of the 1 to 100 range.';
} else if (guess > number) {
output.innerHTML = 'Your guess is too high.';
} else if (guess < number) {
output.innerHTML = 'Your guess is too low.';
}
return false;
}

document.getElementById('guessNumber').onsubmit = verify;
```
```</script>
```

### Spidermonkey Version

```#!/usr/bin/env js

function main() {
guessTheNumber(1, 100);
}

function guessTheNumber(low, high) {
var num = randOnRange(low, high);
var guessCount = 0;

function checkGuess(n) {
if (n < low || n > high) {
print('That number is not between ' + low + ' and ' + high + '!');
return false;
}

if (n == num) {
print('You got it in ' + String(guessCount) + ' tries.');
return true;
}

if (n < num) {
print('Too low.');
} else {
print('Too high.');
}
return false;
}

print('I have picked a number between ' + low + ' and ' + high + '. Try to guess it!');
while (true) {
guessCount++;
if (checkGuess(n)) break;
}
}

function randOnRange(low, high) {
var r = Math.random();
return Math.floor(r * (high - low + 1)) + low;
}

main();
```

Example session:

```I have picked a number between 1 and 100. Try to guess it!
Too low.
Too high.
Too high.
Too low.
Too high.
You got it in 6 tries.
```

## 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

```# \$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```

Transcript:

```\$ 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
\$```

## Julia

Works with: Julia version 0.6
```function guesswithfeedback(n::Integer)
number = rand(1:n)
print("I choose a number between 1 and \$n\nYour guess? ")
while (guess = readline()) != dec(number)
if all(isdigit, guess)
print("Too ", parse(Int, guess) < number ? "small" : "big")
else
end
print(", new guess? ")
end
println("You guessed right!")
end

guesswithfeedback(10)
```
Output:
```I choose a number between 1 and 10
Too big, new guess? 1
Too small, new guess? 5
Too small, new guess? 6
Too small, new guess? 7
You guessed right!```

## Kotlin

```import kotlin.random.Random

fun main() {
val n = 1 + rand.nextInt(20)
println("Guess which number I've chosen in the range 1 to 20\n")
while (true) {
when (guess) {
n              ->  { println("Correct, well guessed!") ; return }
in n + 1 .. 20 ->    println("Your guess is higher than the chosen number, try again")
in 1 .. n - 1  ->    println("Your guess is lower than the chosen number, try again")
else           ->    println("Your guess is inappropriate, try again")
}
}
}
```

Sample inout/output

Output:
```Guess which number I've chosen in the range 1 to 20

Your guess is higher than the chosen number, try again
Your guess is higher than the chosen number, try again
Your guess is higher than the chosen number, try again
Correct, well guessed!
```

## Lambdatalk

```{def game

{def game.rec                                  // recursive part
{lambda {:n :l :h}
{let { {:n :n} {:l :l} {:h :h}               // :n, :l, :h redefined
{:g {round {/ {+ :l :h} 2}}}}         // :g is the middle
{if {< :g :n}
then {br}:g too low!
{game.rec :n {+ :g 1} :h}             // do it again higher
else {if {> :g :n}
then {br}:g too high!
{game.rec :n :l {- :g 1}}             // do it again lower
else {br}{b :g Got it!} }} }}}             // bingo!

{lambda {:n}
{let { {:n :n}                                // :n redefined
{:N {floor {* :n {random}}}}}          // compute a random number
Find {b :N} between 0 and :n {game.rec :N 0 :n}
}}}

{game {pow 2 32}}       // 2**32 = 4294967296
```

Sample inout/output

Output:
```Find 2037303041 between 0 and 4294967296
2147483648 too high!
1073741824 too low!
1610612736 too low!
1879048192 too low!
2013265920 too low!
2080374784 too high!
2046820352 too high!
2030043136 too low!
2038431744 too high!
2034237440 too low!
2036334592 too low!
2037383168 too high!
2036858880 too low!
2037121024 too low!
2037252096 too low!
2037317632 too high!
2037284864 too low!
2037301248 too low!
2037309440 too high!
2037305344 too high!
2037303296 too high!
2037302272 too low!
2037302784 too low!
2037303040 too low!
2037303168 too high!
2037303104 too high!
2037303072 too high!
2037303056 too high!
2037303048 too high!
2037303044 too high!
2037303042 too high!
2037303041 Got it!
```

## Lasso

```#!/usr/bin/lasso9

local(
lower	= integer_random(10, 1),
higher	= integer_random(100, 20),
number	= integer_random(#higher, #lower),
status	= false,
guess
)

// prompt for a number
stdout('Guess a number: ')

while(not #status) => {
#guess = null

// the following bits wait until the terminal gives you back a line of input
while(not #guess or #guess -> size == 0) => {
#guess = file_stdin -> readSomeBytes(1024, 1000)
}
#guess = integer(#guess)

if(not (range(#guess, #lower, #higher) == #guess)) => {
stdout('Input not of correct type or range. Guess a number: ')
else(#guess > #number)
stdout('That was to high, try again! ')
else(#guess < #number)
stdout('That was to low, try again! ')
else(#guess == #number)
stdout('Well guessed!')
#status = true
}
}
```

With range value 8 and 73. Correct number 13

Output:

```Guess a number: 70
That was to high, try again! 35
That was to high, try again! 14
That was to high, try again! 7
Input not of correct type or range. Guess a number: 10
That was to low, try again! 12
That was to low, try again! 13
Well guessed!
```

## LFE

```(defmodule guessing-game
(export (main 0)))

(defun get-player-guess ()
(let (((tuple 'ok (list guessed)) (: io fread '"Guess number: " '"~d")))
guessed))

(cond
(: io format '"Well-guessed!!~n"))
(if (> answer guessed) (: io format '"Your guess is too low.~n"))
(if (< answer guessed) (: io format '"Your guess is too high.~n"))

(defun main ()
(: io format '"Guess the number I have chosen, between 1 and 10.~n")
(check-guess
(: random uniform 10)
(get-player-guess)))
```

From the LFE REPL (assuming the above code was saved in the file "guessing-game.lfe"):

```> (slurp '"guessing-game.lfe")
#(ok guessing-game)
> (main)
Guess the number I have chosen, between 1 and 10.
Guess number: 10
Guess number: 1
Guess number: 5
Guess number: 7
Well-guessed!!
ok
>
```

## Liberty BASIC

```[start]
target = int( rnd( 1) * 100) +1

while 1
do
input "Guess a whole number between 1 and 100. To finish, type 'exit' "; b\$
if b\$ ="exit" then print "Thank you for playing!": end
c = val( b\$)
ok =( c =int( c)) and ( c >=1) and ( c <=100)
if ok =0 then notice "Invalid data. Integers 1 to 100 only."
loop until ok <>0

if c =target       then print "      You guessed correctly.": print: goto [start]
if c <target       then print "   Your guess was too low."
if c >target       then print "   Your guess was too high."
wend```

## LiveCode

```command guessTheNumber lowN highN
local tNumber, tguess, tmin, tmax
if lowN is empty or lowN < 1 then
put 1 into tmin
else
put lowN into tmin
end if
if highN is empty then
put 10 into tmax
else
put highN into tmax
end if
put random(tmax - tmin + 1) + tmin - 1 into tNumber
repeat until tguess is tNumber
ask question "Please enter a number between" && tmin && "and" && tmax titled "Guess the number"
if it is not empty then
put it into tguess
if tguess is tNumber then
else if tguess < tNumber then
else
end if
else
exit repeat
end if
end repeat
end guessTheNumber```

Test

```command testGuessNumber
guessTheNumber  --defaults to 1-10
guessTheNumber 9,12
end testGuessNumber```

## Locomotive Basic

```10 CLS:RANDOMIZE TIME
20 PRINT "Please specify lower and upper limits":guess=0
30 INPUT "  (must be positive integers) :", first, last
40 IF first<1 OR last<1 THEN 20
50 num=INT(RND*(last-first+1)+first)
60 WHILE num<>guess
70 INPUT "Your guess? ", guess
80 IF guess<num THEN PRINT "too small!"
90 IF guess>num THEN PRINT "too large!"
100 WEND
110 INPUT "That's correct! Another game (y/n)? ", yn\$
120 IF yn\$="y" THEN 20
```

Output:

## Logo

Translation of: UNIX Shell
```to guess [:max 100]
local "number
make "number random :max
local "guesses
make "guesses 0

local "guess
forever [
(type [Guess my number! \(range 1 -\ ] :max "\):\ )
ifelse (or (not numberp :guess) (lessp :guess 1) (greaterp :guess :max)) [
print sentence [Guess must be a number between 1 and] (word :max ".)
] [
make "guesses (:guesses + 1)
ifelse lessp :guess :number [
print [Too low!]
] [ifelse equalp :guess :number [
(print [You got it in] :guesses "guesses!)
stop
] [
print [Too high!]
]]
]
]
end```

Sample run:

```? guess
Guess my number! (range 1 - 100): 50
Too low!
Guess my number! (range 1 - 100): 75
Too high!
Guess my number! (range 1 - 100): 67
Too low!
Guess my number! (range 1 - 100): 71
Too high!
Guess my number! (range 1 - 100): 69
You got it in 5 guesses!
```

## Lua

```math.randomseed(os.time())
me_win=false
my_number=math.random(1,10)
while me_win==false do
print "Guess my number from 1 to 10:"
if type(tonumber(your_number))=="number" then
your_number=tonumber(your_number)
if your_number>10 or your_number<1 then
print "Your number was not between 1 and 10, try again."
elseif your_number>my_number then
print "Your number is greater than mine, try again."
elseif your_number<my_number then
print "Your number is smaller than mine, try again."
elseif your_number==my_number then
print "That was correct."
me_win=true
end
else
print "Your input was not a number, try again."
end
end
```
```Output:

Guess my number from 1 to 10:
4
Your number is smaller than mine, try again.
Guess my number from 1 to 10:
8
Your number is greater than mine, try again.
Guess my number from 1 to 10:
5
That was correct.
```

## M2000 Interpreter

Translation of: BASIC256
```Module GuessNumber {
chosen = Random(Min, Max)
print "guess a whole number between ";Min;" and ";Max
do {
\\ we use guess so Input get integer value
\\ if we press enter without a number we get error
do {
\\ if we get error then we change line, checking the cursor position
If Pos>0 then Print
Try ok {
input "Enter your number " , guess%
}
} until ok
Select Case guess%
case  min to chosen-1
print "Sorry, your number was too low"
case chosen+1 to max
print "Sorry, your number was too high"
case chosen
print "Well guessed!"
else case
print "That was an invalid number"
end select
} until guess% = chosen
}
GuessNumber 5, 15```
Output:

same as BASIC256

## Maple

```GuessANumber := proc(low, high)
local number, input;
randomize():
printf( "Guess a number between %d and %d:\n:> ", low, high );
number := rand(low..high)();
do
if input > number then
printf("Too high, try again!\n:> ");
elif input < number then
printf("Too low, try again!\n:> ");
else
printf("Well guessed! The answer was %d.\n", number);
break;
end if;
end do:
end proc:```
`GuessANumber(2,5);`

## Mathematica / Wolfram Language

```guessnumber[min_, max_] :=
Module[{number = RandomInteger[{min, max}], guess},
While[guess =!= number,
guess = Input[
If[guess > number, "Too high.Guess again.",
"Too low.Guess again.",
"Guess a number between " <> ToString@min <> " and " <>
ToString@max <> "."]]];
CreateDialog[{"Well guessed!", DefaultButton[]}]];
guessnumber[1, 10]
```

## MATLAB / Octave

 This example is untested. Please check that it's correct, debug it as necessary, and remove this message.

Tested in MATLAB. Untested in Octave.

```function guess_a_number(low, high)

if nargin < 1 || ~isnumeric(low) || length(low) > 1 || isnan(low)
low = 1;
end;
if nargin < 2 || ~isnumeric(high) || length(high) > 1 || isnan(high)
high = low+9;
elseif low > high
[low, high] = deal(high, low);
end

n = floor(rand(1)*(high-low+1))+low;
gs = input(sprintf('Guess an integer between %i and %i (inclusive): ', low, high), 's');
while gs    % No guess quits the game
g = str2double(gs);
if length(g) > 1 || isnan(g) || g < low || g > high
gs = input('Invalid input, guess again: ', 's');
elseif g < n
gs = input('Too low, guess again: ', 's');
elseif g > n
gs = input('Too high, guess again: ', 's');
else
disp('Good job, you win!')
gs = '';
end
end
```

## MAXScript

```Range = [1,100]
randomNumber = (random Range.x Range.y) as integer
clearListener()
while true do
(
userVal = getKBValue prompt:("Enter a number between "+(range[1] as integer) as string+" and "+(range[2] as integer) as string+": ")
if userVal == randomNumber do (format "\nWell guessed!\n"; exit with OK)
case of
(
(classOf userVal != classof randomNumber): (format "\nBad number!\n")
(userVal > Range[2] or userVal < Range[1]): (format "\nNumber out of range\n")
(userVal > randomNumber): (format "\nToo high!\n")
(userVal < randomNumber): (format "\nToo low!\n")
)
)```

Output:

```Enter a number between 1 and 100:  5
Too low!
Enter a number between 1 and 100:  9.0
Enter a number between 1 and 100:  145
Number out of range
Enter a number between 1 and 100:  95
Too low!
Enter a number between 1 and 100:  99
Too high!
Enter a number between 1 and 100:  97
Well guessed!
OK```

## Mirah

```def getInput:int
Integer.parseInt(s)
end

number = int(Math.random() * 10 + 1)

puts "Guess the number between 1 and 10"

guessed = false

while !guessed do
begin
userNumber = getInput
if userNumber == number
guessed = true
puts "You guessed it."
elsif userNumber > number
puts "Too high."
else
puts "Too low."
end
rescue NumberFormatException => e
end
end```

## Modula-2

```MODULE guessf;

IMPORT  InOut, Random, NumConv, Strings;

VAR     number, guess           : CARDINAL;
input                   : Strings.String;
OK, Done                : BOOLEAN;

BEGIN
number := Random.nr (1000);
InOut.WriteString ("I have chosen a number below 1000; please try to guess it.");
InOut.WriteLn;
REPEAT
REPEAT
InOut.WriteString ("Enter your guess : ");        InOut.WriteBf;
NumConv.Str2Num (guess, 10, input, OK);
IF  NOT OK  THEN
InOut.WriteString (input);
InOut.WriteString (" is not a valid number...");
InOut.WriteLn
END
UNTIL  OK;
IF  number = guess  THEN
Done := TRUE;
InOut.WriteString ("spot on!")
ELSE
Done := FALSE;
IF  guess > number  THEN
InOut.WriteString ("too high.")
ELSE
InOut.WriteString ("too low.")
END
END;
InOut.WriteLn
UNTIL  Done;
InOut.WriteString ("Thank you for playing; have a nice day!");
InOut.WriteLn
END guessf.
```
```I have chosen a number below 1000; please try to guess it.
kwak is not a valid number...
Thank you for playing; have a nice day!```

## Nanoquery

```import Nanoquery.Util

random = new(Random)
inclusive_range = {1, 100}

print format("Guess my target number that is between %d and %d (inclusive).\n",\
inclusive_range[0], inclusive_range[1])
target = random.getInt(inclusive_range[1]) + inclusive_range[0]
i = 0
i += 1
txt = input()

valid = true
try
catch
println format("  I don't understand you input of '%s' ?", txt)
value = false
end

if valid
println "  Out of range!"
println "  Ye-Haw!!"
println "  Too low."
println "  Too high."
end
end
end

println "\nThanks for playing."```

## Nemerle

```using System;
using System.Console;

module GuessHints
{
Main() : void
{
def     rand   = Random();
def     secret = rand.Next(1, 101);
mutable guess  = 0;

WriteLine("Guess a number between 1 and 100:");

do
{
guess = GetGuess();
match(guess.CompareTo(secret))
{
|(-1) => WriteLine("Too low! Guess again:")
|1    => WriteLine("Too high! Guess again:")
|0    => WriteLine("Well guessed!")
}
} while (guess != secret)
}
}
```

## NetRexx

```/* NetRexx */
options replace format comments java crossref symbols nobinary

parse arg lo hi .
if lo = '' | lo = '.' then lo = 1
if hi = '' | hi = '.' then hi = 100
if lo > hi then parse (hi lo) lo hi -- make sure lo is < hi
rg = Random()
tries = 0
guessThis = rg.nextInt(hi - lo) + lo

say
say 'Rules:  Guess a number between' lo 'and' hi
say '        Use QUIT or . to stop the game'
say '        Use TELL to get the game to tell you the answer.'
say
say 'Starting...'
say

loop label g_ forever
say  'Try guessing a number between' lo 'and' hi
select
when guess.upper = 'QUIT' | guess = '.' then do
say 'You asked to leave the game.  Goodbye...'
leave g_
end
when guess.upper = 'TELL' | guess = '.' then do
say 'The number you were looking for is' guessThis
end
when \guess.datatype('w') then do
say guess 'is not a whole number.  Try again.'
end
when guess = guessThis then do
tries = tries + 1
say 'Well guessed!' guess 'is the correct number.'
say 'It took you' tries 'tries.'
leave g_
end
when guess < lo then do
tries = tries + 1
say guess 'is below the lower limit of' lo
end
when guess > hi then do
tries = tries + 1
say guess 'is above the upper limit of' hi
end
when guess < guessThis then do
tries = tries + 1
say guess 'is too low. Try again.'
end
when guess > guessThis then do
tries = tries + 1
say guess 'is too high. Try again.'
end
otherwise do
say guess 'is an unexpected value.'
end
end
end g_

return
```

## NewLISP

```; guess-number-feedback.lsp
; oofoe 2012-01-19
; http://rosettacode.org/wiki/Guess_the_number/With_feedback

(seed (time-of-day)) ; Initialize random number generator from clock.
(setq low  -5
high 62
number (+ low (rand (- high low)))
found nil
)

(print "I'm thinking of a number between " low " and " high ".")

(print " What's your guess? ")
(print (cond ((= 'bad guess) "That's not a number! Try again!")
((or (< guess low) (> guess high))
(string "My number is between " low
" and " high ". Try again!"))
((< number guess) "Try a little lower...")
((> number guess) "Maybe a bit higher...")
((= number guess) (setq found true) "Exactly right!")))
)

(println "\nWell guessed! Congratulations!")

(exit)
```

Sample output:

```I'm thinking of a number between -5 and 62. What's your guess? No idea.
That's not a number! Try again! What's your guess? 99
My number is between -5 and 62. Try again! What's your guess? -5
Maybe a bit higher... What's your guess? 30
Try a little lower... What's your guess? 15
Try a little lower... What's your guess? 0
Maybe a bit higher... What's your guess? 3
Try a little lower... What's your guess? 1
Exactly right!
Well guessed! Congratulations!

```

## Nim

```import random, strutils

randomize()

let iRange = 1..100

echo "Guess my target number that is between ", iRange.a, " and ", iRange.b, " (inclusive)."
let target = rand(iRange)
inc i
stdout.write "Your guess ", i, ": "
except ValueError:
echo "  I don't understand your input of '", txt, "'"
continue
if answer notin iRange: echo "  Out of range!"
elif answer < target: echo "  Too low."
elif answer > target: echo "  Too high."
else: echo "  Ye-Haw!!"

echo "Thanks for playing."
```

## NS-HUBASIC

```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."```

## Objeck

```use IO;

bundle Default {
class GuessNumber {
function : Main(args : String[]) ~ Nil {
Guess();
}
function : native : Guess() ~ Nil {
done := false;
"Guess the number which is between 1 and 10 or 'n' to quite: "->PrintLine();
rand_num := (Float->Random() * 10.0)->As(Int) + 1;
while(done = false) {
number := guess->ToInt();
if(number <> 0) {
if(number <> rand_num) {
->Print(number < rand_num ? "low" : "high")
->Print(".\nGuess again: ");
}
else {
"Hurray! You guessed correctly!"->PrintLine();
done := true;
};
}
else {
if(guess->StartsWith("q") | guess->StartsWith("Q")) {
done := true;
};
};
};
}
}
}```

## OCaml

```let rec _read_int() =
with _ ->
print_endline "Please give a cardinal numbers.";
(* TODO: what is the correct word? cipher, digit, figure or numeral? *)

let () =
print_endline "Please give a set limits (two integers):";
let a, b =
if a < b
then (a, b)
else (b, a)
in
Random.self_init();
let target = a + Random.int (b - a) in
Printf.printf "I have choosen a number between %d and %d\n%!" a b;
let rec loop () =
if guess = target then
begin
print_endline "The guess was equal to the target.\nCongratulation!";
exit 0
end;
if guess < a || guess > b then
print_endline "The input was inappropriate."
else if guess > target then
print_endline "The guess was higher than the target."
else if guess < target then
print_endline "The guess was less than the target.";
loop ()
in
loop ()
```

Playing the game:

```\$ ocaml inapropriate.ml
Please give a set limits (two integers):
3
7
I have choosen a number between 3 and 7
6
The guess was higher than the target.
7
The guess was higher than the target.
8
The input was inappropriate.
3
The guess was less than the target.
4
The guess was equal to the target.
Congratulation!```
```\$ ocaml inapropriate.ml
Please give a set limits (two integers):
2
6
I have choosen a number between 2 and 6
three
```

## Octave

```function guess_a_number(low,high)
% Guess a number (with feedback)
% http://rosettacode.org/wiki/Guess_the_number/With_feedback

if nargin<1,
low=1;
end;
if nargin<2,
high=10;
end;

n = floor(rand(1)*(high-low+1))+low;
[guess,state] = str2num(input(sprintf('Guess a number between %i and %i: ',low,high),'s'));
while (~state || guess~=n)
if guess < n,
g = input('to low, guess again: ','s');
[guess, state] = str2num(g);
elseif guess > n,
g = input('to high, guess again: ','s');
[guess, state] = str2num(g);
end;
while ~state
g = input('invalid input, guess again: ','s');
[guess, state] = str2num(g);
end
end
disp('Well guessed!')
```

## Oforth

```import: console

: guessNumber(a, b)
| n g |
b a - rand a + 1- ->n
begin
"Guess a number between" . a . "and" . b . ":" .
while(System.Console askln asInteger dup -> g isNull) [ "Not a number " println ]
g n == ifTrue: [ "You found it !" .cr return ]
g n <  ifTrue: [ "Less" ] else: [ "Greater" ] . "than the target" .cr
again ;```

## Ol

```(import (otus random!))

(define from 0)
(define to 100)

(define number (+ from (rand! (+ from to 1))))

(let loop ()
(for-each display `("Pick a number from " ,from " through " ,to ": "))
(cond
((not (number? guess))
(print "Not a number!")
(loop))
((or (< guess from)
(< to guess))
(print "Out of range!")
(loop))
((< guess number)
(print "Too low!")
(loop))
((> guess number)
(print "Too high!")
(loop))
((= guess number)
(print "Well guessed!")))))```

## ooRexx

While the program for REXX works perfectly well with ooRexx, here is a version written in an alternate (my) style.

```Select instead of a series of If's
different indentations.
entering ? shows the number we are looking for
```

This program should, of course, also work with all other Rexxes

```/*REXX program that plays the guessing (the number) game. */
low=1                /*lower range for the guessing game.*/
high=100              /*upper range for the guessing game.*/
try=0                 /*number of valid attempts.         */
r=random(1,100)       /*get a random number (low-->high). */

do forever
say
say "guess the number, it's between" low 'and' high '(inclusive)',
'or enter quit to end the game.'
say
pull g
say
g=space(g)
Select
When g='' then iterate
When g='QUIT' then exit
When g='?' then Do
Say 'The number you are looking for is' r
Iterate
End
When \datatype(g,'W') then do
call ser g "isn't a valid number"
iterate
end
When g<low then do
call ser g 'is below the lower limit of' low
iterate
end
When g>high then do
call ser g 'is above the higher limit of' high
iterate
end
When g=r then do
try=try+1
Leave
End
Otherwise Do
try=try+1
if g>r then what='high'
else what='low'
say 'your guess of' g 'is too' what'.'
end
end
end

say
tries='tries'
if try=1 then
say 'Congratulations!, you guessed the number in 1 try. Did you cheat?'
Else
say 'Congratulations!, you guessed the number in' try 'tries.'
say
exit

ser: say; say '*** error ! ***'; say arg(1); say; return
```

## PARI/GP

```guess_the_number(N=10)={
a=random(N);
print("guess the number between 0 and "N);
for(x=1,N,
if(x>1,
if(b>a,
print("guess again lower")
,
print("guess again higher")
);
b=input();
if(b==a,break())
);
print("You guessed it correctly")
};```

See Delphi

## Perl

```sub prompt {
my \$prompt = shift;
while (1) {
print "\n", \$prompt, ": ";
# type ^D, q, quit, quagmire, etc to quit
defined(\$_ = <STDIN>) and !/^\s*q/ or exit;

return \$_ if /^\s*\d+\s*\$/s;
\$prompt = "Please give a non-negative integer";
}
}

my \$tgt = int(rand prompt("Hola! Please tell me the upper bound") + 1);
my \$tries = 1;

\$tries++, print "You guessed too ", (\$_ == -1 ? "high" : "low"), ".\n"
while (\$_ = \$tgt <=> prompt "Your guess");

print "Correct! You guessed it after \$tries tries.\n";
```

## Phix

```--
-- demo\rosetta\Guess_the_number3.exw
--
with javascript_semantics
requires("1.0.1") -- (VALUECHANGED_CB fix)
include pGUI.e

constant lower_limit = 0, upper_limit = 100,
secret = rand_range(lower_limit,upper_limit),
fmt = "Enter your guess, a number between %d and %d",
prompt = sprintf(fmt,{lower_limit,upper_limit})

Ihandle dlg, label, input, state

function valuechanged_cb(Ihandle /*input*/)
integer guess = IupGetInt(input,"VALUE")
string msg = iff(guess<secret?"Too low":
iff(guess=secret?"You got it!":
iff(guess>secret?"Too high":
"uh?")))
IupSetAttribute(state,"TITLE",msg)
return IUP_CONTINUE
end function

IupOpen()
label = IupLabel(prompt)
state = IupLabel("","EXPAND=HORIZONTAL")
IupSetCallback(input,"VALUECHANGED_CB",Icallback("valuechanged_cb"))
{} = valuechanged_cb(input)
dlg = IupDialog(IupVbox({label,input,state},"MARGIN=15x15"),`TITLE="Guess the number"`)
IupShow(dlg)
if platform()!=JS then
IupMainLoop()
IupClose()
end if
```

## PHP

```<?php

session_start();

if(isset(\$_SESSION['number']))
{
\$number = \$_SESSION['number'];
}
else
{
\$_SESSION['number'] = rand(1,10);
}

if(\$_POST["guess"]){
\$guess  = htmlspecialchars(\$_POST['guess']);

echo \$guess . "<br />";
if (\$guess < \$number)
{
echo "Your guess is too low";
}
elseif(\$guess > \$number)
{
echo "Your guess is too high";
}
elseif(\$guess == \$number)
{
echo "You got the correct number!";
}

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Guess A Number</title>

<body>
<form action="<?=\$_SERVER['PHP_SELF'] ?>" method="post" name="guess-a-number">
<label for="guess">Guess A Number:</label><br/ >
<input type="text" name="guess" />
<input name="number" type="hidden" value="<?= \$number ?>" />
<input name="submit" type="submit" />
</form>
</body>
</html>
```

## PicoLisp

Translation of: PureBasic
```(de guessTheNumber ()
(use (Low High Guess)
(until
(and
(prin "Enter low limit : ")
(prin "Enter high limit: ")
(> High Low) ) )
(seed (time))
(let Number (rand Low High)
(loop
(prin "Guess what number I have: ")
(T (= Number (setq Guess (read)))
(prinl "You got it!") )
(prinl
(if (> Number Guess) "low" "high")
"." ) ) ) ) )```

Output:

```: (guessTheNumber)
Enter low limit : 1
Enter high limit: 64
Guess what number I have: 32
Guess what number I have: 16
Guess what number I have: 24
You got it!```

## Plain English

```The low number is 1.

The high number is 100.

To run:
Start up.
Play the guessing game.
Wait for the escape key.
Shut down.

To play the guessing game:
Pick a secret number between the low number and the high number.
Write "I chose a secret number between " then the low number then " and " then the high number then "." then the return byte on the console.
Loop.
Ask the user for a number.
If the number is the secret number, break.
If the number is less than the secret number, write "  Too low." on the console.
If the number is greater than the secret number, write "  Too high." on the console.
Repeat.
Write "  Well guessed!" on the console.

To ask the user for a number:
Read a string from the console.
If the string is not any integer, write "  Guess must be an integer." on the console; repeat.
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.```
Output:
```I chose a secret number between 1 and 100.

Guess must be an integer.
Guess must be an integer.
Guess can't be lower than 1.
Guess can't be higher than 100.
Too high.
Too low.
Too low.
Too low.
Too low.
Well guessed!
```

## PowerShell

```function Get-Guess
{
[int]\$number = 1..100 | Get-Random
[int]\$guess = 0
[int[]]\$guesses = @()

Write-Host "Guess a number between 1 and 100" -ForegroundColor Cyan

while (\$guess -ne \$number)
{
try
{

if (\$guess -lt \$number)
{
Write-Host "Greater than..."
}
elseif (\$guess -gt \$number)
{
Write-Host "Less than..."
}
else
{
Write-Host "You guessed it"
}
}
catch [Exception]
{
Write-Host "Input a number between 1 and 100." -ForegroundColor Yellow
continue
}

\$guesses += \$guess
}

[PSCustomObject]@{
Number  = \$number
Guesses = \$guesses
}
}

Write-Host ("The number was {0} and it took {1} guesses to find it." -f \$answer.Number, \$answer.Guesses.Count)
```
Output:
```Guess a number between 1 and 100
Guess: 50
Greater than...
Guess: 60
Greater than...
Guess: a
Input a number between 1 and 100.
Guess: 70
Greater than...
Guess: 80
Less than...
Guess: 75
Less than...
Guess: 73
You guessed it
The number was 73 and it took 6 guesses to find it.
```

## Prolog

Works with: SWI-Prolog version 6
```main :-
play_guess_number.

/* Parameteres */

low(1).
high(10).

/* Basic Game Logic */

play_guess_number :-
low(Low),
high(High),
random(Low, High, N),
tell_range(Low, High),
repeat,                         % roughly, "repeat ... (until) Guess == N "
give_feedback(N, Guess),
Guess == N.

/* IO Stuff */

tell_range(Low, High) :-
format('Guess an integer between ~d and ~d.~n', [Low,High]).

format('Guess the number: '),

give_feedback(N, Guess) :-
( \+integer(Guess) -> writeln('Invalid input.')
; Guess < N        -> writeln('Your guess is too low.')
; Guess > N        -> writeln('Your guess is too high.')
; Guess =:= N      -> writeln("Correct!")
).
```

Input in the standard Prolog top level is terminated with a `.`: E.g.,

```?- main.
Guess an integer between 1 and 10.
Guess the number: a.
Invalid input.
Guess the number: 3.
```

## PureBasic

```OpenConsole()

Repeat
; Ask for limits, with sanity check
Print("Enter low limit : "): low  =Val(Input())
Print("Enter high limit: "): High =Val(Input())
Until High>low

TheNumber=Random(High-low)+low
Debug TheNumber
Repeat
Print("Guess what number I have: "): Guess=Val(Input())
If Guess=TheNumber
PrintN("You got it!"): Break
ElseIf Guess < TheNumber
Else
EndIf
ForEver
```

## Python

```import random

inclusive_range = (1, 100)

print("Guess my target number that is between %i and %i (inclusive).\n"
% inclusive_range)
target = random.randint(*inclusive_range)
i += 1
txt = input("Your guess(%i): " % i)
try:
except ValueError:
print("  I don't understand your input of '%s' ?" % txt)
continue
print("  Out of range!")
continue
print("  Ye-Haw!!")
break
if answer < target: print("  Too low.")
if answer > target: print("  Too high.")

print("\nThanks for playing.")
```

Sample Game

```Guess my target number that is between 1 and 100 (inclusive).

Too high.
Too low.
Too high.
Too low.
Too high.
Too high.
Too high.
Ye-Haw!!

Thanks for playing.```

Sample trapped Errors

```Guess my target number that is between 1 and 100 (inclusive).

Out of range!
Out of range!
I don't understand your input of 'Howdy' ?

## 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 ( --> )```
Output:

In the Quackery shell.

```/O> guess-the-number
...
Guess the number (1-100 inclusive.)

That is not a number.
Too small.
Too small.
Too large.
Too large.
Too large.
Well done! 76 is correct.

Stack empty.

/O>```

## R

The previous solution lacked the required checks for if the input was appropriate, was bugged if low==high (R's sample function works differently if the input is of length 1), and behaved very strangely if low or high weren't whole numbers.

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 page entry dated 1st June 2020.

```guessANumber <- function(low, high)
{
boundryErrorCheck(low, high)
goal <- sample(low:high, size = 1)
guess <- getValidInput(paste0("I have a whole number between ", low, " and ", high, ". What's your guess? "))
while(guess != goal)
{
if(guess < low || guess > high){guess <- getValidInput("Out of range! Try again "); next}
if(guess > goal){guess <- getValidInput("Too high! Try again "); next}
if(guess < goal){guess <- getValidInput("Too low! Try again "); next}
}
"Winner!"
}

boundryErrorCheck <- function(low, high)
{
if(!is.numeric(low) || as.integer(low) != low) stop("Lower bound must be an integer. Try again.")
if(!is.numeric(high) || as.integer(high) != high) stop("Upper bound must be an integer. Try again.")
if(high < low) stop("Upper bound must be strictly greater than lower bound. Try again.")
if(low == high) stop("This game is impossible to lose. Try again.")
invisible()
}

#R's system for checking if numbers are integers is lacking (e.g. is.integer(1) returns FALSE)
#so we need this next function.
#A better way to check for integer inputs can be found in is.interger's docs, but this is easier to read.
#Note that readline outputs the user's input as a string, hence the need for type.convert.
getValidInput <- function(requestText)
{
while(!is.numeric(guess) || as.integer(guess) != guess){guess <- type.convert(readline("That wasn't an integer! Try again "))}
as.integer(guess)
}
```

## Racket

```#lang racket

(define (guess-number min max)
(define target (+ min (random (- max min -1))))
(printf "I'm thinking of a number between ~a and ~a\n" min max)
(printf "~a: " prompt)
(flush-output)
(define response
(cond [(not (exact-integer? guess)) "Please enter a valid integer"]
[(< guess target)             "Too low"]
[(> guess target)             "Too high"]
[else #f]))
(when response (printf "~a\n" response) (loop "Try again")))
(printf "Well guessed!\n"))

(guess-number 1 100)
```

## Raku

(formerly Perl 6)

```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";
\$maxnum = prompt("Please give me a valid upper boundary: ");
}

my \$count = 0;
my \$number = (1..\$maxnum).pick;

say "I'm thinking of a number from 1 to \$maxnum, try to guess it!";
repeat until my \$guessed-right {
when /^[e|q]/ { say 'Goodbye.'; exit; }
when not 1 <= \$_ <= \$maxnum {
say "You really should give me a number from 1 to \$maxnum."
}
\$count++;
when \$number { \$guessed-right = True }
when \$number < \$_ { say "Sorry, my number is smaller." }
when \$number > \$_ { say "Sorry, my number is bigger." }
}
}
say "Great you guessed right after \$count attempts!";
```
```Hello, please give me an upper boundary: 10
I'm thinking of a number from 1 to 10, try to guess it!
Sorry, my number is bigger.
Sorry, my number is smaller.
Great you guessed right after 3 attempts!```

## Retro

```: high|low ( gn-g\$ )
over > [ "high" ] [ "low" ] if ;

: checkGuess ( gn-gf || f )
2over = [ "You guessed correctly!\n" puts 2drop 0 ]
[ high|low "Sorry, your guess was too %s.\nTry again.\n" puts -1 ] if ;

: think ( -n )
random abs 100 mod 1+ ;

: guess ( - )
"I'm thinking of a number between 1 and 100.\n" puts
"Try to guess it!\n" puts
think [ getToken toNumber checkGuess ] while
"You got it!\n" puts ;```

## REXX

To make the program more engaging, randomized words for the hint are used.

```/*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   "    "   "      "      " */
try=   0                                       /*the number of valid (guess) attempts.*/
r= random(low, high)                         /*get a random number  (low ───► high).*/
lows= 'too_low  too_small too_little below under underneath   too_puny'
highs= 'too_high too_big   too_much   above over  over_the_top too_huge'
erm= '***error***'
@gtn= "guess the number, it's between"
prompt= centre(@gtn   low     'and'     high     '(inclusive)  ───or───  Quit:', 79, "─")
/* [↓]  BY 0 ─── used to  LEAVE  a loop*/
do ask=0  by 0;     say;      say prompt;      say;     pull g;     g= space(g);    say
do validate=0  by 0                          /*DO index required; LEAVE instruction.*/
select
when abbrev('QUIT', g, 1)  then exit                              /*what a whoos.*/
when words(g) \== 1        then say erm    'too many numbers entered:'        g
when \datatype(g, 'N')     then say erm g  "isn't numeric"
when \datatype(g, 'W')     then say erm g  "isn't a whole number"
when g<low                 then say erm g  'is below the lower limit of'     low
when g>high                then say erm g  'is above the higher limit of'   high
otherwise       leave  /*validate*/
end   /*select*/
end      /*validate*/

try= try + 1
if g=r  then leave
if g>r  then what= word(highs, random(1, words(highs) ) )
else what= word( lows, random(1, words( lows) ) )
say 'your guess of'     g     "is"      translate(what, , "_").
/*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."
```

## Ring

```fr = 1 t0 = 10
while true
see "Hey There,
========================
I'm thinking of a number between " + fr + " and " + t0 + ", Can you guess it??
Guess :> "
give x
n = nrandom(fr,t0)
if x = n see "

Congratulations :D

*****************************************************
** Your guess was right You Are Genius :D **
*****************************************************

"
exit
else
see "Oops its not true, you were just few steps"
if x > n see " up :)" else see " down :)" ok
see copy(nl,3)
ok
end

func nRandom s,e
while true
d = random(e)
if d >= s return d ok
end```

## Ruby

Translation of: Mirah
```number = rand(1..10)

puts "Guess the number between 1 and 10"

loop do
begin
user_number = Integer(gets)
if user_number == number
puts "You guessed it."
break
elsif user_number > number
puts "Too high."
else
puts "Too low."
end
rescue ArgumentError
end
end
```

## Rust

Library: rand
```use rand::Rng;
use std::cmp::Ordering;
use std::io;

const LOWEST: u32 = 1;
const HIGHEST: u32 = 100;

fn main() {

println!("I have chosen my number between {} and {}. Guess the number!", LOWEST, HIGHEST);

loop {

let mut guess = String::new();

io::stdin()

let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};

println!("You guessed: {}", guess);

match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}
```
```I have chosen my number between 1 and 100. Guess the number!
5
You guessed: 5
Too small!
50
You guessed: 50
Too small!
80
You guessed: 80
Too big!
67
You guessed: 67
Too big!
57
You guessed: 57
Too small!
63
You guessed: 63
Too small!
65
You guessed: 65
Too small!
66
You guessed: 66
You win!```

## Scala

```import java.util.Random
import java.util.Scanner

val scan = new Scanner(System.in)
val random = new Random
val (from , to) = (1, 100)
val randomNumber = random.nextInt(to - from + 1) + from
var guessedNumber = 0
printf("The number is between %d and %d.\n", from, to)

do {
print("Guess what the number is: ")
guessedNumber = scan.nextInt
if (guessedNumber > randomNumber) println("Your guess is too high!")
else if (guessedNumber < randomNumber) println("Your guess is too low!")
else println("You got it!")
} while (guessedNumber != randomNumber)
```

## Scheme

Works with: Chicken Scheme
Works with: Guile
```(define maximum 5)
(define minimum -5)
(define number (+ (random (- (+ maximum 1) minimum)) minimum))

(display "Pick a number from ")
(display minimum)
(display " through ")
(display maximum)
(display ".\n> ")
(if (or (>= guess maximum) (< guess minimum))
(display "Out of range!\n> ")
(begin
(if (> guess number)
(display "Too high!\n> "))
(if (< guess number)
(display "Too low!\n> ")))))
(display "Correct!\n")
```

## Seed7

```\$ include "seed7_05.s7i";

const integer: lower_limit is 0;
const integer: upper_limit is 100;

const proc: main is func
local
var integer: number is 0;
var integer: guess is 0;
begin
number := rand(lower_limit, upper_limit);
write("Guess the number between " <& lower_limit <& " and " <& upper_limit <& ": ");
while succeeds(readln(guess)) and number <> guess do
if number < guess then
writeln("high.");
else
writeln("low.");
end if;
write("Try again: ");
end while;
if number = guess then
writeln("You guessed correctly!");
else
writeln("You gave up!");
end if;
end func;```

## Sidef

Translation of: Ruby
```var number = rand(1..10);
say "Guess the number between 1 and 10";

loop {
given(var n = Sys.scanln("> ").to_i) {
when (number)     { say "You guessed it."; break }
case (n < number) { say "Too low" }
default           { say "Too high" }
}
}
```

## Small Basic

```number=Math.GetRandomNumber(10)
TextWindow.WriteLine("I just thought of a number between 1 and 10. What is it?")
While guess<>number
If guess>number Then
TextWindow.WriteLine("Lower number!")
EndIf
If guess<number Then
TextWindow.WriteLine("Higher number!")
EndIf
EndWhile
TextWindow.WriteLine("You win!")```

## Smalltalk

Works with: Pharo version 7.0.3

To load: Save the example. In a Pharo image, go to "Tools" -> "File Browser" -> Select the file... -> Filein. To play: run "GuessingGame playFrom: 1 to: 100" on a Workspace (Tools -> Workspace)

```'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
instanceVariableNames: 'min max uiManager tries'
classVariableNames: ''
poolDictionaries: ''
category: 'GuessingGame'!

!GuessingGame methodsFor: 'initialization' stamp: 'EduardoPadoan 10/26/2019 23:51'!
initialize
uiManager := UIManager default.
tries := 0! !

!GuessingGame methodsFor: 'services' stamp: 'EduardoPadoan 10/26/2019 23:40'!

!GuessingGame methodsFor: 'accessing' stamp: 'EduardoPadoan 10/26/2019 23:36'!
max
^ max! !

!GuessingGame methodsFor: 'accessing' stamp: 'EduardoPadoan 10/26/2019 23:36'!
min
^ min! !

!GuessingGame methodsFor: 'accessing' stamp: 'EduardoPadoan 10/26/2019 23:36'!
min: anObject
min := anObject! !

!GuessingGame methodsFor: 'accessing' stamp: 'EduardoPadoan 10/26/2019 23:36'!
max: anObject
max := anObject! !

!GuessingGame methodsFor: 'playing-main' stamp: 'EduardoPadoan 10/27/2019 00:18'!
play
| toGuess |
toGuess := self selectNumber.
[ :break |
| choice |
[
choice := self getGuessOrExitWith: break.
choice
ifNil: [ self alert: 'Invalid Input' ]
ifNotNil: [
self incrementTries.
choice = toGuess
ifTrue: [ self congratulateForGuess: choice andExitWith: break ]
ifFalse: [ choice > toGuess ifTrue: [ self alert: 'Too high' ]
ifFalse: [ self alert: 'Too low' ] ]
]
] repeat.
] valueWithExit.! !

!GuessingGame methodsFor: 'as yet unclassified' stamp: 'EduardoPadoan 10/26/2019 23:39'!
makeRequest: aString
^ uiManager request: aString! !

!GuessingGame methodsFor: 'as yet unclassified' stamp: 'EduardoPadoan 10/26/2019 23:48'!
getGuessOrExitWith: anExitBlock
^ [(self makeRequest: 'Guess number a between , min,' and ', max, '.') asInteger]
on: MessageNotUnderstood "nil"
do: [
self sayGoodbye.
anExitBlock value ].! !

!GuessingGame methodsFor: 'as yet unclassified' stamp: 'EduardoPadoan 10/26/2019 23:51'!
incrementTries
tries := tries + 1! !

!GuessingGame methodsFor: 'as yet unclassified' stamp: 'EduardoPadoan 10/27/2019 00:05'!
sayGoodbye

!GuessingGame methodsFor: 'as yet unclassified' stamp: 'EduardoPadoan 10/27/2019 00:15'!
congratulateForGuess: anInteger andExitWith: anExitBlock
self alert: 'Correct!! The value was indeed ', anInteger asString, '. Took you only ', tries asString, ' tries.'.
^ anExitBlock value! !

!GuessingGame methodsFor: 'as yet unclassified' stamp: 'EduardoPadoan 10/26/2019 23:35'!
selectNumber
^ (min to: max) atRandom ! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

GuessingGame class
slots: {  }!

!GuessingGame class methodsFor: 'creating' stamp: 'EduardoPadoan 10/27/2019 00:15'!
playFrom: aMinNumber to: aMaxNumber
self new
min: aMinNumber;
max: aMaxNumber;
play! !
```

## Sparkling

```printf("Lower bound: ");
let lowerBound = toint(getline());

printf("Upper bound: ");
let upperBound = toint(getline());

assert(upperBound > lowerBound, "upper bound must be greater than lower bound");

seed(time());
let n = floor(random() * (upperBound - lowerBound) + lowerBound);
var guess;

print();

while true {
guess = toint(getline());

if guess < n {
print("too low");
} else if guess > n {
print("too high");
} else {
print("You guessed it!");
break;
}
}```

## Swift

```import Cocoa

var found = false

let randomNum = Int(arc4random_uniform(100) + 1)

println("Guess a number between 1 and 100\n")

while (!found) {
var fh = NSFileHandle.fileHandleWithStandardInput()

println("Enter a number: ")
let data = fh.availableData
let str = NSString(data: data, encoding: NSUTF8StringEncoding)
if (str?.integerValue == randomNum) {
found = true
println("Well guessed!")
} else if (str?.integerValue < randomNum) {
println("Good try but the number is more than that!")
} else if (str?.integerValue > randomNum) {
println("Good try but the number is less than that!")
}
}
```

## Tcl

```set from 1
set to 10
set target [expr {int(rand()*(\$to-\$from+1) + \$from)}]
puts "I have thought of a number from \$from to \$to."
puts "Try to guess it!"
while 1 {
puts -nonewline "Enter your guess: "
flush stdout
gets stdin guess
if {![string is int -strict \$guess] || \$guess < \$from || \$guess > \$to} {
puts "Your guess should be an integer from \$from to \$to (inclusive)."
} elseif {\$guess > \$target} {
puts "Your guess was too high. Try again!"
} elseif {\$guess < \$target} {
puts "Your guess was too low. Try again!"
} else {
puts "Well done! You guessed it."
break
}
}
```

Sample output:

```I have thought of a number from 1 to 10.
Try to guess it!

Your guess was too low. Try again!
Your guess should be an integer from 1 to 10 (inclusive).
Your guess was too high. Try again!
Your guess was too low. Try again!
Your guess was too high. Try again!
Well done! You guessed it.
```

## TUSCRIPT

```\$\$ MODE TUSCRIPT
PRINT "Find the luckynumber (7 tries)!"
SET luckynumber=RANDOM NUMBERS (1,100,1)
LOOP round=1,7
SET message=CONCAT ("[",round,"] Please insert a number")
IF (n!='digits') THEN
PRINT "wrong insert: ",n," Please insert a digit"
ELSEIF (n>100.or.n<1) THEN
PRINT "wrong insert: ",n," Please insert a number between 1-100"
ELSEIF (n==#luckynumber) THEN
PRINT "BINGO"
EXIT
ELSEIF (n.gt.#luckynumber) THEN
PRINT "too big"
ELSEIF (n.lt.#luckynumber) THEN
PRINT "too small"
ENDIF
IF (round==7) PRINT/ERROR "You've lost: luckynumber was: ",luckynumber
ENDLOOP```

Output:

```Find the luckynumber (7 tries)!
[1] Please insert a number >51
too small
[2] Please insert a number >76
too small
[3] Please insert a number >89
too big
[4] Please insert a number >80
too small
[5] Please insert a number >84
too small
[6] Please insert a number >86
too small
[7] Please insert a number >88
too big
@@@@@@@@  You've lost: luckynumber was: 87
```

## UNIX Shell

Works with: Bourne Again SHell
Works with: Z SHell
```function guess {
[[ -n \$BASH_VERSION ]] && shopt -s extglob
[[ -n \$ZSH_VERSION ]] && set -o KSH_GLOB
local -i max=\${1:-100}
local -i number=RANDOM%max+1
local -i guesses=0

local guess
while true; do
echo -n "Guess my number! (range 1 - \$max): "
if [[ "\$guess" != +([0-9]) ]] || (( guess < 1 || guess > max )); then
echo "Guess must be a number between 1 and \$max."
continue
fi
let guesses+=1
if (( guess < number )); then
echo "Too low!"
elif (( guess == number )); then
echo "You got it in \$guesses guesses!"
break
else
echo "Too high!"
fi
done
}
```

Sample run:

```\$ guess
Guess my number! (range 1 - 100): 50
Too low!
Guess my number! (range 1 - 100): 75
Too high!
Guess my number! (range 1 - 100): 62
Too low!
Guess my number! (range 1 - 100): 69
Too low!
Guess my number! (range 1 - 100): 72
You got it in 5 guesses!
```

## Ursa

Translation of: Python
```decl int high low
set low 0
set high 100

out "Guess a number between " low " and " high "." endl endl console
decl ursa.util.random random
set target (int (+ 1 (+ low (random.getint (int (- high low))))))
inc i
out "Your guess(" i "): " console

out "  Out of range!" endl console
continue
end if
out "  Ye-Haw!!" endl console
continue
end if
out "  Too low." endl console
end if
out "  Too high." endl console
end if
end while

out endl "Thanks for playing." endl console```

## Vala

```void main(){
const int from = 1;
const int to = 100;

int random = Random.int_range(from, to);
int guess = 0;

while (guess != random){
stdout.printf("Guess the target number that's between %d and %d.\n", from, to);

num.canon("0123456789", '!'); // replaces any character in num that's not in "0123456789" with "!"

if ("!" in num)

else{
guess = int.parse(num);

if (guess > random && guess <= to)
stdout.printf("Too high!\n");
if (guess < random && guess >= from)
stdout.printf("Too low!\n");
if (guess == random)
stdout.printf("You guess it! You win!\n");
if (guess < from || guess > to)
stdout.printf("%d Your guess isn't even in the right range!\n", guess);
}

}//while
} // main
```

Shorter but no error checking

```int main() {
int guess, x = Random.int_range(1, 10);
stdout.printf("Make a guess (1-10): ");
while((guess = int.parse(stdin.read_line())) != x) {
stdout.printf("%s! Try again: ", x < guess ? "Lower" : "Higher");
}
stdout.printf("Got it!\n");
return 0;
}
```

## VBA Excel

The Application.InputBox display a message when input is inappropriate.

```Sub GuessTheNumberWithFeedback()
Dim Nbc&, Nbp&, m&, n&, c&

Randomize Timer
m = 11
n = 100
Nbc = Int((Rnd * (n - m + 1)) + m)
Do
c = c + 1
Nbp = Application.InputBox("Choose a number between " & m & " and " & n & " : ", "Enter your guess", Type:=1)
Select Case Nbp
Case Is > Nbc: MsgBox "Higher than target!"
Case Is < Nbc: MsgBox "Less than target!"
Case Else: Exit Do
End Select
Loop
MsgBox "Well guessed!" & vbCrLf & "You find : " & Nbc & " in " & c & " guesses!"
End Sub
```

## VBScript

```Dim max,min,secretnum,numtries,usernum
max=100
min=1
numtries=0
Randomize
secretnum = Int((max-min+1)*Rnd+min)

Do While usernum <> secretnum
usernum = Inputbox("Guess the secret number beween 1-100","Guessing Game")
If IsEmpty(usernum) Then
WScript.Quit
End If
If IsNumeric(usernum) Then
numtries = numtries + 1
usernum = Cint(usernum)
If usernum < secretnum Then
Msgbox("The secret number is higher than " + CStr(usernum))
ElseIf usernum > secretnum Then
Msgbox("The secret number is lower than " + CStr(usernum))
Else
Msgbox("Congratulations, you found the secret number in " + CStr(numtries) + " guesses!")
End If
Else
End If
Loop
```

## V (Vlang)

Translation of: Go
```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
}
}
}```

## VTL-2

```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```
Output:
```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```

## Wren

```import "io" for Stdin, Stdout
import "random" for Random

var rand = Random.new()
var n = rand.int(1, 21) // computer number from 1..20 inclusive, say
System.print("The computer has chosen a number between 1 and 20 inclusive.")
while (true) {
System.write("  Your guess 1-20 : ")
Stdout.flush()
if (!g || g.type != Num || !g.isInteger || g < 1 || g > 20) {
System.print("  Inappropriate")
} else if (g > n) {
System.print("  Too high")
} else if (g < n) {
System.print("  Too low")
} else {
System.print("  Spot on!")
break
}
}
```
Output:

Sample session:

```The computer has chosen a number between 1 and 20 inclusive.
Inappropriate
Inappropriate
Inappropriate
Too low
Too high
Too high
Too high
Spot on!
```

## XLISP

```(defun guessing-game (a b)
; minimum and maximum, to be supplied by the user
(defun prompt ()
(display "What is your guess? ")
(if (eq guess n) ; EQ, unlike =, won't blow up
; if GUESS isn't a number
(display "Well guessed!")
(begin
(display
(cond
((not (integer? guess)) "Come on, that isn't even an integer")
((or (< guess a) (> guess b)) "Now you   k n o w   it won't be that")
((< guess n) "Too low")
((> guess n) "Too high")))
(display "! Try again...")
(newline)
(prompt))))
(define n (+ (random (- (+ b 1) a)) a))
(display "I have thought of an integer between ")
(display a)
(display " and ")
(display b)
(display ". Try to guess it!")
(newline)
(prompt))
```
Output:
```[1] (guessing-game 19 36)
I have thought of an integer between 19 and 36. Try to guess it!
Now you   k n o w   it won't be that! Try again...
Too high! Try again...
Come on, that isn't even an integer! Try again...
Come on, that isn't even an integer! Try again...
Too low! Try again...
Too high! Try again...
Too high! Try again...
Well guessed!```

## XPL0

```include c:\cxpl\codes;
int Lo, Hi, C, Guess, Number;

[loop  [Text(0, "Low  limit: ");  Lo:= IntIn(0);
Text(0, "High limit: ");  Hi:= IntIn(0);
if Lo < Hi then quit;
Text(0, "Low limit must be lower!^M^J^G");
];
Number:= Ran(Hi-Lo+1)+Lo;
Text(0, "I'm thinking of a number between ");
IntOut(0, Lo);  Text(0, " and ");  IntOut(0, Hi); Text(0, ".^M^J");
repeat  Text(0, "Can you guess the number? ");
loop    [C:= ChIn(0);
if C>=^0 & C<=^9 then quit;
Text(0, "Please enter a number in the given range.^M^J");
OpenI(0);
];
Backup;  Guess:= IntIn(0);
Text(0, if Guess = Number then "Correct!"
else if Guess > Number then "Nope, too high."
else "You're too low.");
CrLf(0);
until   Guess = Number;
]```

Example output:

```Low  limit: 1
High limit: 10
I'm thinking of a number between 1 and 10.
Can you guess the number? maybe
Please enter a number in the given range.
3
You're too low.
Can you guess the number? 6
You're too low.
Can you guess the number? 8
Correct!
```

## zkl

```r:=(0).random(10)+1;
while(1){
n:=ask("Num between 1 & 10: ");
if(n==r){ println("Well guessed!"); break; }
println((n<r) and "small" or "big");
}```
Output:
```Num between 1 & 10: foo
Num between 1 & 10: 5
big
Num between 1 & 10: 3
big
Num between 1 & 10: 2
big
Num between 1 & 10: 1
Well guessed!
```

## Zoomscript

For typing:

```var randnum
var guess
randnum & random 1 10
guess = 0
while ne randnum guess
print "I'm thinking of a number between 1 and 10. What is it? "
guess & get
if lt guess randnum
print "Too low. Try again!"
println
endif
if gt guess randnum
print "Too high. Try again!"
println
endif
endwhile
print "Correct number. You win!"```

For importing:

¶0¶var randnum¶0¶var guess¶0¶randnum & random 1 10¶0¶guess = 0¶0¶while ne randnum guess¶0¶print "I'm thinking of a number between 1 and 10. What is it? "¶0¶guess & get¶0¶if lt guess randnum¶0¶print "Too low. Try again!"¶0¶println¶0¶endif¶0¶if gt guess randnum¶0¶print "Too high. Try again!"¶0¶println¶0¶endif¶0¶endwhile¶0¶print "Correct number. You win!"

## ZX Spectrum Basic

```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
```