Have edited the code to properly indent it which should help highlight the issue. The error is because an If statement can only have one Else condition, the basic structure of an If statement is;
If <boolean condition> Then
<true outcome>
Else
<false outcome>
End If
At the moment you have multiple instances of
If <boolean condition> Then
<true outcome>
Else
<false outcome>
Else
<unknown condition>
End If
which is invalid syntax because a basic If statement can only return True or False.
However there is also ElseIf which allows multiple boolean conditions to be specified and return different outcomes.
Which looks like this;
If <boolean condition> Then
<true outcome>
ElseIf <boolean condition> Then
<true outcome>
ElseIf <boolean condition> Then
<true outcome>
Else
<false outcome>
End If
Based on how you are nesting the If statements the code in question could be re-written like this;
Randomize
value = Int((150 * Rnd + 1) * Rnd + lowerbound)
guess = InputBox("guess the number", "guess", "guess here")
If guess = value Then
Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
WScript.Quit
ElseIf guess < value Then
Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
once = 1
Else
Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
End if
once = 1
Do
guess = InputBox("try again", "guess", "guess here")
If guess = value Then
Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
ElseIf guess < value then
Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
Else
Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
End If
Loop
Couple of things to note about the example above
Replaced the + with = in the line
guess+inputbox("guess the number","guess","guess here")
as it won't assign the result of InputBox() to guess which is what I'm assuming you are doing. If you were trying to concatenate the result of InputBox() to guess that still wouldn't work you would have to use
guess = guess + InputBox("guess the number", "guess", "guess here")
If that was the case though personally I prefer to use & over + for string concatenation.
The brackets in MsgBox() appear to be a bit odd, usually you would call MsgBox() without brackets if not returning the result to a variable, like this;
MsgBox "correct", vbOKOnly + vbInformation + vbSystemModal, "guess"
but if you did want to include the brackets (as I prefer to do) you can just use Call like this
Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
which avoids the dreaded
Microsoft VBScript compilation error: Cannot use parentheses when calling a Sub
but still allows the function to be surrounded with brackets when no value is being returned.
You may also noticed that I replaced the hardcoded numeric values in MsgBox() with the Named Constants which is easier for others to read and interpret their meaning. They are also hard-boiled into VBScript so there is no excuse for not using them.
Thinking about what you trying to do thought I could re-factor the code a little bit to hopefully make it work as you expected
Dim value, guess, msg, once
Randomize
value = Int((150 * Rnd + 1) * Rnd + lowerbound)
Do
If once = 0 Then
msg = "guess the number"
Else
msg = "try again"
End If
guess = CInt(InputBox(msg, "guess", "guess here"))
If guess = value Then
Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
WScript.Quit
ElseIf guess < value then
Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
once = 1
Else
Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
once = 1
End If
Loop
Things to take from this;
We only need the one InputBox() inside the loop to handle both the first initial guess and the subsequent "try again" attempts. This means we are not duplicating the same piece of code again, see DRY Principle.
InputBox() returns a string so at first the code was trying to compare a string value with a integer value which will give all sorts of weird results. By casting to integer using CInt() the comparison starts to work as expected.
else ifrather thanelsea lot of the time.elseshould be on same line as theelseto turn it into anelse if. Alsolowerboundisn't defined so will be treated as 0.Select-Casefor faster and more readable code.Select Case Truefaster thenElseIf? That's debatable...