Giter Site home page Giter Site logo

Handle wrong answers about quiz HOT 4 CLOSED

gophercises avatar gophercises commented on July 24, 2024
Handle wrong answers

from quiz.

Comments (4)

joncalhoun avatar joncalhoun commented on July 24, 2024

There are a few ways you could do this. One would be to add the question to the back of the slice of questions and to update the for loop that iterates over all questions to check its length every pass. Do you have a need for this? If you have some code where you are trying you can post it and I'll try to help out.

from quiz.

veganexe avatar veganexe commented on July 24, 2024

Yes, using it myself for language learning. Revising the answers i have gotten wrong would help alot! I am currently using csos95’s implementation. Only thing i have changed is removing the time limit flag and channel.

I did make an attempt at this before. But all I could come up with is to print the wrong ones at the end of the quiz using bytes.Buffer.

But adding the adding them back to the slice would be more.... intuitive.

from quiz.

csos95 avatar csos95 commented on July 24, 2024

In my implementation I use a map to store the question and answer pairs.
This gives me a randomized ordering when I range over them, but also means that new items can't simply be added inside the loop because there is no guarantee they would be ranged over.
To re-ask incorrect questions I would add incorrect questions to a second map, swap the qaPair map with this second map, and loop over the part that asks questions until the qaPair map is empty.

// loop until the map is empty
for len(qaPair) != 0 {
	// a second map to store incorrect questions
	qaPairIncorrect := make(map[string]string, 0)
	// iteration order for maps in go is randomized so the questions won't be in the same order every time
	for question, answer := range qaPair {
		qNum++
		// ask a question
		fmt.Printf("Problem #%d: %s = ", qNum, question)
		// get an answer
		scanner.Scan()
		userAnswer = scanner.Text()
		// trim leading and trailing whitespace
		userAnswer = strings.TrimSpace(userAnswer)
		userAnswer = strings.ToLower(userAnswer)
		answer = strings.ToLower(answer)
		// check the answer
		if answer == userAnswer {
			numCorrect++
		} else {
			// if it was incorrect, add it to the second map
			qaPairIncorrect[question] = answer
		}
	}
	// replace the first map with the second
	qaPair = qaPairIncorrect
}

This breaks the last print statement showing the score so I would also change the last argument from len(qaPair) to qNum

from quiz.

joncalhoun avatar joncalhoun commented on July 24, 2024

@csos95 Thanks for chiming in 👍

@veganexe if you need more help let me know, but it looks like the code sample Christopher gave should help get you going 😄

from quiz.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.