1

what am I doing wrong in this script?

Randomize
value=int((150 * Rnd + 1) * Rnd + lowerbound)
guess+inputbox("guess the number","guess","guess here")
if guess=value then
  msgbox("correct"),0+64+4096,("guess")
  wscript.quit
else
  if guess < value then
    msgbox("too low"),0+32+4096,("guess")
    once=1
  end if
else
  msgbox("too high"),0+32+4096,("guess")
end if
once=1
do
  guess=inputbox("try again","guess","guess here")
  if guess=value then
    msgbox("correct"),0+64+4096,("guess")
  else
    if guess < value then
      msgbox("too low"),0+32+4096,("guess")
    end if
  else
    msgbox("too high"),0+32+4096,("guess")
  end if
loop

If you can figure out what is going wrong that would be great.

It has been saying

expected "end"

and when I do what it says I will run it and it still won't work.

6
  • Do you mean to use else if rather than else a lot of the time. Commented Apr 14, 2016 at 1:56
  • Or perhaps the line under the else should be on same line as the else to turn it into an else if. Also lowerbound isn't defined so will be treated as 0. Commented Apr 14, 2016 at 2:17
  • Alternatively, you can use Select-Case for faster and more readable code. Commented Apr 14, 2016 at 9:36
  • @PankajJaju Is Select Case True faster then ElseIf? That's debatable... Commented Apr 14, 2016 at 10:31
  • @Lankymart - According to MSDN, Case is more efficient. In my experience, I validate 10k test trade files and have found Case to be faster. Commented Apr 14, 2016 at 11:48

1 Answer 1

2

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

  1. 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.

  2. 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.

  3. 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;

  1. 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.

  2. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.