Guess the number/With feedback: Difference between revisions

m
syntax highlighting fixup automation
(Add VTL-2)
m (syntax highlighting fixup automation)
Line 19:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V (target_min, target_max) = (1, 100)
 
print("Guess my target number that is between #. and #. (inclusive).\n".format(target_min, target_max))
Line 41:
I answer > target {print(‘ Too high.’)}
 
print("\nThanks for playing.")</langsyntaxhighlight>
 
{{out}}
Line 76:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
BYTE x,n,min=[1],max=[100]
 
Line 94:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Guess_the_number_with_feedback.png Screenshot from Atari 8-bit computer]
Line 109:
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Discrete_Random;
with Ada.Text_IO;
procedure Guess_Number_Feedback is
Line 159:
end loop;
Guess_Number (Lower_Limit, Upper_Limit);
end Guess_Number_Feedback;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<langsyntaxhighlight lang="algol68"># simple guess-the-number game #
 
main:(
Line 209:
OD;
print( ( "That's correct!", newline ) )
)</langsyntaxhighlight>
{{out}}
<pre>
Line 235:
=={{header|AppleScript}}==
 
<langsyntaxhighlight AppleScriptlang="applescript">-- defining the range of the number to be guessed
property minLimit : 1
property maxLimit : 100
Line 273:
end try
end repeat
end run</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">n: random 1 10
while ø [
try? [
Line 288:
]
else -> print "\tInvalid input!"
]</langsyntaxhighlight>
 
{{out}}
Line 305:
=={{header|AutoHotkey}}==
 
<langsyntaxhighlight AutoHotkeylang="autohotkey">MinNum = 1
MaxNum = 99999999999
 
Line 335:
}
TotalTime := Round((A_TickCount - TotalTime) / 1000,1)
MsgBox, 64, Correct, The number %RandNum% was guessed in %Tries% tries, which took %TotalTime% seconds.</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f GUESS_THE_NUMBER_WITH_FEEDBACK.AWK
BEGIN {
Line 367:
exit(0)
}
</syntaxhighlight>
</lang>
 
=={{header|BASIC}}==
 
==={{header|Applesoft BASIC}}===
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic">100 L% = 1
110 U% = 10
120 N% = RND(1) * (U% - L% + 1) + L%
Line 385:
210 Q = G% = N%
220 NEXT
230 PRINT "THE GUESS WAS EQUAL TO THE TARGET."</langsyntaxhighlight>
 
==={{header|BASIC256}}===
{{works with|BASIC256 }}
<langsyntaxhighlight lang="basic256">
Min = 5: Max = 15
chosen = int(rand*(Max-Min+1)) + Min
Line 404:
end if
until guess = chosen
</syntaxhighlight>
</lang>
Output:(example)
<pre>
Line 417:
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Guess.bas"
110 RANDOMIZE
120 LET UP=10:LET LO=1 ! Limits
Line 433:
240 PRINT "Well guessed! Numner of tips:";COUNT
250 END SELECT
260 LOOP UNTIL NR=GU</langsyntaxhighlight>
 
==={{header|QB64}}===
Note that <code>INPUT</code> only allows the user to type things that can fit in the variable it's storing to. Since we're storing to a byte, we don't have to worry about the user typing any non-numbers.
<langsyntaxhighlight lang="qbasic">DIM secretNumber AS _BYTE ' the secret number
DIM guess AS _BYTE ' the player's guess
 
Line 456:
END SELECT
LOOP UNTIL guess%% = secretNumber%%
PRINT "You won!"; secretNumber%%; "was the secret number!"</langsyntaxhighlight>
{{out}}
<pre>
Line 477:
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">@echo off
 
:A
Line 493:
echo You won! The number was %number%
pause>nul
goto A</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> Min% = 1
Max% = 10
chosen% = RND(Max%-Min%+1) + Min% - 1
Line 514:
ENDCASE
UNTIL FALSE
END</langsyntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
static $( randstate = ? $)
 
Line 559:
wrch('*N')
play(min, max, rand(min, max))
$)</langsyntaxhighlight>
{{out}}
<pre>Guess the number
Line 587:
The number range is hardcoded at the start of the program (<tt>1:"d"</tt> being 1 to 100), but can easily be changed.
 
