Missing statement block after 'else' keyword.

if($guess -lt ($number + 10)) { #The player's guess was too high Clear-Host #Clear the Windows command console screen Write-Host "`n Sorry. Your guess was too high. You are getting hot. Press Enter to" ` "guess again." Write-Host "`n The secret number was: $number." $guess = "" #Reset the player's guess Read-Host #Pause the game until the player presses the Enter key } elseif($guess -lt ($number + 20)) { #The player's guess was too high Clear-Host #Clear the Windows command console screen Write-Host "`n Sorry. Your guess was too high. You are getting warmer. Press Enter to" ` "guess again." Write-Host "`n The secret number was: $number." $guess = "" #Reset the player's guess Read-Host #Pause the game until the player presses the Enter key } else ($guess -gt $number) { #The player's guess was too high Clear-Host #Clear the Windows command console screen Write-Host "`n Sorry. Your guess was too high. You are getting warmer. Press Enter to" ` "guess again." Write-Host "`n The secret number was: $number." $guess = "" #Reset the player's guess Read-Host #Pause the game until the player presses the Enter key } if ($guess -gt ($number - 10)) { #The player's guess was too low Clear-Host #Clear the Windows command console screen Write-Host "`n Sorry. Your guess was too low. You are getting hot. Press Enter to" ` "guess again." Write-Host "`n The secret number was: $number." $guess = "" #Reset the player's guess Read-Host #Pause the game until the player presses the Enter key } elseif ($guess -gt ($number - 20)) { #The player's guess was too low Clear-Host #Clear the Windows command console screen Write-Host "`n Sorry. Your guess was too low. You are getting warm. Press Enter to" ` "guess again." Write-Host "`n The secret number was: $number." $guess = "" #Reset the player's guess Read-Host #Pause the game until the player presses the Enter key } else ($guess -lt $number) { #The player's guess was too low Clear-Host #Clear the Windows command console screen Write-Host "`n Sorry. Your guess was too low. You are getting warmer. Press Enter to" ` "guess again." Write-Host "`n The secret number was: $number." $guess = "" #Reset the player's guess Read-Host #Pause the game until the player presses the Enter key } if ($guess -eq $number) { #The player has guessed the game's secret number Clear-Host #Clear the Windows command console screen Write-Host "`n Congratulations. You guessed my number! Press Enter" ` "to continue." Write-Host "`n The secret number was: $number." $status = "Stop" #Reset the player's guess Read-Host #Pause the game until the player presses the Enter key } #The player has elected to play again if ($reply -eq "Y") { #Reset variables to their default values $number = 0 $noOfGuesses = 0 $status = "Play" $guess = 0 } else { #The player has decided to quit playing $playGame = "No" #Modify variable indicating that it is time to #terminate game play } #Clear the Windows command console screen Clear-Host What does this mean?

It says my very first "else" statement running properly. I am not sure why this error is occuring




August 24th, 2015 10:09am

Too much code, no actual ques
Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 10:10am

Hi Steven,

the question is? Find the error? I've copied it to ISE and it think you should have a look in Line 147:

else  ($guess -lt $number) {  #The player's guess was too low

This isn't possible. You've to use If or ElseIf.

  • Proposed as answer by heyko 17 hours 0 minutes ago
August 24th, 2015 10:12am

I changed that specific line to elseif, same error occurs.
Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 10:15am

I shortened up the code now
August 24th, 2015 10:15am

The problem is that Else should not have a condition following it. It is the default outcome if neither "if" or "elseif" match so there are no conditions for the statement triggering. An example code block should look like this:

    if($guess -lt ($number + 10)) {  #The player's guess was too high

      Clear-Host  #Clear the Windows command console screen
      Write-Host "`n Sorry. Your guess was too high. You are getting hot. Press Enter to" `
        "guess again."
         Write-Host "`n The secret number was: $number." 
      $guess = ""  #Reset the player's guess
      Read-Host  #Pause the game until the player presses the Enter key

}    
    
    elseif($guess -lt ($number + 20)) {  #The player's guess was too high

      Clear-Host  #Clear the Windows command console screen
      Write-Host "`n Sorry. Your guess was too high. You are getting warmer. Press Enter to" `
        "guess again."
         Write-Host "`n The secret number was: $number." 
      $guess = ""  #Reset the player's guess
      Read-Host  #Pause the game until the player presses the Enter key

 } 

    else {  #The player's guess was too high

      Clear-Host  #Clear the Windows command console screen
      Write-Host "`n Sorry. Your guess was too high. You are getting warmer. Press Enter to" `
        "guess again."
         Write-Host "`n The secret number was: $number." 
      $guess = ""  #Reset the player's guess
      Read-Host  #Pause the game until the player presses the Enter key
}

Notice I have removed the condition from your else statement

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 10:16am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics