From e4199d2837d2179f17e97b8d50366d96c8babded Mon Sep 17 00:00:00 2001 From: Cody Date: Mon, 27 Feb 2023 12:14:16 -0600 Subject: Added while loops & guessing game --- examples/guessing.sloth | 26 ++++++++++++++++++++++++++ examples/test.sloth | 16 ---------------- 2 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 examples/guessing.sloth delete mode 100644 examples/test.sloth (limited to 'examples') diff --git a/examples/guessing.sloth b/examples/guessing.sloth new file mode 100644 index 0000000..5d69944 --- /dev/null +++ b/examples/guessing.sloth @@ -0,0 +1,26 @@ +val computer = random(1, 10); + +var tries = 0; +var correct = false; + +while !correct { + print("\nPick a number between 1 and 5: "); + val human = parse_int(readln()); + + if human == computer { + println("You guessed the same number as me!"); + correct = true; + } + + if human > computer { + println("Your guess was too high."); + } + + if human < computer { + println("Your guess was too low."); + } + + tries = tries + 1; +} + +println("\nIt took you ", tries, " tries to guess correctly!"); diff --git a/examples/test.sloth b/examples/test.sloth deleted file mode 100644 index 7104593..0000000 --- a/examples/test.sloth +++ /dev/null @@ -1,16 +0,0 @@ -print("Pick a number between 1 and 5: "); - -val human = parse_int(readln()); -val computer = random(1, 5); - -if human == computer { - println("You guessed the same number as me!"); -} - -if human > computer { - println("Your guess was too high."); -} - -if human < computer { - println("Your guess was too low."); -} -- cgit v1.2.3