Last active
June 22, 2019 18:40
-
-
Save adilw3nomad/e810cc5d21fc160d422d7fec0465bea4 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import ( | |
"bufio" | |
"encoding/csv" | |
"fmt" | |
"log" | |
"os" | |
) | |
type quizItem struct { | |
question string | |
answer string | |
} | |
func main() { | |
csvFile, err := os.Open("problems.csv") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer csvFile.Close() | |
reader := csv.NewReader(csvFile) | |
records, err := reader.ReadAll() | |
if err != nil { | |
log.Fatal(err) | |
} | |
for i := range records { | |
// Create a quizItem object. | |
quizItem := quizItem{ | |
question: records[i][0], | |
answer: records[i][1], | |
} | |
// Print out the question. | |
fmt.Println("Question: ", quizItem.question) | |
// Create a reader and allow user to input their answer. | |
inputReader := bufio.NewReader(os.Stdin) | |
fmt.Print("Enter your answer now: ") | |
// Expect an answer to be given once they hit return. | |
text, err := inputReader.ReadString('\n') | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Your answer is:", text) | |
// Compare the answer given by the user to the correct answer | |
// Print a response accordingly. | |
if text == quizItem.answer { | |
fmt.Println("Correct!") | |
} else { | |
fmt.Println("WRONG! Answer is: ", quizItem.answer) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment