Prompt the user to select an answer and evaluate the response

The final section is one of the easiest sections.  We prompt the user to select their answer from the hash $displayHash.  We take their selection and compare their selection to the correct answer (first row of $hash).  If they matched the correct answer, we let them know.  Otherwise, we let them know that they were incorrect.  Additionally, since this script always has 5 answers, we let the user break out of the quiz by selecting 6 to EXIT.

# -----------------------------------------------------------------------
# Ask user to make their selection
# -----------------------------------------------------------------------
[int]$selection = Read-Host "Please enter your selection (type 6 to EXIT)"
Write-Host ""

# -----------------------------------------------------------------------
# Compare their selection to the correct answer
# -----------------------------------------------------------------------
If (($displayhash.get_item($selection)) -eq ($hash.get_item(1))) 
    {
    Write-Host "That is correct"
    $Correct++
    }
Elseif ($Selection -eq 6)
    {
    Write-Host "Thanks for playing"
    $percent = ($correct + $incorrect)/$correct
    Write-Host "You got $percent% Correct"
    }
Else
    {
    Write-Host "That is incorrect"
    $Incorrect++
    }
}

No comments:

Post a Comment