Search⌘ K

Exercise: Loop Constructs

Explore Bash loop constructs by developing a number guessing game where you implement control flow, handle user input, and apply a loop for repeated attempts. This lesson teaches how to use loops efficiently to create interactive scripts with random number generation and conditional responses.

We'll cover the following...

Exercise

Write a game called “More or Less.” The first participant chooses any number from 1 to 100. The second participant tries to guess it in seven tries.

Our script chooses a number. The user enters their guess. The script answers if the guess is more or less than the chosen number. The user then tries to guess the number six more times.

# Write your code here.

Solution

The player has seven tries to guess a number. The same algorithm handles each try. Therefore, we can apply a loop with seven iterations.

Here is the algorithm for processing a ...