<langsyntaxhighlight lang="befunge">1:"d">>048*"neewteb rebmun a sseuG">:#,_$\:.\"d na",,\,,:.55+,\-88+0v
<*2\_$\1+%+48*">",,#v>#+:&#>#5-:!_0`00p0"hgih"00g>_0"wol"00g!>_48vv1?
\1-:^v"d correctly!"<^,,\,+55,". >"$_,#!>#:<"Your guess was too"*<>+>
:#,_@>"ess" >"eug uoY"></langsyntaxhighlight>
 
{{out}}
Line 604:
 
=={{header|Brat}}==
<langsyntaxhighlight lang="brat">number = random 10
 
p "Guess a number between 1 and 10."
Line 620:
}
}
}</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <time.h>
Line 647:
 
return 0;
}</langsyntaxhighlight>
 
Demonstration:
Line 666:
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
 
class Program
Line 705:
}
}
</syntaxhighlight>
</lang>
Output:
<pre>The number is between 1 and 10. Make a guess: 1
Line 724:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cstdlib>
#include <ctime>
Line 751:
 
return 0;
}</langsyntaxhighlight>
Output:
<pre>Enter lower limit: 1
Line 771:
 
=={{header|Caché ObjectScript}}==
<langsyntaxhighlight Cachélang="caché ObjectScriptobjectscript">GUESSNUM
; get a random number between 1 and 100
set target = ($random(100) + 1) ; $r(100) gives 0-99
Line 799:
write !!,"You guessed the number in "_tries_" attempts."
quit</langsyntaxhighlight>
 
{{out}}<pre>SAMPLES>do ^GUESSNUM^
Line 824:
In you module.ceylon file put import ceylon.random "1.3.1";
 
<langsyntaxhighlight lang="ceylon">import ceylon.random {
DefaultRandom
}
Line 860:
}
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn guess-run []
(let [start 1
end 100
Line 878:
:else true)
(println "Correct")
(recur (inc i)))))))</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">read_number = proc (prompt: string) returns (int)
po: stream := stream$primary_output()
pi: stream := stream$primary_input()
Line 936:
secret: int := min + random$next(max - min + 1)
play_game(min, max, secret)
end start_up</langsyntaxhighlight>
{{out}}
<pre>Guess the number
Line 959:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Guess-With-Feedback.
 
Line 988:
GOBACK
.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun guess-the-number-feedback (&optional (min 1) (max 100))
(let ((num-guesses 0)
(num (+ (random (1+ (- max min))) min))
Line 1,007:
(= guess num)))
(format t "You got the number correct on the ~:r guess!~%" num-guesses)))
</syntaxhighlight>
</lang>
Output:
<pre>CL-USER> (guess-the-number-feedback 1 1024)
Line 1,032:
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">number = rand(1..10)
 
puts "Guess the number between 1 and 10"
Line 1,050:
puts "Please enter an integer."
end
end</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.typecons, std.conv, std.string,
std.range;
 
Line 1,084:
writeln(answer < target ? " Too low." : " Too high.");
}
}</langsyntaxhighlight>
Sample game:
<pre>Guess my target number that is between 1 and 100 (inclusive).
Line 1,108:
 
=={{header|DCL}}==
<langsyntaxhighlight DCLlang="dcl">$ rnd = f$extract( 21, 2, f$time() )
$ count = 0
$ loop:
Line 1,122:
$ if guess .gt. rnd then $ write sys$output "too large"
$ if guess .ne. rnd then $ goto loop
$ write sys$output "it only took you ", count, " guesses"</langsyntaxhighlight>
{{out}}
<pre>$ @guess
Line 1,144:
=={{header|Delphi}}==
 
<langsyntaxhighlight Delphilang="delphi">program GuessTheNumber;
 
{$APPTYPE CONSOLE}
Line 1,229:
 
end.
</syntaxhighlight>
</lang>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">print "Guess a number between 1 and 100!"
n = random 100 + 1
repeat
Line 1,246:
until g = n
.
print " is correct"</langsyntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="lisp">
;;(read <default-value> <prompt>) prompts the user with a default value using the browser dialog box.
;; we play sounds to make this look like an arcade game
Line 1,267:
(play-sound 'ok )
" 🔮 Well played!! 🍒 🍇 🍓")
</syntaxhighlight>
</lang>
 
