locked
Is there a way to escape an "If, ElseIf, Else, EndIf" set? RRS feed

  • Question

  • What I mean is, I am trying out the Easy Challenge 1 for March and I implemented the "If" (routine?), but am wondering if there is an "escape hatch" so the user can try another number after successfully finding the random number (instead of being stuck with the same number until the program is closed), and also so I can have the program count the number of guesses the user made.

    ------------------------------------------------------------------------------------------

    Wednesday, March 21, 2012 6:34 PM

Answers

  • Yes, this can be done, without your code so far its hard to say exactly how to approach it from where you are.

    Based on the challenge 1, perhaps the following pseudo code (just the logic is helpful)

    While the game is in progress
    
      Set a random number X
      Intialise try counter
    
      While number not guessed
    
        Ask for a number
        Update try counter
    
        If it is larger than X
          Output a message
        ElseIf it is smaller then X
          Output a message
        Else it is = X
          Output a message and the try count
          number is guessed (inner while loop will end)
        EndIf
    
      EndWhile
    
      Ask it the user wants to try again
      If No
        The game is not in progress (outer loop will end)
      EndIf
    
    EndWhile

    The actual code for the outer loop may look like this

    inProgress = "True"
    While (inProgress)
      
      TextWindow.WriteLine("Do you want to play again? y/n")
      input = Text.ConvertToLowerCase(TextWindow.Read())
      If (input <> "y") Then
        inProgress = "False"
      EndIf
    EndWhile

    • Edited by litdev Wednesday, March 21, 2012 6:52 PM
    • Marked as answer by Joman Mied Wednesday, March 21, 2012 8:13 PM
    Wednesday, March 21, 2012 6:47 PM
  • Better to try to use While rather than a Goto; with this in mind, the inner loop in the pseudo code looks like:

    TextWindow.WriteLine("Welcome to the number-guessing game! I will think of a number between 1 and 100,and you get to guess it!")
    rn = Math.GetRandomNumber(100)
    guessing = "True"
    While (guessing)
      TextWindow.Write("Please enter your guess:  ")
      guess1 = TextWindow.ReadNumber()
      If guess1 < rn Then
        TextWindow.WriteLine("Sorry, you were too low, please guess again.")
      ElseIf guess1 > rn then
        TextWindow.WriteLine("Sorry, you were too high, please guess again.")
      Elseif guess1 = rn then
        TextWindow.WriteLine("You got it!!!!")
        guessing = "False"
      EndIf
    EndWhile
        
    Now all you need to do is embed this loop inside the outer loop code from above.

    • Marked as answer by Joman Mied Wednesday, March 21, 2012 8:13 PM
    Wednesday, March 21, 2012 7:04 PM

All replies

  • Yes, this can be done, without your code so far its hard to say exactly how to approach it from where you are.

    Based on the challenge 1, perhaps the following pseudo code (just the logic is helpful)

    While the game is in progress
    
      Set a random number X
      Intialise try counter
    
      While number not guessed
    
        Ask for a number
        Update try counter
    
        If it is larger than X
          Output a message
        ElseIf it is smaller then X
          Output a message
        Else it is = X
          Output a message and the try count
          number is guessed (inner while loop will end)
        EndIf
    
      EndWhile
    
      Ask it the user wants to try again
      If No
        The game is not in progress (outer loop will end)
      EndIf
    
    EndWhile

    The actual code for the outer loop may look like this

    inProgress = "True"
    While (inProgress)
      
      TextWindow.WriteLine("Do you want to play again? y/n")
      input = Text.ConvertToLowerCase(TextWindow.Read())
      If (input <> "y") Then
        inProgress = "False"
      EndIf
    EndWhile

    • Edited by litdev Wednesday, March 21, 2012 6:52 PM
    • Marked as answer by Joman Mied Wednesday, March 21, 2012 8:13 PM
    Wednesday, March 21, 2012 6:47 PM
  • okay, here is my import code for what I have so far: KVJ790

    as I am unsure how to weave your solution into my problem.


    ------------------------------------------------------------------------------------------

    Wednesday, March 21, 2012 6:59 PM
  • Better to try to use While rather than a Goto; with this in mind, the inner loop in the pseudo code looks like:

    TextWindow.WriteLine("Welcome to the number-guessing game! I will think of a number between 1 and 100,and you get to guess it!")
    rn = Math.GetRandomNumber(100)
    guessing = "True"
    While (guessing)
      TextWindow.Write("Please enter your guess:  ")
      guess1 = TextWindow.ReadNumber()
      If guess1 < rn Then
        TextWindow.WriteLine("Sorry, you were too low, please guess again.")
      ElseIf guess1 > rn then
        TextWindow.WriteLine("Sorry, you were too high, please guess again.")
      Elseif guess1 = rn then
        TextWindow.WriteLine("You got it!!!!")
        guessing = "False"
      EndIf
    EndWhile
        
    Now all you need to do is embed this loop inside the outer loop code from above.

    • Marked as answer by Joman Mied Wednesday, March 21, 2012 8:13 PM
    Wednesday, March 21, 2012 7:04 PM
  • Thankyou for all of your help :)

    I got my program to work and it's, in my opinion, AWESOME!

    But I couldn't have done it without your knowledge.

    Here's the code if you want to check it out: MZH752


    ------------------------------------------------------------------------------------------

    Wednesday, March 21, 2012 8:08 PM
  • Very good, and this is the one I did from your code using a While for the outer loop, import MZH752-0.

    The thing about GoTo is that it is considered bad style because it can lead to code that jumps around all over the place and can be hard to read or bug fix (spaghetti code).

    Occasionally they can be useful, but if you possibly can, consider other means.

    Wednesday, March 21, 2012 8:13 PM
  • Awesome.

    ------------------------------------------------------------------------------------------

    Wednesday, March 21, 2012 8:19 PM