Answered step by step
Verified Expert Solution
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
# menudriven commandline program. This program will ask the user to
# select a choice from a menu using a number eg Select through
# where choice 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
# choice intinputfEnter a choice between and numChoices:
# while choice and choicenumChoices:
# choice intinputfEnter a choice between and numChoices:
#
# The problem is if a person enters a nondigit 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 to numChoices. The range
# command described in Section 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 so the acceptable values are
# 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started