=={{header|Ela}}==
 
<langsyntaxhighlight lang="ela">open string datetime random core monad io
 
guess () = do
Line 1,306:
ask ()
 
guess () ::: IO</langsyntaxhighlight>
 
=={{header|Elena}}==
ELENA 4.x :
<langsyntaxhighlight lang="elena">import extensions;
public program()
Line 1,335:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,349:
=={{header|Elixir}}==
{{works with|Elixir|1.2}}
<langsyntaxhighlight lang="elixir">defmodule GuessingGame do
def play(lower, upper) do
play(lower, upper, Enum.random(lower .. upper))
Line 1,368:
end
GuessingGame.play(1, 100)</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(let* ((min 1)
(max 100)
(number (+ (random (1+ (- max min))) min))
Line 1,385:
((= guess number)
(setq done t)
(message "Well guessed!")))))</langsyntaxhighlight>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(guess_number).
-export([main/0]).
Line 1,415:
guess(N)
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>1> c(guess_number).
Line 1,435:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">include get.e
 
constant lower_limit = 0, upper_limit = 100
Line 1,453:
puts(1,"You guessed to low.\nTry again: ")
end if
end while</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
open System
 
Line 1,487:
0
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING:
formatting
fry
Line 1,515:
[ unparse "Number in range %s, your guess?\n" printf flush ]
[ random '[ _ game-step ] loop ]
bi ;</langsyntaxhighlight>
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 1,556:
}
}
</syntaxhighlight>
</lang>
 
Sample game:
Line 1,584:
 
=={{header|FOCAL}}==
<langsyntaxhighlight FOCALlang="focal">01.01 S T=0
01.02 A "LOWER LIMIT",L
01.03 A "UPPER LIMIT",H
Line 1,598:
01.16 T "TOO LOW!",!;G 1.1
01.17 T "CORRECT! GUESSES",%4,T,!;Q
01.18 T "TOO HIGH!",!;G 1.1</langsyntaxhighlight>
 
{{out}}
Line 1,621:
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">program Guess_a_number
implicit none
Line 1,647:
end if
end do
end program</langsyntaxhighlight>
Output
<pre>I have chosen a number bewteen 1 and 100 and you have to try to guess it.
Line 1,665:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Randomize
Line 1,686:
End If
Loop
End</langsyntaxhighlight>
 
Sample input/output
Line 1,702:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">// Guess a Number with feedback.
target = random[1,100] // Min and max are both inclusive for the random function
guess = 0
Line 1,722:
println["$guess is correct! Well guessed!"]
}
</syntaxhighlight>
</lang>
{{out}}
Including an example with a non-integer entered.
Line 1,735:
 
=={{header|Genie}}==
<langsyntaxhighlight lang="genie">[indent=4]
/*
Number guessing with feedback, in Genie
Line 1,785:
init
var game = new NumberGuessing(1, 100)
game.start()</langsyntaxhighlight>
 
{{out}}
Line 1,823:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,851:
}
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Based on the Java implementation
<langsyntaxhighlight lang="groovy">
def rand = new Random() // java.util.Random
def range = 1..100 // Range (inclusive)
Line 1,876:
}
}
</syntaxhighlight>
</lang>
Example:
<syntaxhighlight lang="text">
The number is in 1..100
Guess the number: ghfvkghj
Line 1,894:
Guess the number: 92
Your guess is spot on!
</syntaxhighlight>
</lang>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">
import Control.Monad
import System.Random
Line 1,921:
putStrLn "Try to guess my secret number between 1 and 100."
ask `until_` answerIs ans
</syntaxhighlight>
</lang>
 
=={{header|HolyC}}==
<langsyntaxhighlight lang="holyc">U8 n, *g;
U8 min = 1, max = 100;
 
Line 1,943:
if (Str2I64(g) > n)
Print("Your guess was too high.\nTry again: ");
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
 
<syntaxhighlight lang="icon">
<lang Icon>
procedure main()
smallest := 5
Line 1,968:
}
end
</syntaxhighlight>
</lang>
 
Output:
Line 1,986:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">require 'misc'
game=: verb define
assert. y -: 1 >. <.{.y
Line 1,996:
smoutput (*x-n){::'You win.';'Too high.';'Too low.'
end.
)</langsyntaxhighlight>
 
Note: in computational contexts, J programmers typically avoid loops. However, in contexts which involve progressive input and output and where event handlers are too powerful (too complicated), loops are probably best practice.
Line 2,002:
Example use:
 
<syntaxhighlight lang="text"> game 100
Guess my integer, which is bounded by 1 and 100
Guess: 64
Line 2,013:
Too low.
Guess: 44
You win.</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight Javalang="java">import java.util.Random;
import java.util.Scanner;
public class Main
Line 2,043:
} while (guessedNumber != randomNumber);
}
}</langsyntaxhighlight>
Demonstration:
<pre>The number is between 1 and 100.
Line 2,060:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="html4strict"><p>Pick a number between 1 and 100.</p>
<form id="guessNumber">
<input type="text" name="guess">
Line 2,066:
</form>
<p id="output"></p>
<script type="text/javascript"></langsyntaxhighlight>
<langsyntaxhighlight lang="javascript">var number = Math.ceil(Math.random() * 100);
function verify() {
Line 2,087:
}
 
document.getElementById('guessNumber').onsubmit = verify;</langsyntaxhighlight>
<syntaxhighlight lang ="html4strict"></script></langsyntaxhighlight>
 
=== Spidermonkey Version ===
<langsyntaxhighlight lang="javascript">#!/usr/bin/env js
 
function main() {
Line 2,135:
 
main();
</syntaxhighlight>
</lang>
 
Example session:
Line 2,155:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function guesswithfeedback(n::Integer)
number = rand(1:n)
print("I choose a number between 1 and $n\nYour guess? ")
Line 2,169:
end
 
guesswithfeedback(10)</langsyntaxhighlight>
 
{{out}}
Line 2,181:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="kotlin">import kotlin.random.Random
 
fun main() {
Line 2,196:
}
}
}</langsyntaxhighlight>
Sample inout/output
{{out}}
Line 2,213:
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
<lang Scheme>
{def game
 
Line 2,235:
 
{game {pow 2 32}} // 2**32 = 4294967296
</syntaxhighlight>
</lang>
 
Sample inout/output
Line 2,276:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">#!/usr/bin/lasso9
 
local(
Line 2,308:
#status = true
}
}</langsyntaxhighlight>
 
With range value 8 and 73. Correct number 13
Line 2,324:
 
=={{header|LFE}}==
<langsyntaxhighlight lang="lisp">
(defmodule guessing-game
(export (main 0)))
Line 2,346:
(: random uniform 10)
(get-player-guess)))
</syntaxhighlight>
</lang>
 
From the LFE REPL (assuming the above code was saved in the file "guessing-game.lfe"):
 
<langsyntaxhighlight lang="lisp">
> (slurp '"guessing-game.lfe")
#(ok guessing-game)
Line 2,365:
ok
>
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
[start]
target = int( rnd( 1) * 100) +1
Line 2,385:
if c >target then print " Your guess was too high."
wend
</syntaxhighlight>
</lang>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">command guessTheNumber lowN highN
local tNumber, tguess, tmin, tmax
if lowN is empty or lowN < 1 then
Line 2,416:
end if
end repeat
end guessTheNumber</langsyntaxhighlight>
Test
<langsyntaxhighlight LiveCodelang="livecode">command testGuessNumber
guessTheNumber --defaults to 1-10
guessTheNumber 9,12
end testGuessNumber</langsyntaxhighlight>
 
=={{header|Locomotive Basic}}==
 
<langsyntaxhighlight lang="locobasic">10 CLS:RANDOMIZE TIME
20 PRINT "Please specify lower and upper limits":guess=0
30 INPUT " (must be positive integers) :", first, last
Line 2,437:
110 INPUT "That's correct! Another game (y/n)? ", yn$
120 IF yn$="y" THEN 20
</syntaxhighlight>
</lang>
 
Output:
Line 2,445:
=={{header|Logo}}==
{{trans|UNIX Shell}}
<langsyntaxhighlight lang="logo">to guess [:max 100]
local "number
make "number random :max
Line 2,470:
]
end
</syntaxhighlight>
</lang>
 
Sample run:<pre>? guess
Line 2,487:
=={{header|Lua}}==
 
<langsyntaxhighlight Lualang="lua">math.randomseed(os.time())
me_win=false
my_number=math.random(1,10)
Line 2,509:
end
end
</syntaxhighlight>
</lang>
 
<pre>
Line 2,527:
=={{header|M2000 Interpreter}}==
{{trans|BASIC256}}
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module GuessNumber {
Read Min, Max
Line 2,555:
}
GuessNumber 5, 15
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,561:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">GuessANumber := proc(low, high)
local number, input;
randomize():
Line 2,577:
end if;
end do:
end proc:</langsyntaxhighlight>
<syntaxhighlight lang Maple="maple">GuessANumber(2,5);</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">guessnumber[min_, max_] :=
Module[{number = RandomInteger[{min, max}], guess},
While[guess =!= number,
Line 2,590:
ToString@max <> "."]]];
CreateDialog[{"Well guessed!", DefaultButton[]}]];
guessnumber[1, 10]</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
{{untested|Octave}}
Tested in MATLAB. Untested in Octave.
<langsyntaxhighlight MATLABlang="matlab">function guess_a_number(low, high)
 
if nargin < 1 || ~isnumeric(low) || length(low) > 1 || isnan(low)
Line 2,620:
gs = '';
end
end</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
<lang MAXScript>
Range = [1,100]
randomNumber = (random Range.x Range.y) as integer
Line 2,639:
)
)
</syntaxhighlight>
</lang>
 
Output:
<syntaxhighlight lang="maxscript">
<lang MAXSCRIPT>
Enter a number between 1 and 100: 5
Too low!
Line 2,656:
Well guessed!
OK
</syntaxhighlight>
</lang>
 
=={{header|Mirah}}==
<langsyntaxhighlight lang="mirah">def getInput:int
s = System.console.readLine()
Integer.parseInt(s)
Line 2,684:
puts "Please enter an integer."
end
end</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE guessf;
 
IMPORT InOut, Random, NumConv, Strings;
Line 2,726:
InOut.WriteString ("Thank you for playing; have a nice day!");
InOut.WriteLn
END guessf.</langsyntaxhighlight>
<pre>I have chosen a number below 1000; please try to guess it.
Enter your guess : 500
Line 2,741:
 
=={{header|Nanoquery}}==
<langsyntaxhighlight Nanoquerylang="nanoquery">import Nanoquery.Util
 
random = new(Random)
Line 2,777:
end
 
println "\nThanks for playing."</langsyntaxhighlight>
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">using System;
using System.Console;
 
Line 2,806:
} while (guess != secret)
}
}</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,871:
 
return
</syntaxhighlight>
</lang>
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">; guess-number-feedback.lsp
; oofoe 2012-01-19
; http://rosettacode.org/wiki/Guess_the_number/With_feedback
Line 2,901:
(println "\nWell guessed! Congratulations!")
 
(exit)</langsyntaxhighlight>
 
Sample output:
Line 2,919:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import random, strutils
 
randomize()
Line 2,941:
else: echo " Ye-Haw!!"
 
echo "Thanks for playing."</langsyntaxhighlight>
 
=={{header|NS-HUBASIC}}==
<langsyntaxhighlight NSlang="ns-HUBASIChubasic">10 NUMBER=RND(10)+1
20 INPUT "I'M THINKING OF A NUMBER BETWEEN 1 AND 10. WHAT IS IT? ",GUESS
30 IF GUESS>NUMBER THEN PRINT "MY NUMBER IS LOWER THAN THAT.": GOTO 20
40 IF GUESS<NUMBER THEN PRINT "MY NUMBER IS HIGHER THAN THAT.": GOTO 20
50 PRINT "THAT'S THE CORRECT NUMBER."</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">use IO;
 
bundle Default {
Line 2,985:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let rec _read_int() =
try read_int()
with _ ->
Line 3,023:
loop ()
in
loop ()</langsyntaxhighlight>
 
Playing the game:
Line 3,056:
 
=={{header|Octave}}==
<langsyntaxhighlight Octavelang="octave">function guess_a_number(low,high)
% Guess a number (with feedback)
% http://rosettacode.org/wiki/Guess_the_number/With_feedback
Line 3,082:
end
end
disp('Well guessed!')</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">import: console
 
: guessNumber(a, b)
Line 3,096:
g n == ifTrue: [ "You found it !" .cr return ]
g n < ifTrue: [ "Less" ] else: [ "Greater" ] . "than the target" .cr
again ;</langsyntaxhighlight>
 
=={{header|Ol}}==
<syntaxhighlight lang="ol">
<lang ol>
(import (otus random!))
 
Line 3,126:
((= guess number)
(print "Well guessed!")))))
</syntaxhighlight>
</lang>
 
=={{header|ooRexx}}==
Line 3,136:
entering ? shows the number we are looking for
This program should, of course, also work with all other Rexxes
<syntaxhighlight lang="oorexx">
<lang ooRexx>
/*REXX program that plays the guessing (the number) game. */
low=1 /*lower range for the guessing game.*/
Line 3,193:
 
ser: say; say '*** error ! ***'; say arg(1); say; return
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">guess_the_number(N=10)={
a=random(N);
print("guess the number between 0 and "N);
Line 3,210:
);
print("You guessed it correctly")
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 3,216:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">sub prompt {
my $prompt = shift;
while (1) {
Line 3,234:
while ($_ = $tgt <=> prompt "Your guess");
 
print "Correct! You guessed it after $tries tries.\n";</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Guess_the_number3.exw
Line 3,275:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
<?php
 
Line 3,329:
</body>
</html>
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
{{trans|PureBasic}}
<langsyntaxhighlight PicoLisplang="picolisp">(de guessTheNumber ()
(use (Low High Guess)
(until
Line 3,351:
"Your guess is too "
(if (> Number Guess) "low" "high")
"." ) ) ) ) )</langsyntaxhighlight>
Output:
<pre>: (guessTheNumber)
Line 3,364:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">The low number is 1.
 
The high number is 100.
Line 3,391:
Convert the string to the number.
If the number is less than the low number, write " Guess can't be lower than " then the low number then "." on the console; repeat.
If the number is greater than the high number, write " Guess can't be higher than " then the high number then "." on the console; repeat.</langsyntaxhighlight>
{{out}}
<pre>
Line 3,419:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-Guess
{
Line 3,465:
 
Write-Host ("The number was {0} and it took {1} guesses to find it." -f $answer.Number, $answer.Guesses.Count)
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,490:
{{works with|SWI-Prolog|6}}
 
<langsyntaxhighlight lang="prolog">main :-
play_guess_number.
 
Line 3,526:
; Guess > N -> writeln('Your guess is too high.')
; Guess =:= N -> writeln("Correct!")
).</langsyntaxhighlight>
 
 
Input in the standard Prolog top level is terminated with a `.`: E.g.,
 
<langsyntaxhighlight lang="prolog">?- main.
Guess an integer between 1 and 10.
Guess the number: a.
Invalid input.
Guess the number: 3.
Your guess is too low.</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">OpenConsole()
 
Repeat
Line 3,558:
PrintN("Your guess is to high.")
EndIf
ForEver</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import random
 
inclusive_range = (1, 100)
Line 3,586:
if answer > target: print(" Too high.")
 
print("\nThanks for playing.")</langsyntaxhighlight>
 
'''Sample Game'''
Line 3,623:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ say "Guess the number (1-100 inclusive.)"
cr cr
100 random 1+
Line 3,641:
say "Well done! "
echo say " is correct. " cr
drop ] is guess-the-number ( --> )</langsyntaxhighlight>
 
{{out}}
Line 3,675:
This solution works on the assumption that the number to be found is an integer and also assumes that the upper and lower bounds are distinct integers used inclusively. For example, this means that low=4 and high=5 should be a solvable case, but low=high=4 will throw an error. See [[Talk:Guess the number/With feedback|Talk page]] entry dated 1st June 2020.
 
<langsyntaxhighlight lang="rsplus">guessANumber <- function(low, high)
{
boundryErrorCheck(low, high)
Line 3,707:
while(!is.numeric(guess) || as.integer(guess) != guess){guess <- type.convert(readline("That wasn't an integer! Try again "))}
as.integer(guess)
}</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define (guess-number min max)
Line 3,728:
 
