Find the best solutions to your questions at Westonci.ca, the premier Q&A platform with a community of knowledgeable experts. Ask your questions and receive accurate answers from professionals with extensive experience in various fields on our platform. Join our platform to connect with experts ready to provide precise answers to your questions in different areas.

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