210k views
1 vote
Write a program, which will display the menu with 5 items. It will ask the use to give their choice. It will trap all possible errors and then print the result when correct input is provided before exiting the program.

1 Answer

5 votes

Answer:

The program in Python is as follows:

print("Welcome to sorting program")

print("1. Title")

print("2. Rank")

print("3. Date")

print("4. Start")

print("5. Likes")

while(True):

choice = input("Enter a choice between 1 and 5 only: ")

if choice.isdigit() == True:

if(int(choice)>=1 and int(choice)<=5):

print("You entered valid choice", choice)

break;

else:

print("You have not entered a number between 1 and 5. Try again")

else:

print("You entered an invalid choice")

Step-by-step explanation:

This prints the header

print("Welcome to sorting program")

The next 5 lines print the instructions

print("1. Title")

print("2. Rank")

print("3. Date")

print("4. Start")

print("5. Likes")

The loop is repeated until it is forcefully exited

while(True):

This prompts the user to enter a choice

choice = input("Enter a choice between 1 and 5 only: ")

This checks if the choice is a digit

if choice.isdigit() == True:

If yes, this checks if the choice is between 1 and 5 (inclusive)

if(int(choice)>=1 and int(choice)<=5):

If yes, this prints valid choice and the choice entered

print("You entered valid choice", choice)

The loop is exited

break;

If the choice is out of range

else:

This prints the error and tells the user to try again

print("You have not entered a number between 1 and 5. Try again")

If choice is not a digit

else:

This prints invalid choice

print("You entered an invalid choice")

User Brandonhilkert
by
8.2k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.