refactor code, add guess limit

This commit is contained in:
mollusk 2019-11-27 14:56:04 -07:00
parent cbe0ebe17d
commit eab4d9001b

View File

@ -1,6 +1,6 @@
# This is just an example to get you started. A typical binary package
# uses this file as the main entry point of the application.
import system, os, random, strutils
import system, os, random, strutils,typetraits
when isMainModule:
#[
@ -24,19 +24,30 @@ when isMainModule:
proc geussNum() =
randomize()
var rng = rand(101)
while true:
stdout.write("Type a number: ")
var sroll = readLine(stdin)
var iroll = parseInt(sroll)
if rng > iroll:
echo "Number is greater"
elif rng < iroll:
echo "Number is less"
elif rng == iroll:
echo "Winner!"
var tries = 5
var guessed = 0
echo "I'm thinking of a number between 1 & 100"
echo "You have ", tries, " tries"
while guessed < tries:
stdout.write("Guess the number: ")
var guess = readLine(stdin).parseInt()
if guess > rng:
echo "Too high"
guessed += 1
echo "Guesses Made: ", guessed
elif guess < rng:
echo "Too low"
guessed += 1
echo "Guesses Made: ", guessed
elif guess == rng:
echo "You have guessed the number: ", rng, "Good job!"
quit(0)
else:
echo "not a number"
echo "You did not guess the number, it was : ", rng
proc help(): string =