File tree 1 file changed +19
-5
lines changed
1 file changed +19
-5
lines changed Original file line number Diff line number Diff line change @@ -9,10 +9,12 @@ import (
9
9
"io/ioutil"
10
10
"log"
11
11
"os"
12
+ "time"
12
13
)
13
14
14
15
func main () {
15
16
filename := flag .String ("filename" , "problems.csv" , "path to problems csv file" )
17
+ limit := flag .Int ("limit" , 30 , "time limit in seconds to complete the quiz" )
16
18
flag .Parse ()
17
19
18
20
// read a csv file from command line, default to problems.csv -filename flag
@@ -49,16 +51,28 @@ func main() {
49
51
// read out each question, then read in answer. keep track of how many are correct and wrong
50
52
scanner := bufio .NewScanner (os .Stdin ) // define our scanner to read stdin
51
53
var correct int
54
+ timer := time .NewTimer (time .Duration (* limit ) * time .Second )
55
+
56
+ problemLoop:
52
57
for _ , p := range problems {
53
58
fmt .Printf ("%s ? >> " , p .Question )
54
- scanner .Scan ()
55
- a := scanner .Text ()
56
- if a == p .Answer {
57
- correct ++
59
+ answerCh := make (chan string )
60
+ go func () {
61
+ scanner .Scan ()
62
+ answerCh <- scanner .Text ()
63
+ }()
64
+ select {
65
+ case <- timer .C :
66
+ fmt .Println ()
67
+ break problemLoop
68
+ case answer := <- answerCh :
69
+ if answer == p .Answer {
70
+ correct ++
71
+ }
58
72
}
73
+
59
74
}
60
75
61
76
// output the results at the end
62
- fmt .Println ("" )
63
77
fmt .Printf ("You answered %d out of %d questions right. Thanks for playing.\n \n " , correct , len (problems ))
64
78
}
You can’t perform that action at this time.
0 commit comments