Guess the Number
This project will be the classic “guess the number” game. This is where a player has a set number of tries to guess a number between a given range.
- Generate a random number in a set range (should be known to user)
- Get user input of guess
- Compare and output if correct or not
- If not correct, tell user if too high or too low and decrement number of lives.
- If correct, congratulate the user and exit
- If number of lives is below 1, tell the user and exit
- Extra: handle possible errors
- Define two variables; these will contain the start and end range
- Choose a int type if your language is statically typed
- Define as a constant if possible in your language
- Suggested names:
RANGE_START
,RANGE_END
- Example value:
RANGE_START=1
,RANGE_END=20
- Define a variable to store the remaining lives (default value will be starting lives).
- Choose a int type if your language is statically typed
- Suggested name:
lives
- Example value:
5
- Define a variable which will store the number to guess
- Choose a int type if your language is statically typed
- Suggested name:
number
- Set
number
to a random number in the rangeRANGE_START
toRANGE_END
- Create a infinite while loop
- Define a variable to store the guess
- Suggested name:
guess_input
- Choose a string type if your language is statically typed
- Suggested name:
- Output message prompting user to guess number e.g. “Guess the number: "
- Get input from user and store in
guess_input
- Define a variable to store the guess as a number
- Choose a int type if your language is statically typed
- Suggested name:
guess
- Convert
guess_input
to int and store inguess
- Extra: handle possible errors for user input
- Output error to user
- Restart to start of loop if error occurred
- Extra: handle possible errors for user input
- Check if number is equal to
number
- Output message saying it was correct
- Extra: Output lives remaining
- Break out of loop
- Check if number is greater than
number
- Output that guess was too high
- Subtract 1 from
lives
- Check if number is less than
number
- Output that guess was too low
- Subtract 1 from
lives
- Check if lives is less than 1
- Output that the user has no lives left
- Output the actual number
- Break out of loop
- Loop!
- Define a variable to store the guess
- Program finished
Once completed test your code with some data, if it does not work try having a look at a working example solution and compare your code.
Please be aware example solutions may not look the same as your code, as there are many different ways of implementing the solution(s).
Guess The Number [5 lives]: 10
Incorrect, 10 was too low, Try again
Guess The Number [4 lives]: 20
Incorrect, 20 was too high, Try again
Guess The Number [3 lives]: 17
Incorrect, 17 was too high, Try again
Guess The Number [2 lives]: 16
Incorrect, 16 was too high, Try again
Guess The Number [1 lives]: 15
Incorrect, 15 was too high, Try again
Guess The Number [0 lives]: 17
Ran out of lives, you lost
Guess The Number [5 lives]: 10
Incorrect, 10 was too high, Try again
Guess The Number [4 lives]: 5
Incorrect, 5 was too low, Try again
Guess The Number [3 lives]: 6
Incorrect, 6 was too low, Try again
Guess The Number [2 lives]: 7
Incorrect, 7 was too low, Try again
Guess The Number [1 lives]: 8
8 was correct!! you won with 1 lives remaining
Guess The Number [5 lives]: 50
ERROR: OutOfRange: Type a number between 1 and 20
Guess The Number [5 lives]:
Guess The Number [5 lives]: test
Incorrect type, must be a whole number between 1 and 20
Guess The Number [5 lives]:
- Implement the steps indicated with “Extra”
- Make user pick starting number of lives/difficulty level. At start of game.
- Save High Score/Streak to a file.
- Take a look at the example solutions, you could learn how to write it in a different language