gophercises / quiz Goto Github PK
View Code? Open in Web Editor NEWEx 1 - Run timed quizzes via the command line
Home Page: https://gophercises.com
Ex 1 - Run timed quizzes via the command line
Home Page: https://gophercises.com
$ go build . && ./quiz -csv=problems.csv
panic: runtime error: index out of range [1] with length 1
goroutine 1 [running]:
main.parseLines(0xc00000c030, 0x1, 0x1, 0x1, 0x0, 0x0)
/home/mitul/go/src/gophercises/quiz/main.go:44 +0x14f
main.main()
/home/mitul/go/src/gophercises/quiz/main.go:24 +0x2d9
facing run time error after running the solution 1 , i've not gone through the solution 2 video yet
But looking for an answer for why this error happening ?
Hi there,
Thanks for putting together these exercises! They're giving me a way to learn some areas of Go I don't get a chance to use very often at work.
I have a question regarding this lesson. This question is a bit long, so thanks in advance for reading through it.
Like your solution, my solution for gathering user input used a goroutine. Also, as with your solution, my solution used a time.Timer
to timeout. Finally, as with your solution, my solution "kind of sort of" leaks the goroutine that's monitoring stdin for user input. That goroutine might also write to a closed channel once the user does hit "Return".
I say it "kind of sort of" leaks this goroutine because the process will exit shortly after the timeout, so the leak in this case is very short-lived. But still... And if the parent goroutine closes the channel after a timeout the application will panic:
panic: send on closed channel
goroutine 25 [running]:
main.main.func1(0xc4200762a0)
/Users/rich_youngkin/Software/repos/go/src/github.com/youngkin/gophercises/quiz/solution/main.go:38 +0x121
created by main.main
/Users/rich_youngkin/Software/repos/go/src/github.com/youngkin/gophercises/quiz/solution/main.go:35 +0x5d2
To make this happen I closed the channel on what is now line 44 in your part 2 solution:
select {
case <-timer.C:
fmt.Println()
Added ======> close(answerCh) <======= this line
break problemloop
case answer := <-answerCh:
if answer == p.a {
correct++
}
}
This is all a bit messy. I've spent some time thinking about how to have the user input goroutine also timeout and exit without attempting to write to the potentially closed channel. But after issuing the read to stdin
it seems like there's no nice way to timeout the actual waiting for input, or to check if a timeout occurred while waiting for user input. For example, this won't reliably work as the order in which the case
conditions are evaluated is random:
ans, _ := in.ReadString('\n')
select {
case c <- ans:
return
case <-ctx.Done():
return
}
I was wondering if you had any thoughts about how to accomplish this and ensure the goroutine eventually exits without attempting to write to the channel.
Cheers,
Rich
Pydroid3class QuizQuestion:
def init(self, question, options, correct_option):
self.question = question
self.options = options
self.correct_option = correct_option
class QuizGame:
def init(self, questions):
self.questions = questions
self.score = 0
def display_question(self, question_obj):
print(question_obj.question)
for index, option in enumerate(question_obj.options, start=1):
print(f"{index}. {option}")
def get_user_answer(self, question_obj):
while True:
try:
user_answer = int(input("Your answer (enter the option number): "))
if 1 <= user_answer <= len(question_obj.options):
return user_answer
else:
print("Invalid input. Please enter a valid option number.")
except ValueError:
print("Invalid input. Please enter a number.")
def evaluate_answer(self, question_obj, user_answer):
if user_answer == question_obj.correct_option:
print("Correct!")
self.score += 1
else:
print(f"Wrong! The correct answer was option {question_obj.correct_option}.")
def play_game(self):
for question_obj in self.questions:
self.display_question(question_obj)
user_answer = self.get_user_answer(question_obj)
self.evaluate_answer(question_obj, user_answer)
print() # Add a newline for better readability
def show_score(self):
print(f"Your final score: {self.score}/{len(self.questions)}")
question1 = QuizQuestion("What is the capital of France?", ["Paris", "Berlin", "Madrid"], 1)
question2 = QuizQuestion("Which programming language is this quiz written in?", ["Java", "Python", "C++"], 2)
question3 = QuizQuestion("What is 2 + 2?", ["3", "4", "5"], 2)
quiz_questions = [question1, question2, question3]
quiz_game = QuizGame(quiz_questions)
quiz_game.play_game()
quiz_game.show_score()
Look over the README.md then attempt to complete the exercise and submit your solution as a directory inside of the students
directory. Eg if you have a github username of joncalhoun
then place your code in students/joncalhoun/your_code.go
For more info on how these are used see the README in the students directory.
Look over the README.md then attempt to complete the exercise and submit your solution as a directory inside of the students
directory. Eg if you have a github username of joncalhoun
then place your code in students/joncalhoun/your_code.go
For more info on how these are used see the README in the students directory.
Let's take @csos95 's implementation. Suppose one question is answered wrong. How to add that question BACK to the range, so it can be asked again later?
Hello. I wrote code that looks like mycode. So I wanted to ask about difference between creating 2 channels (answer and time) and 1 goroutine for timer only is there any disadvantages in my code?
Hello,
Please, I misunderstood part 2 of the exercise before seeing the solution.
I was thinking that the timeout is for each question and I struggled with it for many days before watching the solution.
This question of StackOverflow explained the challenges I face through https://stackoverflow.com/questions/50797563/how-to-cancel-fmt-scanf-after-a-certain-timeout I am not the author of the question
Can you provide us a solution for part 2 if the timeout is for each solution?
Tasks to be completed:
[ ] Write the first draft of the code
[ ] Outline the screencast
[ ] Record the screencast
[ ] Upload the screencast
[ ] Add the screencast to the course on gophercises.com
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.