Skip to content

Instantly share code, notes, and snippets.

@define-private-public
Last active September 14, 2017 16:41
Show Gist options
  • Save define-private-public/bba8700c342107750ca46c12ad9f5414 to your computer and use it in GitHub Desktop.
Save define-private-public/bba8700c342107750ca46c12ad9f5414 to your computer and use it in GitHub Desktop.
Guess my number [from rosetta code]
import random, 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 ValueError:
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."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment