refactor code, add guess limit
This commit is contained in:
@@ -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:
|
||||||
|
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)
|
quit(0)
|
||||||
else:
|
echo "You did not guess the number, it was : ", rng
|
||||||
echo "not a number"
|
|
||||||
|
|
||||||
|
|
||||||
proc help(): string =
|
proc help(): string =
|
||||||
|
|||||||
Reference in New Issue
Block a user