none
help with program RRS feed

  • Question

  • hey everyone, i'm a beginner programmer who just recently started with small basic, i've been noodling around with it for about a week or less and still learning things, i've been starting to write a little number guessing game, but have been having some troubles, here's the code so far:

    guess:
    TextWindow.WriteLine("Please enter a number")
    rnum = Math.GetRandomNumber(100)
    num = TextWindow.ReadNumber()

    If rnum = 0 Then
      rnum = rnum + 1
      EndIf
     
      If num > 100 Then
        TextWindow.WriteLine("Number not valid, guess again")
      EndIf
      Goto guess
     
    If num < rnum Then
      TextWindow.WriteLine("Too low, guess again")
      EndIf
      Goto guess
     
      If num > rnum Then
        TextWindow.WriteLine("Too high, guess again")
       EndIf
       Goto guess
       
        If num = rnum Then
          TextWindow.WriteLine("Congrats, you got it right!")
          EndIf

    my main problem is that after i enter a number, the program won't tell me whether it's too low, too high, or if it is the correct number, also, i'm having some difficulties about where i should put the guess: label. anyone know how to fix this?


    Monday, July 4, 2011 4:32 AM

Answers

  • A couple of other points:

    1] We should place the guess: after the random number is set since we don't want the random number to change with each guess (this would be hard!).  But we want the guess: to ask the question again, so we need:

    rnum = Math.GetRandomNumber(100)
    guess:
    TextWindow.WriteLine("Please enter a number")
    num = TextWindow.ReadNumber()

    2] The random number will be between 1 and 100, so no need to check for it being 0.

    3] We should turn the user input to an integer - it is possible an unhelpful user may enter 3.5.  We can use Math.Floor to do this.

    4] We can use If, ElseIf construct to check for mutually exclusive possibilities.

    Here is the whole thing:

    rnum = Math.GetRandomNumber(100)
    guess:
    TextWindow.WriteLine("Please enter a number")
    num = TextWindow.ReadNumber()
    num = Math.Floor(num)

    If num < 1 or num > 100 Then
      TextWindow.WriteLine("Number not valid, guess again")
      Goto guess
    ElseIf num < rnum Then
      TextWindow.WriteLine("Too low, guess again")
      Goto guess
    ElseIf num > rnum Then
      TextWindow.WriteLine("Too high, guess again")
      Goto guess
    Else 'Must be right
      TextWindow.WriteLine("Congrats, you got it right!")
    EndIf  

    You could add a counter to tell the player how many guesses they took, and perhaps ask if they want to play again.


    • Proposed as answer by AirWaves Monday, July 4, 2011 8:42 PM
    • Marked as answer by litdevModerator Thursday, July 14, 2011 7:33 PM
    Monday, July 4, 2011 6:29 PM
    Moderator

All replies

  • All your code is correct, except for your GOTOs. YOu have the "Goto guess" outside of your condition, so it always goes to the beginning, right after you enter a number, without giving you the hints. Use this code instead:

    guess:

    TextWindow.WriteLine("Please enter a number")

    rnum = Math.GetRandomNumber(100)

    num = TextWindow.ReadNumber()

    If rnum = 0 Then

      rnum = rnum + 1

    EndIf

    If num > 100 Then

      TextWindow.WriteLine("Number not valid, guess again")

      Goto guess

    EndIf

    If num < rnum Then

      TextWindow.WriteLine("Too low, guess again")

      Goto guess

    EndIf

    If num > rnum Then

      TextWindow.WriteLine("Too high, guess again")

      Goto guess

    EndIf   

    If num = rnum Then

      TextWindow.WriteLine("Congrats, you got it right!")

    EndIf

     

    That should fix the problem.


    ~~AirWaves!!~~
    Monday, July 4, 2011 8:11 AM
  • A couple of other points:

    1] We should place the guess: after the random number is set since we don't want the random number to change with each guess (this would be hard!).  But we want the guess: to ask the question again, so we need:

    rnum = Math.GetRandomNumber(100)
    guess:
    TextWindow.WriteLine("Please enter a number")
    num = TextWindow.ReadNumber()

    2] The random number will be between 1 and 100, so no need to check for it being 0.

    3] We should turn the user input to an integer - it is possible an unhelpful user may enter 3.5.  We can use Math.Floor to do this.

    4] We can use If, ElseIf construct to check for mutually exclusive possibilities.

    Here is the whole thing:

    rnum = Math.GetRandomNumber(100)
    guess:
    TextWindow.WriteLine("Please enter a number")
    num = TextWindow.ReadNumber()
    num = Math.Floor(num)

    If num < 1 or num > 100 Then
      TextWindow.WriteLine("Number not valid, guess again")
      Goto guess
    ElseIf num < rnum Then
      TextWindow.WriteLine("Too low, guess again")
      Goto guess
    ElseIf num > rnum Then
      TextWindow.WriteLine("Too high, guess again")
      Goto guess
    Else 'Must be right
      TextWindow.WriteLine("Congrats, you got it right!")
    EndIf  

    You could add a counter to tell the player how many guesses they took, and perhaps ask if they want to play again.


    • Proposed as answer by AirWaves Monday, July 4, 2011 8:42 PM
    • Marked as answer by litdevModerator Thursday, July 14, 2011 7:33 PM
    Monday, July 4, 2011 6:29 PM
    Moderator
  • thanks, that fixed everything. i'll be sure to stick around the forums from now on

    Monday, July 4, 2011 6:54 PM