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

- The computer will choose a number between given set limits and asks the player for repeated guesses until the player guesses the target number correctly. At each guess, the computer responds with whether the guess was higher than, equal to, or less than the target - or signals that the input was inappropriate.

C.f: Guess the number/With Feedback (Player)

## [edit] Ada

with Ada.Numerics.Discrete_Random;

with Ada.Text_IO;

procedure Guess_Number_Feedback is

procedure Guess_Number (Lower_Limit : Integer; Upper_Limit : Integer) is

subtype Number is Integer range Lower_Limit .. Upper_Limit;

package Number_IO is new Ada.Text_IO.Integer_IO (Number);

package Number_RNG is new Ada.Numerics.Discrete_Random (Number);

Generator : Number_RNG.Generator;

My_Number : Number;

Your_Guess : Number;

begin

Number_RNG.Reset (Generator);

My_Number := Number_RNG.Random (Generator);

Ada.Text_IO.Put_Line ("Guess my number!");

loop

Ada.Text_IO.Put ("Your guess: ");

Number_IO.Get (Your_Guess);

exit when Your_Guess = My_Number;

if Your_Guess > My_Number then

Ada.Text_IO.Put_Line ("Wrong, too high!");

else

Ada.Text_IO.Put_Line ("Wrong, too low!");

end if;

end loop;

Ada.Text_IO.Put_Line ("Well guessed!");

end Guess_Number;

package Int_IO is new Ada.Text_IO.Integer_IO (Integer);

Lower_Limit : Integer;

Upper_Limit : Integer;

begin

loop

Ada.Text_IO.Put ("Lower Limit: ");

Int_IO.Get (Lower_Limit);

Ada.Text_IO.Put ("Upper Limit: ");

Int_IO.Get (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;

## [edit] ALGOL 68

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

## [edit] 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.

## [edit] 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) {

print("Incorrect, your answer was too low. Try again.")

continue

}

if (n < ans) {

print("Incorrect, your answer was too high. Try again.")

continue

}

}

exit(0)

}

## [edit] BASIC

### [edit] 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."

## [edit] 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 Enter your number 10 Sorry, your number was too high Enter your number 7 Sorry, your number was too low Enter your number 8 Well guessed!

## [edit] 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

## [edit] 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

## [edit] Brat

number = random 10

p "Guess a number between 1 and 10."

until {

guess = ask("Guess: ").to_i

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!" }

}

}

}

## [edit] 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 );

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 Your guess was too low. Try again: 75 Your guess was too high. Try again: 63 Your guess was too high. Try again: 57 Your guess was too low. Try again: 60 Your guess was too high. Try again: 58 Your guess was too low. Try again: 59 You guessed correctly!

## [edit] 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 Your guess is too high Guess what number I have: 40 Your guess is too high Guess what number I have: 25 Your guess is too high Guess what number I have: 10 Your guess is too high Guess what number I have: 2 Your guess is too low Guess what number I have: 4 Your guess is too high Guess what number I have: 3 You got it!

## [edit] 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 (int.TryParse(Console.ReadLine(), out guessedNumber))

{

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.");

Console.ReadKey();

}

}

Output:

The number is between 1 and 10. Make a guess: 1 Your guess was too low. Make a guess: 9 Your guess was too high. Make a guess: 5 Your guess was too low. Make a guess: 6 Your guess was too low. Make a guess: hello, world Input was not an integer. Make a guess: 7 You guessed the right number! Press any key to exit.

## [edit] 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]

(printf "Your guess %d:\n" i)

(let [ans (read)]

(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)))))))

## [edit] 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

.

## [edit] 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)

(setf guess (read))

(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 Your guess is too small. Guess? 768 Your guess is too small. Guess? 896 Your guess is too large. Guess? 832 Your guess is too small. Guess? 864 Your guess is too large. Guess? 848 Your guess is too large. Guess? 840 Your guess is too large. Guess? 836 Your guess is correct! You got the number correct on the eighth guess!

## [edit] D

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);

immutable txt = stdin.readln.strip;

Nullable!int answer;

try {

answer = txt.to!int;

} catch (ConvException e) {

writefln(" I don't understand your input '%s'", txt);

continue;

}

if (answer < interval[0] || answer > interval[1]) {

writeln(" Out of range!");

continue;

}

if (answer == target) {

writeln(" Well guessed.");

break;

}

writeln(answer < target ? " Too low." : " Too high.");

}

}

Sample game:

Guess my target number that is between 1 and 100 (inclusive). Your guess #1: 100 Too high. Your guess #2: 5 Too low. Your guess #3: 522 Out of range! Your guess #4: 50 Too low. Your guess #5: 75 Too low. Your guess #6: 80 Too low. Your guess #7: 90 Too high. Your guess #8: 85 Too high. Your guess #9: 83 Well Guessed.

## [edit] 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

Readln(s);

// 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');

Readln(s);

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.

## [edit] Ela

open random core console string datetime read

rnd' v = rnd s 1 v where s = milliseconds <| datetime.now()

start () =

match bound() with

Some v = writen ("Guess a number from 1 to " ++ show v) `seq` (guess <| rnd' v)

None = start()

bound () =

writen "What's the upper bound?" `seq` (bound' <| readStr <| readn())

where bound' v | v <= 0 = writen "Bound should be greater than 0." `seq` None

| else = Some v

success v =

writen "Correct! Do you want to continue? (Y/N)" `seq` ask()

where ask () = read <| upper <| readn()

read "Y" = start()

read "N" = writen "Bye!"

read x = writen "Say what?" `seq` ask()

failed v n =

writen ("No, this is not " ++ show v ++ ". " ++ hint) `seq` guess n

where hint | v < n = "Try bigger."

| else = "Try smaller."

guess n = g <| readStr <| readn()

where g v | v == n = success v

| else = failed v n

start()

## [edit] 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 ->

io:format("Your guess is too high.\n");

true ->

io:format("Your guess is too low.\n")

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 Your guess is too low. Guess the number : 70 Your guess is too low. Guess the number : 85 Your guess is too high. Guess the number : 75 Your guess is too high. Guess the number : 73 Well guessed!! ok

## [edit] 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

## [edit] Factor

USING:

formatting

fry

io

kernel

math math.parser math.ranges

prettyprint

random ;

IN: guessnumber

: game-step ( target -- ? )

readln string>number

[

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 ;

## [edit] 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

guess = Env.cur.in.readLine.trim.toInt

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!

## [edit] Fortran

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: "

read*, guess

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

## [edit] 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

}

}

}

## [edit] Haskell

import Control.Monad

import System.Random

-- Repeat the action until the predicate is true.

until_ act pred = act >>= pred >>= flip unless (until_ act pred)

answerIs ans guess =

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

-- we use "reads" we allow garbage to follow it, though.)

ask = do line <- getLine

case reads line of

((num,_):_) -> return num

otherwise -> putStrLn "Please enter a number." >> ask

main = do

ans <- randomRIO (1,100) :: IO Int

putStrLn "Try to guess my secret number between 1 and 100."

ask `until_` answerIs ans

## [edit] Icon and Unicon

procedure main()

smallest := 5

highest := 25

n := smallest-1 + ?(1+highest-smallest)

repeat {

writes("Pick a number from ", smallest, " through ", highest, ": ")

guess := read ()

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 Your guess is too low Pick a number from 5 through 25: 16 Your guess is too low Pick a number from 5 through 25: 20 Your guess is too low Pick a number from 5 through 25: 23 Your guess is too high Pick a number from 5 through 25: 22 Well guessed!

## [edit] 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.

## [edit] 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)

System.out.println("Your guess is too high!");

else if (guessedNumber < randomNumber)

System.out.println("Your guess is too low!");

else

System.out.println("You got it!");

} while (guessedNumber != randomNumber);

}

}

Demonstration:

The number is between 1 and 100. Guess what the number is: 50 Your guess is too high! Guess what the number is: 25 Your guess is too high! Guess what the number is: 17 Your guess is too high! Guess what the number is: 10 Your guess is too high! Guess what the number is: 5 Your guess is too low! Guess what the number is: 7 You got it!

## [edit] 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>`

### [edit] 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++;

putstr(" Your guess: ");

var n = parseInt(readline());

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! Your guess: 50 Too low. Your guess: 75 Too high. Your guess: 62 Too high. Your guess: 56 Too low. Your guess: 58 Too high. Your guess: 57 You got it in 6 tries.

