Sheerpower Logo
R.1  A simple Guessing Game
This guessing game uses the following Sheerpower features:
  • Random number Generation
  • Conditions
  • printed output
The RND() funtion generates a random number between 1 and whatever number you pass to it. For example to generate a number between 1 and 100 and storing the number into the variable target and printing its value:
target = rnd(100) print target
The code below generates a random number between 1 and 5. Put into X your guess and then click on RUN. A new random number will be generated each time.
print 'A simple guessing game' print highnum = 5 target = rnd(highnum) print 'Shhh.. the target was:'; target print // Uncomment below if you downloaded Sheerpower // input 'Your guess': x if x = 0 then print "Put into X your guess and click on RUN" stop end if if x > target then print "too high" elseif x < target then print "too low" else print "You got it" end if

Code Explanation:

1. Introduction:

This line prints the message "A simple guessing game" to the screen, introducing the program to the user.

2. Blank Line:

Prints an empty line for better readability, separating the introduction from the rest of the output.

3. Set Upper Bound:

The variable highnum is set to 5, which defines the upper bound of the range within which the random target number will be generated. The target number will be between 1 and 5.

4. Generate Random Number:

The rnd(highnum) function generates a random integer between 1 and highnum (inclusive) and stores it in the variable target.

5. Reveal Target:

This line prints the randomly generated target number to the screen. This is typically used for debugging or demonstration purposes, as the target is meant to be hidden from the player during an actual game.

6. Blank Line:

Prints another empty line for spacing and readability.

7. Commented Input Statement:

These lines are comments that suggest how to add user input functionality. If the user has downloaded Sheerpower, they can uncomment these lines to allow the player to input their guess, which will be stored in the variable x.

8. Check for Missing Guess:

If the variable x is still 0 (meaning the user did not make a guess), the program prompts the user to input their guess into x and rerun the program. The program then stops to allow the user to make this adjustment.

9. Guess Too High:

If the user's guess (x) is greater than the target number, the program prints "too high" to inform the user that their guess was higher than the target.

10. Guess Too Low:

If the user's guess (x) is less than the target number, the program prints "too low" to inform the user that their guess was lower than the target.

11. Correct Guess:

If neither of the above conditions is true (i.e., x is equal to the target), the program prints "You got it" to congratulate the user on guessing the correct number.

Summary:

The program is a simple number-guessing game where the user tries to guess a randomly generated number within a range. It generates a target number, checks the user's guess, and provides feedback if the guess was too high, too low, or correct. The interactive input functionality is provided as a comment that can be uncommented for user input support.

Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen