Discover a world of knowledge and get your questions answered at IDNLearn.com. Get the information you need from our community of experts who provide accurate and thorough answers to all your questions.

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.

Sagot :

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")

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")

Thank you for being part of this discussion. Keep exploring, asking questions, and sharing your insights with the community. Together, we can find the best solutions. IDNLearn.com is committed to providing accurate answers. Thanks for stopping by, and see you next time for more solutions.