Results 1 to 37 of 37

Thread: Trying to make a Quiz which picks a question assigned to a random integer

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Trying to make a Quiz which picks a question assigned to a random integer

    Hello!

    This is my first thread here, so hopefully I am doing this right. My code is written in Visual Studio 2010 and I was wondering if I could get a bit of help with my program.

    Code:
    Module Module1
    
        Sub Main()
            Randomize()
            Dim ques() As String = {"After leaving Bethlehem, to which country did Joseph, Mary and Jesus travel?", "Christmas is celebrated on the 25th of December. Who is the patron saint remembered on the 26th of December?", "Every elf has this ornament on the tip of their shoes. Which ornament are we talking about?", "Everyone is familiar with the mistletoe tradition. What is the colour of the berries of the plant?", "Which was the first state In the US to recognize Christmas as an official holiday?", "How many points does a snowflake traditionally have? (type number in words)", "Which country can be credited with the creation of the Christmas beverage, eggnog?", "Traditionally, kids leave out snacks for Santa Claus. What are these snacks?", "What are the gifts that the Three Wise Men gave Baby Jesus, according to Xmas history (please separate your answers with commas and type them in alphabetical order)?", "When does the Russian Orthodox Church celebrate Christmas in the order dd/mm?"}
            Dim ans() As String = {"Egypt", "Stephen", "Bells", "White", "Alabama", "Six", "England", "Turkey", "Gold, Myrrh, Frankincense", "07/01"}
            'Dim anotherGo As String = "yes"
            'Dim score As Integer = 0
    
            'Do Until anotherGo = "no"
            Dim Choose As Integer = Math.Round(Rnd() * 9, 0)
            Select Case Choose
                Case 0
                    Console.WriteLine(ques(0))
                Case 1
                    Console.WriteLine(ques(1))
                Case 2
                    Console.WriteLine(ques(2))
                Case 3
                    Console.WriteLine(ques(3))
                Case 4
                    Console.WriteLine(ques(4))
                Case 5
                    Console.WriteLine(ques(5))
                Case 6
                    Console.WriteLine(ques(6))
                Case 7
                    Console.WriteLine(ques(7))
                Case 8
                    Console.WriteLine(ques(8))
                Case 9
                    Console.WriteLine(ques(9))
            End Select
            If Console.ReadLine() = "Which was the first state In the US to recognize Christmas as an official holiday?" Then
                Console.WriteLine("Your answer is: ")
                If Console.ReadLine() = ans(0) Then
                    Console.WriteLine("Correct!")
                Else
                    Console.WriteLine("You are wrong! The answer was: " & ans(0))
                End If
            End If
            'Loop
            Console.ReadKey()
        End Sub
    
    End Module
    I haven't finished the code yet, but when the line "Which was the first state In the US to recognize Christmas as an official holiday?" comes up, it doesn't prompt me with "Your answer is ". I have tried replacing the quotation with ques(5) and it still didn't work.

    Is there another way I can make it so a random number is chosen which makes a random question appear? And I also tried to make it so that all the questions are asked in one go without the question repeating itself.

    Thank you!
    Last edited by si_the_geek; Dec 12th, 2017 at 08:47 AM. Reason: added Code tags

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Welcome to VBForums

    When you post code please put it inside code tags so it is displayed in a more readable way - either using the Code/VBCode buttons in the post editor screen (or at the top of the Quick Reply box), or by putting them in manually, like this: [code] code here [/code] (I have added them to your post)


    I haven't finished the code yet, but when the line "Which was the first state In the US to recognize Christmas as an official holiday?" comes up, it doesn't prompt me with "Your answer is ". I have tried replacing the quotation with ques(5) and it still didn't work.
    I don't really understand your intentions with that line, as it expects the user to enter an entire question, and if they get that exactly right then you show "Your answer is: " and allow them to enter an answer to the original question (which you check in a sensible way).

    I would recommend removing that line (and the matching End If), as you should then get correct behaviour for asking one question and checking the answer.

    Is there another way I can make it so a random number is chosen which makes a random question appear? And I also tried to make it so that all the questions are asked in one go without the question repeating itself.
    To start with you should be using the Random class rather than Rnd and Randomize (which were replaced in 2002, when .Net was released).

    In order to avoid repeated questions you need to keep track of which ones have been asked (or not asked) so far, and eliminate the chance of already-asked ones being picked again.

    A sensible way to do that is to start by creating a List of all the valid question numbers, and instead of picking a random question pick a random item from that List, and remove the item you just picked (so next time that question number will no longer be available).



    Also note that the entire Select Case section can be replaced by this:
    Code:
            Console.WriteLine(ques(Choose))

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Hello and thank you,

    So what should I replace the If statement with? If this question was picked then I should get this answer.
    And for the repeated questions bit, would I create another list with all the questions in and then remove the questions after its been asked? How would I remove the questions from the list and how would I know whether its been asked?

    I am sorry for all these questions, I am quite new to VB programming.

    Thank you!

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Quote Originally Posted by LukasPookas View Post
    So what should I replace the If statement with? If this question was picked then I should get this answer.
    You don't need to replace that lines I referred to with anything.

    In the code after it however (including the other If) you need to change ans(0) to ans(Choose)


    And for the repeated questions bit, would I create another list with all the questions in and then remove the questions after its been asked? How would I remove the questions from the list and how would I know whether its been asked?
    No, the other list would just be the question numbers (0, 1, 2, ...).

    You then randomly pick an element from that list (from 0 to .Count-1 ), retrieve the element at that position and store it in Choose, then remove the element at the position.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Hi,

    I don't really understand. I wanted it so If a question was chosen from the random number generator, the If statement under it would check whether the Answer for that specific question is correct. The question 5 I used was just an example. For some reason, my current code won't output "Your answer is" when the question i put in the if statement appears. I changed the ans(0) to ans(choose), but I am not sure what changes it made.

    When I made the list, I wrote: Dim validQues As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} which doesn't work. I also don't know how to remove an element.

    Thanks!
    Last edited by LukasPookas; Dec 12th, 2017 at 11:05 AM.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Quote Originally Posted by LukasPookas View Post
    I don't really understand. I wanted it so If a question was chosen from the random number generator, the If statement under it would check whether the Answer for that specific question is correct.
    That section of code should become this:
    Code:
            'If Console.ReadLine() = "Which was the first state In the US to recognize Christmas as an official holiday?" Then
                Console.WriteLine("Your answer is: ")
                If Console.ReadLine() = ans(Choose) Then
                    Console.WriteLine("Correct!")
                Else
                    Console.WriteLine("You are wrong! The answer was: " & ans(Choose))
                End If
            'End If
            'Loop
    When I made the list, I wrote: Dim validQues As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} which doesn't work. I also don't know how to remove an element.
    ah, that is an Array rather than a List.

    They are similar, but a List is designed to allow you to easily add and remove items (plus some other useful features), and removing items is the important part here.

    You can set it up like this:
    Code:
    Dim validQues As New List(Of Integer) 
    For i as Integer = 0 To 9
      validQues.Add(i)
    Next
    And if memory serves you can remove an item at a specific position like this:
    Code:
    validQues.RemoveAt(3)

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    You're being offered help with solving "How do I display a random question from a list?" because that's the title of the thread. I don't think si_the_geek has noticed you asked a different question and have a different problem, and that "making a quiz application" is really just the context for that problem.

    Here's the line where things went wrong:
    Code:
    If Console.ReadLine() = "Which was the first state In the US to recognize Christmas as an official holiday?" Then
    Console.ReadLine() is a function that reads input from the console and returns that input as a String. So what this line says is:
    Get the next line the user types. If that line = "Which was the first state [...] holiday?", then...
    I think what you meant to write is:
    Print the current question, and accept a user's answer from the console. If that answer matches the correct answer for this question, then...
    Do you see how different those are?

    If you want to print something to the console, you use Console.WriteLine(). You use Console.ReadLine() if you want to get something from the user. There's not a way to do both at the same time.

    So if we ignore the larger problem of "selecting a random question/answer pair", the way you ask a question, get a response, and validate the response should look like this:
    Code:
    Dim question As String = "What number is four?"
    Console.WriteLine(question)
    
    Dim answer As String = Console.ReadLine()
    
    If answer = "4" Then
        Console.WriteLine("Good job!")
    Else
        Console.WriteLine("Uh...")
    End If
    Given your two arrays, you know that the String for the question is in the array "ques", and the String for the answer is in the array "ans". Go ahead right now and rename them "questions" and "answers". You're going to lose points from your homework assignment if you use names like "ques" and "ans". It's also a bad practice. Imag if I wrt snt lk this, can u grok? No? Then don't write code like that either!

    Anyway, assuming you rename those arrays, if we only ever ask the first question:
    Code:
    Dim question As String = questions(0)
    Console.WriteLine(question)
    
    Dim answer As String = Console.ReadLine()
    
    If answer = answers(0) Then
        Console.WriteLine("Good job!")
    Else
        Console.WriteLine("Uh...")
    End If
    Changing from "the first question" to "a random question" doesn't take much work.

    Code:
    Dim rng As New Random()
    Dim index As Integer = rng.Next(0, questions.Length)
    
    Dim question As String = questions(index)
    Console.WriteLine(question)
    
    Dim answer As String = Console.ReadLine()
    
    If answer = answers(index) Then
        Console.WriteLine("Good job!")
    Else
        Console.WriteLine("Uh...")
    End If
    Notice how I started with an easy problem, "Ask one question." Then I turned it into a harder problem, "As one of many questions." Then I was able to turn that into the solution I wanted in the first place: "Ask a random one of many questions." This is how (many) experts write code! Solving really simple problems is easy, and usually solving a hard problem involves tweaking those small solutions ever closer.

    The next step would be, "Ask each question in sequence in a random order." That's the problem other experts are trying to help you solve. I don't think they noticed you hadn't quite got there yet.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    I had a question, but it's ok, I have just figured it out.

    Thank you!
    Last edited by LukasPookas; Dec 12th, 2017 at 12:47 PM.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Ah, I see. Thank you for your help!

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Quote Originally Posted by Sitten Spynne View Post
    You're being offered help with solving "How do I display a random question from a list?" because that's the title of the thread. I don't think si_the_geek has noticed you asked a different question and have a different problem, and that "making a quiz application" is really just the context for that problem.
    You appear to have misread... in each of my posts I have been referring to both of the questions (the one you answered, and the 'no repeats' one), albeit using hints rather than explicit code until post #6 (which was virtually the same time as your post).

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Thank you for your help,

    And sorry for asking so many questions, but does the For Loop go around the whole code because I want it to keep looping until all the questions run out, or where does it go? And inside the If statement, I put the validQues bit right? And how would I get the integer value of the question to remove it from my validQues list? Would I do validQues.RemoveAt(Choose)?

    Thank you very much

  12. #12
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Again, it helps to think of the problem in terms of "what's the easiest thing to solve?"

    Let's say we've already got our list of questions.

    I want to do something with each item in the array and stop when I've done something with all of them.
    NORMALLY we would use a For or ForEach loop to work with arrays. Those loops are harder to use properly if we want to remove items from the array. So in this case a While loop is pretty fine. Sometimes it helps to write code with <magic> inside it to help us understand what things might look like:
    Code:
    While <the array isn't empty>
        <get a random element>
        <get the question for that element's index>
        <ask the question>
        <judge the answer>
        <remove the element from the array>
    End While
    We have to figure out how to do the <magic> parts. These are all pretty easy on their own!

    "<the array isn't empty>" is easy enough to check. It has a Length property. If that property is 0, it has no items and is empty. So:
    Code:
    While validQuestions.Length > 0
    (Sorry si_the_geek, I skimmed most of the threads and didn't notice that particular question getting answered.)

    If the length is bigger than zero, we know there's still questions to ask.

    The next "magic" bit you really haven't seen is <remove the element from the array>. That's a little bit of a lie. Removing items from an array is actually tough. It's best to not bother. si_the_geek showed you in post #6 how to create a list with the right numbers in it. List(Of T) has a .RemoveAt() method that can remove an element at the indicated index. So if I fill in all the magic you need to do that:
    Code:
    Dim validQuestions As New List(Of Integer)()
    For i As Integer = 0 To 9
        validQuestions.Add(i)
    Next
    
    Dim rng As New Random()
    
    While validQuestions.Count > 0
        Dim currentIndex As Integer = rng.Next(0, validQuestions.Count)
    
        <get the question for that element's index>
        <ask the question>
        <judge the answer>
    
        validQuestions.RemoveAt(currentIndex)
    End While
    One quirk of List(Of T) is MS decided 'Count' makes more sense than 'Length' for everything that isn't an array. This is consistent with everything they made afterwards, so we live with it. (There's a pretty good reason why they did it, but again it's way off topic.)

    You've already seen how to get a question, ask the question, and judge the answer, so filling in that "magic" ought to be doable.

    (Traditionally, to use arrays for this, you'd take a different approach. Instead of "generate a list of numbers, pick a random number and remove it", to use arrays you would "generate a list of numbers, shuffle the list, then choose each item in order." This alternative is used because of the aforementioned difficulties and inefficiencies with removing items from arrays. But it's a lesson in itself to show how to shuffle an array, so I agree with si_the_geek's choice of technique. The "problems" it has aren't going to be problems in an assignment this small.)
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Quote Originally Posted by Sitten Spynne View Post
    (Sorry si_the_geek, I skimmed most of the threads and didn't notice that particular question getting answered.)
    No worries, we're all human.

    (Traditionally, to use arrays for this, you'd take a different approach. Instead of "generate a list of numbers, pick a random number and remove it", to use arrays you would "generate a list of numbers, shuffle the list, then choose each item in order." This alternative is used because of the aforementioned difficulties and inefficiencies with removing items from arrays. But it's a lesson in itself to show how to shuffle an array, so I agree with si_the_geek's choice of technique. The "problems" it has aren't going to be problems in an assignment this small.)
    The List is far easier to understand, but my personal preference is a kind of semi-shuffle (when picking an element, copy the final item over it and reduce the maximum allowable index). I'm not sure about the randomness in comparison to a proper shuffle, but it is simpler code to write and marginally helps with speed.

  14. #14
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    It's probably sufficient. I'd use a shuffle because I'm used to it, but if I stumbled into the implementation you described in a code review I'd shrug and keep going.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Hi, thank you for your response.

    May I ask, why do we have to create another random number to remove the number chosen? Everything seemed to just slip past me, I completely understand what I was doing wrong now. It seems to be very easy except for the removing bit.

    Thank you!

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Quote Originally Posted by LukasPookas View Post
    May I ask, why do we have to create another random number to remove the number chosen?
    There is still only one random number, it is just being used in a slightly different way.

    Previously to determine which question to ask, you were picking a random number up to the amount of questions, and using it to directly refer to the questions... but you could randomly pick the same question next time.

    Now there are a more steps:
    1. as part of the initial setup, create a list of questions that haven't been asked yet (called validQuestions) which starts by containing the index number of all the questions (so: 0,1,2,3,4,5,6,7,8,9).

    2. to determine which question to ask, pick a random element from validQuestions, and use the item in that position to refer to the question

    3. to make sure the same question can't be asked again, remove the element from validQuestions you just used (using the same random number you picked in step 2).


    Step 2 will initially give the same outcome as you would have got before (because the element at position 3 is 3), but the next time it will be different because you have removed an item (if you removed the 3, the element at position 3 will now be 4).

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Sorry, I mean't why do we create another random number variable and use it in order to get rid of the number chosen, but I understand now, thanks.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Hi,

    I realized my mistakes from the other bits and realized that they were pretty easy to do, however I am still not sure how to remove the correct question from the list. I would've thought that validQues.RemoveAt(Choose) would've done it because whatever number was chosen, it would remove it from the list as well, but it didn't seem to work. What you wrote got a random number which was assigned to a number between 0 and the length of validQues, but it doesn't have anything to do with the numbers in Choose.
    So basically what I am trying to achieve is whatever number that Choose chooses has to be removed from the list.

    Thank you
    Last edited by LukasPookas; Dec 13th, 2017 at 11:45 AM.

  19. #19
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    OK, let's take a step back and explain it.

    What's really happening is we're using a technique sometimes called "parallel arrays". It doesn't really have a name but that name is convenient.

    So we have a "questions" array, and an "answers" array. We set it up so that questions(0) and answers(0) correspond to each other. This is true for each element, answer 1 is the answer for question 1, answer 2 -> question 2, etc.

    We also have a validQuestions list. It's not complex: it's the numbers 0, 1, 2, 3... all the way up to our number of questions.

    So let's say we have 3 questions, for simplicity. Our data structures could look like:
    Code:
    Dim questions() = { "What is four?", "What is seven?", "What is zero?" }
    Dim answers() = { "4", "7", "0" }
    Dim validQuestions As New List(Of Integer)() = { 0, 1, 2 }
    That's not 100% valid syntax, but it illustrates the point.

    When we want to ask a question, we get a random number between 0 and the upper bound of the validQuestions array, then use that number to figure out which question/answer pair we want to use. After we ask the question, we remove that number from validQuestions. Let's try an iteration.

    Code:
    Dim randomIndex = rng.Next(0, validQuestions.Count)
    validQuestions has 3 items, so this will return 0, 1, or 2 randomly. Let's say it returns 1.
    Code:
    Dim randomIndex = rng.Next(0, validQuestions.Count)
    ' 1
    
    Dim questionIndex = validQuestions(1)
    ' 1
    
    Dim question As String = questions(1)
    Dim answer As String = answers(1)
    <ask the question>
    <judge the answer>
    
    validQuestions.RemoveAt(questionIndex)
    Now we've removed 1 from validQuestions. It looks like this:
    Code:
    { 0, 2 }
    We haven't changed the arrays of questions and answers, but now the only valies in validQuestions are 0 and 2, which makes sense because we haven't asked question 0 or question 2 yet.

    So let's do another iteration:
    Code:
    Dim randomIndex = rng.Next(0, validQuestions.Count)
    validQuestions has 2 items, so we'll get 0 or 1. If we get 0, we ask question 0, then remove the element at 0. Now validQuestions is:
    Code:
    { 2 }
    This indicates we've asked every question but 2.

    On the next iteration, the RNG can only return 0 since that's the last item in validQuestions. After removing it, validQuestions.Count is 0 and the While loop (not pictured in my post) will terminate.

    I can't explain why your code isn't working because you didn't post it. The #1 waste of time on this forum is posting an explanation of your problem but not posting the code that causes it. I can't fix your paragraph, but I can tell you why your code isn't working.

    Post the code, what you tried with it, what you expected it to do, and what it did instead.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  20. #20
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Also side note:

    The code you have doesn't work. The code we're suggesting looks very different, other than having arrays of questions and answers.

    The right thing to do with code that doesn't work is delete it. Throw it away. It's broken. Even as an expert, if you decide the code is wrong, it's much easier to try again and get it right than it is to make tiny changes and have it suddenly start working.

    When I see you fretting about how to get "Choose" working, I have a feeling your code is now a mixture of your old, broken code and our new, suggested code. You can't fix a broken car by welding it to a working car. Delete the code that isn't working and replace it with code that does.

    This probably sounds like a waste of effort now but this is a very, very small program. Some of the features I work on span more than 5,000 lines across dozens of files. I've been part of updates that deleted more than half of them for replacement. You get used to it, and deleting a few dozen lines isn't really a loss in the big picture.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    I think I posted it in the beginning. However, it isn't the full version. It was a rough copy. I tried a lot of things out but none of them seemed to work. It's got a lot of quotations all over the place, so it might be a bit hard to read, sorry about that.

    Code:
    Module Module1
    
        Sub Main()
            Randomize() 'calls randomize function
            Dim ques() As String = {"After leaving Bethlehem, to which country did Joseph, Mary and Jesus travel?", "Christmas is celebrated on the 25th of December. Who is the patron saint remembered on the 26th of December?", "Every elf has this ornament on the tip of their shoes. Which ornament are we talking about?", "Everyone is familiar with the mistletoe tradition. What is the colour of the berries of the plant?", "Which was the first state In the US to recognize Christmas as an official holiday?", "How many points does a snowflake traditionally have? (type number in words)", "Which country can be credited with the creation of the Christmas beverage, eggnog?", "Traditionally, kids leave out snacks for Santa Claus. What are these snacks?", "What are the gifts that the Three Wise Men gave Baby Jesus, according to Xmas history (please separate your answers with commas and type them in alphabetical order)?", "When does the Russian Orthodox Church celebrate Christmas in the order dd/mm?"} 'an array of the questions
            Dim ans() As String = {"Egypt", "Stephen", "Bells", "White", "Alabama", "Six", "England", "Turkey", "Gold, Myrrh, Frankincense", "07/01"} 'an array of the answers of the questions
            Dim score As Integer = 0 'new variable to keep player's score
            'Dim rndNo As New Random()
            Dim validQues As New List(Of Integer) 'a list is used to make it easier to remove and add items. Inside the brackets is the type of elements in the list. This list is a list for all the questions that haven't been asked
            For i As Integer = 0 To 9 'i is an integer between 0 - 9
                validQues.Add(i) 'integers between 0 and 9 are added to the list, and this loops until all are added
            Next 'ends for loop
            Dim name As String 'variable to store player's name
            Console.WriteLine("Welcome to my Christmas Quiz! There are many questions awaiting to be answered… A random question will show up and you have to enter the answer. You get a point for every correct answer. What is your name?") 'asks user to input their name
            name = Console.ReadLine() 'user's input is their name
            Console.WriteLine("Well hello " & name & ". Merry Christmas and good luck!") 'prints this message with the user's name
    
            While validQues.Count > 0 'whilst the amount of questions that haven't been asked is more than none (Count is used to see how many numbers are left)
                Dim Choose As Integer = Math.Round(Rnd() * 9, 0) 'variable to store randomly generated numbers between 0 - 9
                'Dim quesUsed As New List(Choose)
                Select Case Choose 'case created to make something happen depending on the random number chosen
                    Case 0 'number 0 chosen
                        Console.WriteLine(ques(0)) 'prints the first question in the array
                    Case 1 'number 1 chosen
                        Console.WriteLine(ques(1)) 'prints the second question in the array
                    Case 2 'number 2 chosen
                        Console.WriteLine(ques(2)) 'prints the third question in the array
                    Case 3 'number 3 chosen
                        Console.WriteLine(ques(3)) 'prints the fourth question in the array
                    Case 4 'number 4 chosen
                        Console.WriteLine(ques(4)) 'prints the fifth question in the array
                    Case 5 'number 5 chosen
                        Console.WriteLine(ques(5)) 'prints the sixth question in the array
                    Case 6 'number 6 chosen
                        Console.WriteLine(ques(6)) 'prints the seventh question in the array
                    Case 7 'number 7 chosen
                        Console.WriteLine(ques(7)) 'prints the eigth question in the array
                    Case 8 'number 8 chosen
                        Console.WriteLine(ques(8)) 'prints the ninth question in the array
                    Case 9 'number 9 chosen
                        Console.WriteLine(ques(9)) 'prints the tenth question in the array
                End Select 'ends select
                If Console.ReadLine = LCase(ans(Choose)) Then 'if the answer given by the user has the same number in the ans array as the random number chosen. The answer is also all put into lower-case as the computer is case sensitive
                    Console.WriteLine("Correct!") 'prints "Correct!"
                    score = score + 1 'adds a point to their score
                    Console.WriteLine("Your score is: " & score) 'displays their score
                Else 'if the answer was wrong 
                    Console.WriteLine("You are wrong! The answer was: " & ans(Choose)) 'prints the fact that you got the answer wrong then prints the actual answer
                End If 'ends the if statement
                'validQues.RemoveAt(quesUsed)
            End While 'ends the while loop
            If score >= 8 Then 'if their final score is more than or equal to 8
                Console.WriteLine("Well done " & name & "! You got a very good score! Have a Merry Xmas!") 'outputs this message with your name
            ElseIf score <= 3 Then 'if their final score is less than or equal to 3
                Console.WriteLine("Well, you didnt do too well. Better luck next time " & name) 'outputs this message with your name
            Else 'if their scores aren't 8 and above or 3 and below
                Console.WriteLine("Not a bad effort " & name & ". Have a merry Xmas!") 'outputs this message with your name
            End If 'ends if
            Console.ReadKey() 'so the program doesn't close
        End Sub
    
    End Module
    I completely understand everything, it's my fault for not posting my code so we misunderstood each-other. I apologize.

    Thank you.

  22. #22
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Yes, I was right. Your code is a mishmash of all of our solutions glued together, and you didn't properly integrate them. For example, while you added code to remove items from "validQues" (then commented it out :/) you aren't using validQues to determine the new question.

    Read my post #7. Did it use a Select..Case? Read si_the_geek's #6: did he suggest a Select..Case? That's one warning bell that you're in the weeds. The Select..Case doesn't even make sense. The code behaves like this:
    Code:
    Generate a random number.
    If the number is 0:
        Use question 0.
    Else if the number is 1:
        Use question 1.
    Else if...
    It is much smarter and less tedious to say:
    Code:
    Generate a random number.
    Print the question represented by the random number.
    Or:
    Code:
    Dim questionIndex = rng.Next(0, 9)
    Dim question = questions(questionIndex)
    That's like what I did in my #7, and like what si_the_geek suggested in #6.

    But I left a step out. Let's fill in some of the magic. For reference, we're starting at:
    Code:
    Dim validQuestions As New List(Of Integer)()
    For i As Integer = 0 To 9
        validQuestions.Add(i)
    Next
    
    Dim rng As New Random()
    
    While validQuestions.Count > 0
        Dim currentIndex As Integer = rng.Next(0, validQuestions.Count)
    
        <get the question for that element's index>
        <ask the question>
        <judge the answer>
    
        validQuestions.RemoveAt(currentIndex)
    End While
    The part that's probably not apparent to you from the start is <get the question for that element's index>. #19 is a demonstration, but let's implement it.

    We generated a random number that points into the validQuestions array. That array has the list of all questions that have NOT been asked yet. So we need to get a random number out of it, and use THAT number to determine our question/answer pair. It would look something like this:
    Code:
    Dim validQuestions As New List(Of Integer)()
    For i As Integer = 0 To 9
        validQuestions.Add(i)
    Next
    
    Dim rng As New Random()
    
    While validQuestions.Count > 0
        Dim currentIndex As Integer = rng.Next(0, validQuestions.Count)
        Dim questionIndex As Integer = validQuestions(questionIndex)
    
        Dim question As String = questions(questionIndex)
        Dim answer As String = answers(questionIndex)
    
        <ask the question>
        <judge the answer>
    
        validQuestions.RemoveAt(currentIndex)
    End While
    This is a good bit more complex than I like this problem, but making it less complex involves using Classes to keep our questions and answers together. That isn't really hard, but if this is a homework problem you can be penalized for using things you haven't been taught yet, so I am forced to avoid it.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    I completely understand everything, it's just that I used a Case statement which made things a bit different. And no, the validQues was a test, that's why I commented it out. I was just putting some stuff in and seeing what happens I was just playing around with the code... It's just the remove the question bit that was getting me.

    I do programming in my freetime and I self teach myself Python, however I am still a beginner to programming itself, but I do know more than my classmates, so I do a little research from time to time.

    Thank you
    Last edited by LukasPookas; Dec 13th, 2017 at 12:30 PM.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Isn't there a way I could use a Case statement and still do this or not really?
    Thanks

  25. #25
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    You say you completely understand everything, but your post #18 indicates you don't.
    Using a list containing numbers to be chosen is a computer model of using a hat with a bunch of slips of papers with numbers on them.
    You use the Random number to choose which slip of paper is pulled from the hat, not which number is pulled from the hat.
    Once you choose that slip of paper, you remove it from the list (it is no longer in the hat).
    Now you use the number that was on that slip of paper to select the question.

    The random number is not selecting the question, it is selecting a "pointer" from the list that "points" to the question, and you are removing that pointer from the list once you've selected it.

  26. #26
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Quote Originally Posted by LukasPookas View Post
    Isn't there a way I could use a Case statement and still do this or not really?
    Thanks
    Yes, you can continue to use the Case statement. It is verbose unnecessary code, but it will work.

  27. #27
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Quote Originally Posted by LukasPookas View Post
    Isn't there a way I could use a Case statement and still do this or not really?
    Thanks
    The most appropriate way to frame this is with a metaphor:

    I'm handing you a hammer. You're asking if it's still OK to use a heavy rock.

    "It will work" is not false and I can't lie and tell you there's no way to make Select..Case work.

    "It will cost you more time, make your program harder to understand, and every expert will ignore your real question while telling you to remove it" is also true.

    If you want to use the Select..Case statement I don't think I want to keep answering. Select..Case is just a slightly more clear way to make a large If..Else statement. You are not in a scenario that needs a large If..Else statement or a Select..Case statement. If you want to do it "for fun", that's fine, but "for fun" projects don't help people who read this post in the future to try and find the answer.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    In 18 I just said I realized my mistakes, but I don't understand the remove bit.

    I am a bit confused as to where you are trying to go here... I think we already sorted that out. All I wanted to do was for it to get a random question then remove it from a list of "valid questions", but I wasn't sure how I would get it to remove from the list of valid questions... I understand his code and how it works...

  29. #29

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Quote Originally Posted by Sitten Spynne View Post
    The most appropriate way to frame this is with a metaphor:

    I'm handing you a hammer. You're asking if it's still OK to use a heavy rock.

    "It will work" is not false and I can't lie and tell you there's no way to make Select..Case work.

    "It will cost you more time, make your program harder to understand, and every expert will ignore your real question while telling you to remove it" is also true.

    If you want to use the Select..Case statement I don't think I want to keep answering. Select..Case is just a slightly more clear way to make a large If..Else statement. You are not in a scenario that needs a large If..Else statement or a Select..Case statement. If you want to do it "for fun", that's fine, but "for fun" projects don't help people who read this post in the future to try and find the answer.
    I am sorry if I came to the wrong place, is this not a place for beginners? Yeah, we want to learn and write more complex code, but at the moment I want to write it as a Case statement.

    And yes, it's technically for fun. In the very beginning I stated it was a "Christmas Quiz".
    Last edited by LukasPookas; Dec 13th, 2017 at 12:52 PM.

  30. #30

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Hi,

    How could I use a Case statement and do this? I can't seem to find out how to prevent the questions from being asked again. I tried stuff like this Dim quesUsed As Integer = Choose(0, validQues.Count) or changing Dim Choose As Integer = Math.Round(Rnd() * 9, 0) to Dim Choose As New Random() then doing Dim quesUsed As Integer = Choose(0, validQues.Count), but in the case statement it showed this error: Operator '=' is not defined for types 'System.Random' and 'Integer'.

    Thank you

  31. #31
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    I gave you another metaphor, i.e. slips of paper in a hat in post #25.
    If you didn't see it, I suggest you read over it. Perhaps you should read it again anyway. Rather than continue to beat this thing further, and there are numerous things that should be improved, I just changed the code you posted in post #21 to emphasize the hat with slips of paper metaphor. I left the rest of the code alone (except I use the random object, not the rnd function).
    Examine the code and see if you can see how using the list to select the number of the question from the list and remove that number from the list correlates to all the posts in this thread.
    Code:
    Module Module1
    
      Sub Main()
        'Randomize() 'calls randomize function
        Dim ques() As String = {"After leaving Bethlehem, to which country did Joseph, Mary and Jesus travel?", "Christmas is celebrated on the 25th of December. Who is the patron saint remembered on the 26th of December?", "Every elf has this ornament on the tip of their shoes. Which ornament are we talking about?", "Everyone is familiar with the mistletoe tradition. What is the colour of the berries of the plant?", "Which was the first state In the US to recognize Christmas as an official holiday?", "How many points does a snowflake traditionally have? (type number in words)", "Which country can be credited with the creation of the Christmas beverage, eggnog?", "Traditionally, kids leave out snacks for Santa Claus. What are these snacks?", "What are the gifts that the Three Wise Men gave Baby Jesus, according to Xmas history (please separate your answers with commas and type them in alphabetical order)?", "When does the Russian Orthodox Church celebrate Christmas in the order dd/mm?"} 'an array of the questions
        Dim ans() As String = {"Egypt", "Stephen", "Bells", "White", "Alabama", "Six", "England", "Turkey", "Gold, Myrrh, Frankincense", "07/01"} 'an array of the answers of the questions
        Dim score As Integer = 0 'new variable to keep player's score
        Dim rndNo As New Random()
        Dim validQues As New List(Of Integer) 'a list is used to make it easier to remove and add items. Inside the brackets is the type of elements in the list. This list is a list for all the questions that haven't been asked
        For i As Integer = 0 To 9 'i is an integer between 0 - 9
          validQues.Add(i) 'integers between 0 and 9 are added to the list, and this loops until all are added
        Next 'ends for loop
        Dim name As String 'variable to store player's name
        Console.WriteLine("Welcome to my Christmas Quiz! There are many questions awaiting to be answered… A random question will show up and you have to enter the answer. You get a point for every correct answer. What is your name?") 'asks user to input their name
        name = Console.ReadLine() 'user's input is their name
        Console.WriteLine("Well hello " & name & ". Merry Christmas and good luck!") 'prints this message with the user's name
    
        While validQues.Count > 0 'whilst the amount of questions that haven't been asked is more than none (Count is used to see how many numbers are left)
          Dim SlipOfPaper As Integer = rndNo.Next(0, validQues.Count) 'pick one of the avaliable numbers
          Dim NumberOnSlip As Integer = validQues(SlipOfPaper)
          validQues.RemoveAt(SlipOfPaper)
          'Dim quesUsed As New List(Choose)
          Select Case NumberOnSlip 'case created to make something happen depending on the random number chosen
            Case 0 'number 0 chosen
              Console.WriteLine(ques(0)) 'prints the first question in the array
            Case 1 'number 1 chosen
              Console.WriteLine(ques(1)) 'prints the second question in the array
            Case 2 'number 2 chosen
              Console.WriteLine(ques(2)) 'prints the third question in the array
            Case 3 'number 3 chosen
              Console.WriteLine(ques(3)) 'prints the fourth question in the array
            Case 4 'number 4 chosen
              Console.WriteLine(ques(4)) 'prints the fifth question in the array
            Case 5 'number 5 chosen
              Console.WriteLine(ques(5)) 'prints the sixth question in the array
            Case 6 'number 6 chosen
              Console.WriteLine(ques(6)) 'prints the seventh question in the array
            Case 7 'number 7 chosen
              Console.WriteLine(ques(7)) 'prints the eigth question in the array
            Case 8 'number 8 chosen
              Console.WriteLine(ques(8)) 'prints the ninth question in the array
            Case 9 'number 9 chosen
              Console.WriteLine(ques(9)) 'prints the tenth question in the array
          End Select 'ends select
          If Console.ReadLine = LCase(ans(NumberOnSlip)) Then 'if the answer given by the user has the same number in the ans array as the random number chosen. The answer is also all put into lower-case as the computer is case sensitive
            Console.WriteLine("Correct!") 'prints "Correct!"
            score = score + 1 'adds a point to their score
            Console.WriteLine("Your score is: " & score) 'displays their score
          Else 'if the answer was wrong 
            Console.WriteLine("You are wrong! The answer was: " & ans(NumberOnSlip)) 'prints the fact that you got the answer wrong then prints the actual answer
          End If 'ends the if statement
          'validQues.RemoveAt(quesUsed)
        End While 'ends the while loop
        If score >= 8 Then 'if their final score is more than or equal to 8
          Console.WriteLine("Well done " & name & "! You got a very good score! Have a Merry Xmas!") 'outputs this message with your name
        ElseIf score <= 3 Then 'if their final score is less than or equal to 3
          Console.WriteLine("Well, you didnt do too well. Better luck next time " & name) 'outputs this message with your name
        Else 'if their scores aren't 8 and above or 3 and below
          Console.WriteLine("Not a bad effort " & name & ". Have a merry Xmas!") 'outputs this message with your name
        End If 'ends if
        Console.ReadKey() 'so the program doesn't close
      End Sub
    
    End Module

  32. #32

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    When things get a little bit complicated, sometimes I just can't get my head around things like that.

    So the paper is to get a number from the list and the number on the slip just gets the number from the list from the slipofpaper to use in the case statement? Hopefully I got it right

    Thank you!

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    And then to remove the question I would do validQues.RemoveAt(SlipOfPaper) right?

    Thanks!

  34. #34
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Quote Originally Posted by LukasPookas View Post
    ...
    So the paper is to get a number from the list and the number on the slip just gets the number from the list from the slipofpaper to use in the case statement? ...!
    I don't quite understand that question, probably because of mixing metaphore with implementation.
    As a metaphore,
    The random number is used to select a slip of paper from the hat. The number on the slip of paper is the index into the array to select the question.
    As an implementation,
    A random number is generated to select an item from the list, and to remove that item from the list.
    The item from the list is a number (an integer) which is used to index into the question and answer arrays to display the question and to check the answer.
    Since the item was removed from the array, you can't select that item (index into the Q&A arrays) again.

    You use the index in a needless case statement to access the question array. (The whole point of using an array is so you can index into the array, rather than have a separate line of code for each item in the array. You're using the index to select a line of code, rather than using the index to select the array item.)

    And then to remove the question I would do validQues.RemoveAt(SlipOfPaper) right?
    Yes, that should be in the code posted.

    p.s. As already stated, you could comment out the 20 or so lines of your "Select Case" block, and add the line

    Console.WriteLine(ques(NumberOnSlip)) 'prints the question

    in its place.
    Last edited by passel; Dec 13th, 2017 at 01:44 PM.

  35. #35

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    oh sorry it wasn't clear, I was trying to take a guess at the purpose of the slip of paper and the number.

    I was trying to explain what they were doing, so what the paper was getting a random number from the list (to of-course use and remove a question) and the number on the slip is how we pick out the numbers, by using the number we got and putting it in the case statement. I understand what you are saying, I was just like clarifying it.

    I used an array because my teacher wanted me to, so basically he wants us to put everything we learnt into this, so I guess everything was a bit inconvenient. And yeah, it was in the post, I missed it sorry.

    And we dont need to call Randomize() function right?

  36. #36
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Quote Originally Posted by LukasPookas View Post
    ...
    And we dont need to call Randomize() function right?
    Correct. Randomize is not applicable when using a Random Class instance.

    Also, "Gold, Myrrh, Frankincense" is not in alphabetical order.

  37. #37

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    19

    Re: Trying to make a Quiz which picks a question assigned to a random integer

    Oh yeah, forgot about that. Thanks for pointing it out.

    Thanks for the help! Merry Christmas!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width