Stuck with or in a loop. :-)

I am having trouble creating a loop that will continue this script until all questions have been answered correctly. Yes, I’m still a student and I’m pretty bad at loops so far. If any one has any ideas I would appreciate hearing them. Thanks



Script Name:Math Quiz Module 7

Version:

Author:Michael Coffey

Date:11/19/2013

Description:


#Clear the windows console
Clear-Host

Define the variables used in this script to store players answers

$question1 = “”
$question2 = “”
$question3 = “”
$question4 = “”
$question5 = “”

Define a variable to keep track of the number of correct answers

quiz questions

$nocorrect = 0

Display games opening screen

Write-Host “nnnntt " WELCOME TO THE MATH QUIZ.”"
Write-Host
Write-Host “nnnttt “By Michael Coffey””
Write-Host “nnnnnnnnnn “Press Enter To continue:””
#Pause script execution until player presses enter key
Read-Host
#Clear the windows console screen
Clear-Host

Provide instruction for player

write-host “nn The Math Quiz will test your knowledge of basic Math Skillsn" Write-host " The Quiz Has Five Questionsn”
Write-Host " At The end Of The Quiz You Will Be Assigned A Rankn" Write-Host " Based On The Number Of Correct Answersn"
Write-Host " Score: 5 Correct = Einstien (Expert)"
Write-Host “tt 4 Correct = CPA”
Write-Host “tt 3 Correct = You Can Balance A Checkbook”
Write-Host “tt 2 Correct = Better Use A debit Card”
Write-Host “tt 1 Correct = Always Have Exact Change”
Write-Host “tt 0 Correct = Have Someone Else Handle Your Money”
Write-Host “nnnn Press Enter To Continue”
#Pause script and wait for player input
Read-Host
#Ask the first question
while (($question1 -ne “a”)-and ($question1 -ne “b”)`
-and($question1 -ne “c”) -and ($question1 -ne “d”))
{
Clear-Host # Clear the windows console
Write-Host
Write-Host " What is the sum of 2+2?"
Write-Host
Write-Host " A. 22"
Write-Host " B. 4 "
Write-Host " C. 12 "
Write-host " D. 75"
Write-Host
$question1 = Read-Host “Type The Letter Of The Correct Answer And Press The Enter Key”
}

Clear the windows console

Clear-host
#Ask the second question
while (($question2 -ne “a”)-and ($question2 -ne “b”)`
-and($question2 -ne “c”) -and ($question2 -ne “d”))
{
Clear-Host # Clear the windows console
Write-Host
Write-Host " What is 10 multiplied by 10?"
Write-Host
Write-Host " A. 1010"
Write-Host " B. 1100 "
Write-Host " C. 100 "
Write-host " D. 0011"
Write-Host
$question2 = Read-Host “Type The Letter Of The Correct Answer And Press The Enter Key”
}

Clear the windows console

Clear-host
#Ask the third question
while (($question3 -ne “a”)-and ($question3 -ne “b”)`
-and($question3 -ne “c”) -and ($question3 -ne “d”))
{
Clear-Host # Clear the windows console
Write-Host
Write-Host " What is the square root Of 9?"
Write-Host
Write-Host " A. 3"
Write-Host " B. 99 "
Write-Host " C. 33 "
Write-host " D. Roots grow on Trees"
Write-Host
$question3 = Read-Host “Type The Letter Of The Correct Answer And Press The Enter Key”
}

Clear the windows console

Clear-host
#Ask the fourth question
while (($question4 -ne “a”)-and ($question4 -ne “b”)`
-and($question4 -ne “c”) -and ($question4 -ne “d”))
{
Clear-Host # Clear the windows console
Write-Host
Write-Host " What is the value of Pi?"
Write-Host
Write-Host " A. 3.14"
Write-Host " B. 1/8 "
Write-Host " C. Cherry "
Write-host " D. 31.4"
Write-Host
$question4 = Read-Host “Type The Letter Of The Correct Answer And Press The Enter Key”
}

Clear the windows console

Clear-host
#Ask the fifth question
while (($question5 -ne “a”)-and ($question5 -ne “b”)`
-and($question5 -ne “c”) -and ($question5 -ne “d”))
{
Clear-Host # Clear the windows console
Write-Host
Write-Host " What 1000 divided by 100?"
Write-Host
Write-Host " A. 10000"
Write-Host " B. 10 "
Write-Host " C. 100 "
Write-host " D. 150"
Write-Host
$question5 = Read-Host “Type The Letter Of The Correct Answer And Press The Enter Key”
}
#Clear the windows console
Clear-host
Write-host
Write-host " Press the Enter Key to get your score."
#pause script
Read-host
#Clear the windows console screen
Clear-host
#Grade the quiz
if ($question1 -eq “B”) {$noCorrect++} # Correct answer B
if ($question2 -eq “C”) {$noCorrect++} # Correct answer C
if ($question3 -eq “A”) {$noCorrect++} # Correct answer A
if ($question4 -eq “A”) {$noCorrect++} # Correct answer A
if ($question5 -eq “B”) {$noCorrect++} # Correct answer B
#Assign a rank based on correct answers
if ($noCorrect -eq 0){
write-host
write-host " You did not answer any questions correctly."
Write-host
Write-host " You should let someone else handle your money."
}
if ($noCorrect -eq 1){
write-host
write-host " You answered one question correctly."
Write-host
Write-host " You should always have exact change."
}
if ($noCorrect -eq 2){
write-host
write-host " You answered two question correctly."
Write-host
Write-host " You should use a debit card."
}
if ($noCorrect -eq 3){
write-host
write-host " You answered three question correctly."
Write-host
Write-host " You can balance a checkbook."
}
if ($noCorrect -eq 4){
write-host
write-host " You answered four question correctly."
Write-host
Write-host " You might be a CPA."
}
if ($noCorrect -eq 5){
write-host
write-host " You answered five question correctly."
Write-host
Write-host " You are as smart as Einstien."
}

All of the loops in this script (including the one you’re trying to add) are good opportunities to use do…while or do…until, rather than while. This ensures that the code inside the loop executes at least once. For example, here’s a simplified version of your current code, with an outer loop to test $noCorrect:

while ($noCorrect -lt 1)
{
    while ($question1 -ne 'a' -and $question1 -ne 'b')
    {
        $question1 = Read-Host -Prompt 'Enter the letter a'
    }
    
    $noCorrect = 0
    if ($question1 -eq 'a')
    {
        $noCorrect++
    }
}

This code works fine the first time through, because $question1 is not yet assigned (and so is not equal to ‘a’ or ‘b’). The problem is that the second time through the outer loop, $question1 is still assigned a value of ‘b’, so the inner loop just get skipped without ever prompting the user for input, and you wind up in an infinite loop. To fix that problem, you could clear the $question1 - $question5 variables at the end of the outer loop, but using do…while loops makes more sense in this case.

do
{
    do
    {
        $question1 = Read-Host -Prompt 'Enter the letter a'
    } while ($question1 -ne 'a' -and $question1 -ne 'b')
    
    $noCorrect = 0
    if ($question1 -eq 'a')
    {
        $noCorrect++
    }
} while ($noCorrect -lt 1)

Hi Dave,
I haven’t got this working correctly yet. I am still playing with it.Thanks for your help and pointing me in the right direction.