Last active
September 14, 2017 16:41
-
-
Save define-private-public/bba8700c342107750ca46c12ad9f5414 to your computer and use it in GitHub Desktop.
Guess my number [from rosetta code]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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