2020-02-02

Guess the number

因為週末沒有外出,所以寫的練習小程式,猜測一個位於 1 - 1000 內的數字。

#!/usr/bin/env tclsh

proc rand_range {min max} { 
    return [expr int(rand()*($max-$min+1)) + $min] 
}

set answer [rand_range 1 1000]
while {1} {
    puts -nonewline "Please input a number to guess (1-1000): "
    flush stdout
    gets stdin guess
    if {$guess == $answer} {
        break;
    } else {
        if {$guess < $answer} {
             puts "Please guess more higher"
        } else {
             puts "Please guess more lower"
        }
    }
}

沒有留言: