# HG changeset patch # User IBBoard # Date 1505762836 -3600 # Node ID 54608c26bda28425afe4de21c86cc53e95a4e43d # Parent f4ea1f637f227753653fcc549db4e813746574ac Add Day 2 Q8 code diff -r f4ea1f637f22 -r 54608c26bda2 2-Io/day2-self-study-8.io --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/2-Io/day2-self-study-8.io Mon Sep 18 20:27:16 2017 +0100 @@ -0,0 +1,35 @@ +# It would be nice to do this as a "do while", but Io doesn't support that +target_num := Random value(100) floor asNumber +"Guess the number (attempt 1): " interpolate print +guess := File standardInput readLine asNumber +old_guess := -1 +old_diff := 100 +diff := 100 + +if (guess == target_num, + "CORRECT! Wow! Very lucky!" println, + diff = target_num - guess + for (i, 2, 10, + "Guess the number (attempt #{i}): " interpolate print + old_guess = guess + old_diff = diff + guess = File standardInput readLine asNumber + diff = (target_num - guess) abs + + if (guess == target_num, + "Correct." println + break) + + if (diff == old_diff, + "Same." println, + if (diff < old_diff, + "Warmer!" println, + "Colder." println + ) + ) + ) +) + +# FIXME: Would be nice to say "nope", but changes seem to be wrapped by context +# So the value doesn't change outside the loops +if (diff != 0, "Sorry, the number was #{target_num}") \ No newline at end of file