Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

( Longer Problem ) Later the course, you will be asked to write a # menu - driven command - line program. This program will

(Longer Problem) Later the course, you will be asked to write a
# menu-driven command-line program. This program will ask the user to
# select a choice from a menu using a number (e.g. Select 1 through 5,
# where choice 5 exits the program). A while loop is the logical structure
# for handling this because you can repeatedly execute all the choices the
# user selects until the user chooses to exit. For example, you could try
#
# numChoices =5
# choice = int(input(f"Enter a choice between 1 and {numChoices}:"))
# while (choice<1 and choice>numChoices):
# choice = int(input(f"Enter a choice between 1 and {numChoices}:"))
#
# The problem is if a person enters a non-digit and you try to cast it
# into an int to check if it has an unacceptable value in your while loop,
# you will get a ValueError exception. So how can we build a while loop
# that checks if the user entered an allowed value? One trick is to avoid
# casting the input into an integer at all. We can do this by building a
# list of the string equivalents of the allowed choices and then use the
# while loop to check if that choice is not in that list.
# * Imagine you are told the menu should have numChoices. Build a list
# that contains the string equivalents of 1 to numChoices. The range
# command (described in Section 7.5.1 of your textbook) will be useful
# here. Do this before you request a choice.
# * Once you have built your list of acceptable answers, modify the
# requests for a choice from the user to not cast to int and modify your
# while loop to check the users input against the list of acceptable
# choices. Set numChoices =5, so the acceptable values are 1,2,3,4,5
# and test that you can ask a person for a choice and the code will keep
# asking for an acceptable choice (using a while loop) until the person
# enters an acceptable choice.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions