305,049 views
28 votes
28 votes
Write an algorithm that ask the user for a number between 1 and 3 until the answer fits

User Manish Punia
by
3.1k points

1 Answer

4 votes
4 votes

One way to write an algorithm that asks the user for a number between 1 and 3 until the answer fits the specified criteria is as follows:

  1. Begin the algorithm by asking the user to input a number.
  2. Check if the number the user entered is between 1 and 3 (inclusive). If the number is between 1 and 3, then the algorithm can stop and print a message indicating that the user has entered a valid number. If the number is not between 1 and 3, then the algorithm should print an error message and ask the user to input another number.
  3. Repeat step 2 until the user enters a number that is between 1 and 3.
  4. Here is an example of how this algorithm could be implemented in a programming language like Python:

# Begin the algorithm by asking the user to input a number.

number = input("Please enter a number between 1 and 3: ")

# Check if the number the user entered is between 1 and 3.

while number < 1 or number > 3:

# If the number is not between 1 and 3, then print an error message and ask the user to input another number.

print("Error: Invalid number. Please try again.")

number = input("Please enter a number between 1 and 3: ")

# If the number is between 1 and 3, then print a message indicating that the user has entered a valid number.

print("You have entered a valid number!")

User MarcinKonowalczyk
by
3.3k points