refactor code, add guess limit

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