Created
October 31, 2018 02:17
-
-
Save somarlyonks/51e9e452f6081904d95b88a6828ae04b to your computer and use it in GitHub Desktop.
guess
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main | |
import System | |
readNumber : IO (Maybe Nat) | |
readNumber = do | |
input <- getLine | |
if all isDigit $ unpack input | |
then pure (Just $ cast input) | |
else pure Nothing | |
guess : (target: Nat) -> IO () | |
guess target = do | |
putStr "Guess> " | |
Just answer <- readNumber | Nothing => retry "Invalid input" | |
if answer > target | |
then retry "Too high" | |
else if answer < target | |
then retry "Too low" | |
else putStrLn "Bingo!" | |
where | |
retry : String -> IO () | |
retry msg = do | |
putStrLn msg | |
guess target | |
main : IO() | |
main = do | |
putStrLn "Guess a number between 0 and 100." | |
randomNum <- time | |
guess (cast $ randomNum `mod` 100) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import random | |
def guess(target): | |
def retry(msg): | |
print(msg) | |
guess(target) | |
try: | |
answer = int(input('Guess> ')) | |
except ValueError: | |
retry('Invalid input') | |
if answer > target: | |
retry('Too high') | |
elif answer < target: | |
retry('Too low') | |
else: | |
print('Bingo!') | |
def main(): | |
print('Guess a number between 0 and 100.') | |
target = int(random() * 100) | |
guess(target) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment