Problem 54
Hi-Lo with Limited Tries
Write a program that picks a random number from 1-100. The user keeps guessing as long as their guess is wrong, and they’ve guessed less than 7 times.
- If their guess is higher than the number, say “Too high.”
- If their guess is lower than the number, say “Too low.”
- When they get it right, the game stops.
- if they hit seven guesses, the game stops even if they never got it right.
This means your while loop will have a compound condition using &&.
I'm thinking of a number between 1-100. You have 7 guesses. First guess: 50 Sorry, you are too low. Guess # 2: 75 Sorry, you are too low. Guess # 3: 87 Sorry, that guess is too high. Guess # 4: 82 Sorry, you are too low. Guess # 5: 84 You guessed it! What are the odds?!?
I'm thinking of a number between 1-100. You have 7 guesses. First guess: 1 Sorry, you are too low. Guess # 2: 2 Sorry, you are too low. Guess # 3: -8 Sorry, you are too low. Guess # 4: 0 Sorry, you are too low. Guess # 5: 7 Sorry, you are too low. Guess # 6: 612 Sorry, that guess is too high. Guess # 7: -523 Sorry, you didn't guess it in 7 tries. You lose.
◄ 53: Number-Guessing with a Counter 55: Adding Values in a Loop ►
Adapted from ProgrammingByDoing.com
©2013 Graham Mitchell
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.