## [edit] Julia

function guess_feedback(n)

number = rand(1:n)

print("I choose a number between 1 and $n\nYour guess? ")

while((guess = chomp(readline(STDIN))) != string(number))

isdigit(guess) ?

print("Too $(int(guess) < number ? "small" : "big")\nNew guess? ") :

print("Enter an integer please\nNew guess? ")

end

print("you guessed right!")

end

- Output:

julia> guess_feedback(10) I choose a number between 1 and 10 Your guess? 5 Too small New guess? 8 Too big New guess? no luck Enter an integer please New guess? 6 you guessed right!

## [edit] 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!

## [edit] LFE

(defmodule guessing-game

(export (main 0)))

(defun get-player-guess ()

(let (((tuple 'ok (list guessed)) (: io fread '"Guess number: " '"~d")))

guessed))

(defun check-guess (answer guessed)

(cond

((== answer guessed)

(: io format '"Well-guessed!!~n"))

((/= answer guessed)

(if (> answer guessed) (: io format '"Your guess is too low.~n"))

(if (< answer guessed) (: io format '"Your guess is too high.~n"))

(check-guess answer (get-player-guess)))))

(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

Your guess is too high.

Guess number: 1

Your guess is too low.

Guess number: 5

Your guess is too low.

Guess number: 7

Well-guessed!!

ok

>

## [edit] 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

## [edit] 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:

## [edit] Logo

to guess [:max 100]Sample run:

local "number

make "number random :max

local "guesses

make "guesses 0

local "guess

forever [

(type [Guess my number! \(range 1 -\ ] :max "\):\ )

make "guess first readlist

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

? 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!

## [edit] 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:"

your_number = io.stdin:read'*l'

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.

## [edit] Mathematica

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]

## [edit] MATLAB / Octave

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

## [edit] Mirah

def getInput:int

s = System.console.readLine()

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

puts "Please enter an integer."

end

end

## [edit] 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;

InOut.ReadString (input);

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;

InOut.WriteString ("Your guess is ");

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. Enter your guess : 500 Your guess is too low. Enter your guess : 750 Your guess is too high. Enter your guess : 625 Your guess is too high. Enter your guess : kwak kwak is not a valid number... Enter your guess : 531 Your guess is spot on! Thank you for playing; have a nice day!

## [edit] Nemerle

using System;

using System.Console;

module GuessHints

{

Main() : void

{

def rand = Random();

def secret = rand.Next(1, 101);

mutable guess = 0;

def GetGuess() : int {Int32.Parse(ReadLine())}

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)

}

}

## [edit] 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

parse ask guess .

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

## [edit] 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 ".")

(while (not found)

(print " What's your guess? ")

(setq guess (int (read-line) 'bad))

(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!

## [edit] Nimrod

import math, rdstdin, strutils

randomize()

let iRange = 1..100

echo "Guess my target number that is between ", iRange.a, " and ", iRange.b, " (inclusive)."

let target = random(iRange)

var answer, i = 0

while answer != target:

inc i

let txt = readLineFromStdin("Your guess " & $i & ": ")

try: answer = parseInt(txt)

except EInvalidValue:

echo " I don't understand your input of '", txt, "'"

continue

if answer < iRange.a or answer > iRange.b: echo " Out of range!"

elif answer < target: echo " Too low."

elif answer > target: echo " Too high."

else: echo " Ye-Haw!!"

echo "Thanks for playing."

Output:

Guess my target number that is between 1 and 100 (inclusive). Your guess 1: 50 Too high. Your guess 2: 300 Out of range! Your guess 3: -10 Out of range! Your guess 4: foo I don't understand your input of 'foo' Your guess 5: 35 Too low. Your guess 6: 42 Too high. Your guess 7: 38 Too low. Your guess 8: 40 Too low. Your guess 9: 41 Ye-Haw!! Thanks for playing.

## [edit] 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) {

guess := Console->ReadString();

number := guess->ToInt();

if(number <> 0) {

if(number <> rand_num) {

Console->Print("Your guess was too ")

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

};

};

};

}

}

}

## [edit] OCaml

let rec _read_int() =

try read_int()

with _ ->

print_endline "Please give a cardinal numbers.";

(* TODO: what is the correct word? cipher, digit, figure or numeral? *)

_read_int() ;;

let () =

print_endline "Please give a set limits (two integers):";

let a = _read_int()

and b = _read_int() in

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;

print_endline "Please guess it!";

let rec loop () =

let guess = _read_int() in

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 Please guess it! 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 Please guess it! three Please give a cardinal numbers.

## [edit] 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!')

## [edit] 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")

};

## [edit] 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 simple comparison instead of strict 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

## [edit] Pascal

See Delphi

## [edit] 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";

## [edit] 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 {

given prompt("Your guess: ") {

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! Your guess: 5 Sorry, my number is bigger. Your guess: 7 Sorry, my number is smaller. Your guess: 6 Great you guessed right after 3 attempts!

## [edit] 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">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Guess A Number</title>

</head>

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

## [edit] PicoLisp

(de guessTheNumber ()

(use (Low High Guess)

(until

(and

(prin "Enter low limit : ")

(setq Low (read))

(prin "Enter high limit: ")

(setq High (read))

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

"Your guess is too "

(if (> Number Guess) "low" "high")

"." ) ) ) ) )

Output:

: (guessTheNumber) Enter low limit : 1 Enter high limit: 64 Guess what number I have: 32 Your guess is too high. Guess what number I have: 16 Your guess is too low. Guess what number I have: 24 You got it!

## [edit] 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

PrintN("Your guess is to low.")

Else

PrintN("Your guess is to high.")

EndIf

ForEver

## [edit] Prolog

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 "

ask_for_guess(Guess),

give_feedback(N, Guess),

Guess == N.

/* IO Stuff */

tell_range(Low, High) :-

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

ask_for_guess(Guess) :-

format('Guess the number: '),

read(Guess).

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.

Your guess is too low.

## [edit] 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)

answer, i = None, 0

while answer != target:

i += 1

txt = input("Your guess(%i): " % i)

try:

answer = int(txt)

except ValueError:

print(" I don't understand your input of '%s' ?" % txt)

continue

if answer < inclusive_range[0] or answer > inclusive_range[1]:

print(" Out of range!")

continue

if answer == target:

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). Your guess(1): 50 Too high. Your guess(2): 25 Too low. Your guess(3): 40 Too high. Your guess(4): 30 Too low. Your guess(5): 35 Too high. Your guess(6): 33 Too high. Your guess(7): 32 Too high. Your guess(8): 31 Ye-Haw!! Thanks for playing.

**Sample trapped Errors**

Guess my target number that is between 1 and 100 (inclusive). Your guess(1): 0 Out of range! Your guess(2): 101 Out of range! Your guess(3): Howdy I don't understand your input of 'Howdy' ? Your guess(4):

## [edit] Racket

#lang racket

(define min 1)

(define max 10)

(define (guess-number (number (+ min (random max))))

(define guess (read))

(cond ((not (number? guess)) (display "That's not a number!\n" (guess-number number)))

((or (> guess max) (> min guess)) (display "Out of range!\n") (guess-number number))

((> guess number) (display "Too high!\n") (guess-number number))

((< guess number) (display "Too low!\n") (guess-number number))

(else (display "Well guessed!\n"))))

(display (format "Guess a number between ~a and ~a\n" min max))

(guess-number)

## [edit] 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 ;

## [edit] REXX

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

/*REXX program that plays the guessing (the number) game. */

low=1 /*lower range for guessing game. */

high=100 /*upper range for guessing game. */

try=0 /*number of valid attempts. */

r=random(1,100) /*get a random # (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'

er!='*** error! ***'

prompt=centre("guess the number, it's between" low 'and',

high '(inclusive) ───or─── Quit:',79,"─")

do ask=0; say; say prompt; say; pull g; g=space(g); say

do validate=0

select

when g=='' then iterate ask

when abbrev('QUIT',g,1) then exit

when words(g)\==1 then say er! 'too many numbers entered:' g

when \datatype(g,'N') then say er! g "isn't numeric"

when \datatype(g,'W') then say er! g "isn't a whole number"

when g<low then say er! g 'is below the lower limit of' low

when g>high then say er! g 'is above the higher limit of' high

otherwise leave validate

end /*select*/

iterate ask

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'.',,"_")

end /*ask*/

if try==1 then say 'Gadzooks!!! You guessed the number right away!'

else say 'Congratulations!, you guessed the number in' try "tries."

/*stick a fork in it, we're done.*/

## [edit] Ruby

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

puts "Please enter an integer."

end

end

## [edit] Rust

use std::io::IoResult;

use std::io::stdio::stdin;

use std::rand::{Rng, task_rng};

static LOWEST: int = 1;

static HIGHEST: int = 100;

fn main() {

let mut rng = task_rng();

let mut stdin = stdin();

loop {

let number: int = rng.gen_range(LOWEST, HIGHEST + 1);

let mut num_guesses = 0i;

println!("I have chosen my number between {:d} and {:d}. You know what to do", LOWEST, HIGHEST);

loop {

num_guesses += 1;

let line: IoResult<String> = stdin.read_line();

let val: Option<int> = line.ok().map_or(None, |line| from_str::<int>(line.as_slice().trim()));

match val {

None => println!("numbers only, please"),

Some(n) if n == number => {

println!("you got it in {:d} tries!", num_guesses);

break;

}

Some(n) if n < number => println!("too low!"),

Some(n) if n > number => println!("too high!"),

Some(_) => println!("something went wrong")

}

}

}

}

I have chosen my number between 0 and 100. You know what to do 50 too high! 25 too high! 12 too low! 18 too low! 21 too low! 23 you got it in 6 tries!

## [edit] Scheme

(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> ")

(do ((guess (read) (read))) ((eq? guess number))

(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")

## [edit] 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 <& ": ");

readln(guess);

while IN.io_ok and number <> guess do

write("Your guess was too ");

if number < guess then

writeln("high.");

else

writeln("low.");

end if;

write("Try again: ");

readln(guess);

end while;

if number = guess then

writeln("You guessed correctly!");

else

writeln("You gave up!");

end if;

end func;

## [edit] 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! Enter your guess: 2 Your guess was too low. Try again! Enter your guess: skfg Your guess should be an integer from 1 to 10 (inclusive). Enter your guess: 9 Your guess was too high. Try again! Enter your guess: 5 Your guess was too low. Try again! Enter your guess: 7 Your guess was too high. Try again! Enter your guess: 6 Well done! You guessed it.

## [edit] 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")

ASK $message: n=""

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

## [edit] UNIX 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): "

read guess

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!

## [edit] 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);

string? num = stdin.read_line ();

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

if ("!" in num)

stdout.printf("Please enter a number!\n");

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;

}

## [edit] 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!

## [edit] zkl

r:=(0).random(10)+1;

while(1){

n:=ask("Num between 1 & 10: ");

try{n=n.toInt()}catch{ println("Number please"); continue; }

if(n==r){ println("Well guessed!"); break; }

println((n<r) and "small" or "big");

}

- Output:

Num between 1 & 10: foo Number please 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!

- Programming Tasks
- Games
- Ada
- ALGOL 68
- AutoHotkey
- AWK
- BASIC
- Applesoft BASIC
- BASIC256
- Batch File
- BBC BASIC
- Brat
- C
- C++
- C sharp
- Clojure
- COBOL
- Common Lisp
- D
- Delphi
- Ela
- Erlang
- Euphoria
- Factor
- Fantom
- Fortran
- Go
- Haskell
- Icon
- Unicon
- J
- Java
- JavaScript
- Julia
- Lasso
- LFE
- Liberty BASIC
- Locomotive Basic
- Logo
- Lua
- Mathematica
- MATLAB
- Octave
- Octave examples needing attention
- Examples needing attention
- Mirah
- Modula-2
- Nemerle
- NetRexx
- NewLISP
- Nimrod
- Objeck
- OCaml
- PARI/GP
- OoRexx
- Pascal
- Perl
- Perl 6
- PHP
- PicoLisp
- PureBasic
- Prolog
- Python
- Racket
- Retro
- REXX
- Ruby
- Rust
- Scheme
- Seed7
- Tcl
- TUSCRIPT
- GUISS/Omit
- UNIX Shell
- Vala
- XPL0
- Zkl
- Lilypond/Omit