(guess-number 1 100)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" perl6line>my $maxnum = prompt("Hello, please give me an upper boundary: ");
until 0 < $maxnum < Inf {
say "Oops! The upper boundary should be > 0 and not Inf";
Line 3,755:
}
}
say "Great you guessed right after $count attempts!";</langsyntaxhighlight>
 
<pre>Hello, please give me an upper boundary: 10
Line 3,767:
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">: high|low ( gn-g$ )
over > [ "high" ] [ "low" ] if ;
 
Line 3,782:
think [ getToken toNumber checkGuess ] while
"You got it!\n" puts ;
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
To make the program more engaging, randomized words for the hint are used.
<langsyntaxhighlight lang="rexx">/*REXX program plays guess the number game with a human; the computer picks the number*/
low= 1 /*the lower range for the guessing game*/
high= 100 /* " upper " " " " " */
Line 3,820:
/*stick a fork in it, we're all done. */
if try==1 then say 'Gadzooks!!! You guessed the number right away!'
else say 'Congratulations!, you guessed the number in ' try " tries."</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">fr = 1 t0 = 10
while true
see "Hey There,
Line 3,853:
d = random(e)
if d >= s return d ok
end</langsyntaxhighlight>
 
=={{header|Ruby}}==
{{trans|Mirah}}
<langsyntaxhighlight lang="ruby">number = rand(1..10)
 
puts "Guess the number between 1 and 10"
Line 3,875:
puts "Please enter an integer."
end
end</langsyntaxhighlight>
 
=={{header|Rust}}==
{{libheader|rand}}
<langsyntaxhighlight lang="rust">use rand::Rng;
use std::cmp::Ordering;
use std::io;
Line 3,916:
}
}
}</langsyntaxhighlight>
<pre>I have chosen my number between 1 and 100. Guess the number!
Please input your guess.
Line 3,952:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import java.util.Random
import java.util.Scanner
 
Line 3,968:
else if (guessedNumber < randomNumber) println("Your guess is too low!")
else println("You got it!")
} while (guessedNumber != randomNumber)</langsyntaxhighlight>
 
=={{header|Scheme}}==
{{works with|Chicken Scheme}}
{{works with|Guile}}
<langsyntaxhighlight lang="scheme">(define maximum 5)
(define minimum -5)
(define number (+ (random (- (+ maximum 1) minimum)) minimum))
Line 3,990:
(if (< guess number)
(display "Too low!\n> ")))))
(display "Correct!\n")</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
const integer: lower_limit is 0;
Line 4,019:
writeln("You gave up!");
end if;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">var number = rand(1..10);
say "Guess the number between 1 and 10";
 
Line 4,032:
default { say "Too high" }
}
}</langsyntaxhighlight>
 
=={{header|Small Basic}}==
<langsyntaxhighlight Smalllang="small Basicbasic">number=Math.GetRandomNumber(10)
TextWindow.WriteLine("I just thought of a number between 1 and 10. What is it?")
While guess<>number
Line 4,046:
EndIf
EndWhile
TextWindow.WriteLine("You win!")</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
Line 4,056:
To play: run "GuessingGame playFrom: 1 to: 100" on a Workspace (Tools -> Workspace)
 
<syntaxhighlight lang="smalltalk">
<lang Smalltalk>
'From Pharo7.0.3 of 12 April 2019 [Build information: Pharo-7.0.3+build.158.sha.0903ade8a6c96633f07e0a7f1baa9a5d48cfdf55 (64 Bit)] on 30 October 2019 at 4:24:17.115807 pm'!
Object subclass: #GuessingGame
Line 4,154:
play! !
 
</syntaxhighlight>
</lang>
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="sparkling">printf("Lower bound: ");
let lowerBound = toint(getline());
 
Line 4,183:
break;
}
}</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">import Cocoa
 
var found = false
Line 4,208:
println("Good try but the number is less than that!")
}
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">set from 1
set to 10
set target [expr {int(rand()*($to-$from+1) + $from)}]
Line 4,230:
break
}
}</langsyntaxhighlight>
Sample output:
<pre>
Line 4,251:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
PRINT "Find the luckynumber (7 tries)!"
Line 4,272:
IF (round==7) PRINT/ERROR "You've lost: luckynumber was: ",luckynumber
ENDLOOP
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,299:
{{works with|Public Domain Korn SHell}}
{{works with|Z SHell}}
<langsyntaxhighlight lang="sh">function guess {
[[ -n $BASH_VERSION ]] && shopt -s extglob
[[ -n $ZSH_VERSION ]] && set -o KSH_GLOB
Line 4,324:
fi
done
}</langsyntaxhighlight>
 
Sample run:
Line 4,343:
=={{header|Ursa}}==
{{trans|Python}}
<langsyntaxhighlight lang="ursa">decl int high low
set low 0
set high 100
Line 4,372:
end while
 
out endl "Thanks for playing." endl console</langsyntaxhighlight>
 
=={{header|Vala}}==
<langsyntaxhighlight lang="vala">
void main(){
const int from = 1;
Line 4,407:
}//while
} // main
</syntaxhighlight>
</lang>
 
Shorter but no error checking
<langsyntaxhighlight lang="vala">int main() {
int guess, x = Random.int_range(1, 10);
stdout.printf("Make a guess (1-10): ");
Line 4,418:
stdout.printf("Got it!\n");
return 0;
}</langsyntaxhighlight>
 
=={{header|VBA Excel}}==
The Application.InputBox display a message when input is inappropriate.
<langsyntaxhighlight lang="vb">Sub GuessTheNumberWithFeedback()
Dim Nbc&, Nbp&, m&, n&, c&
 
Line 4,439:
Loop
MsgBox "Well guessed!" & vbCrLf & "You find : " & Nbc & " in " & c & " guesses!"
End Sub</langsyntaxhighlight>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Dim max,min,secretnum,numtries,usernum
max=100
Line 4,469:
End If
Loop
</syntaxhighlight>
</lang>
 
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">import rand.seed
import rand
import os
Line 4,496:
}
}
}</langsyntaxhighlight>
 
=={{header|VTL-2}}==
<langsyntaxhighlight VTL2lang="vtl2">10 ?="Minimum? ";
20 L=?
30 ?="Maximum? ";
Line 4,522:
220 ?="Correct!"
230 ?="Tries: ";
240 ?=T</langsyntaxhighlight>
{{out}}
<pre>Minimum? 10
Line 4,541:
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">import "io" for Stdin, Stdout
import "random" for Random
 
Line 4,561:
break
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,586:
 
=={{header|XLISP}}==
<langsyntaxhighlight lang="lisp">(defun guessing-game (a b)
; minimum and maximum, to be supplied by the user
(defun prompt ()
Line 4,611:
(display ". Try to guess it!")
(newline)
(prompt))</langsyntaxhighlight>
{{out}}
<pre>[1] (guessing-game 19 36)
Line 4,633:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
int Lo, Hi, C, Guess, Number;
 
Line 4,656:
CrLf(0);
until Guess = Number;
]</langsyntaxhighlight>
 
Example output:
Line 4,674:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">r:=(0).random(10)+1;
while(1){
n:=ask("Num between 1 & 10: ");
Line 4,680:
if(n==r){ println("Well guessed!"); break; }
println((n<r) and "small" or "big");
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,697:
=={{header|Zoomscript}}==
For typing:
<langsyntaxhighlight Zoomscriptlang="zoomscript">var randnum
var guess
randnum & random 1 10
Line 4,713:
endif
endwhile
print "Correct number. You win!"</langsyntaxhighlight>
 
For importing:
Line 4,720:
 
=={{header|ZX Spectrum Basic}}==
<langsyntaxhighlight lang="zxbasic">ZX Spectrum Basic has no [[:Category:Conditional loops|conditional loop]] constructs, so we have to emulate them here using IF and GO TO.
1 LET n=INT (RND*10)+1
2 INPUT "Guess a number that is between 1 and 10: ",g: IF g=n THEN PRINT "That's my number!": STOP
3 IF g<n THEN PRINT "That guess is too low!": GO TO 2
4 IF g>n THEN PRINT "That guess is too high!": GO TO 2</langsyntaxhighlight>
10,327

edits