Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Input Validation The idea of input validation means that we want to be sure that the input the user provides is actually valid for

imageimageimageimageimageimageimageimage

Input Validation The idea of input validation means that we want to be sure that the input the user provides is actually valid for use in our program. If it is, we know that we can use it safely in our computations. If it isn't, we cannot use it and will need to ask the user to re-enter the input. In general, valid input is user-supplied data that is of the correct type and that is within an acceptable range of values. For Part 2 of this project, you will implement a more robust version of the get_int function you implemented in Project 1. You will also implement an additional function named get_ints. This new function will require users to input several integers and return them in a list. Both of these functions should be implemented in a csc111_input module. The interface of your functions should look like this: get_int (prompt, low, high) returns an integer within low and high get_ints (number, prompt, low, high) returns a list of number integers within low and high Figure 2 below shows a flowchart for the implementation of the get_int function. Your first task will be to implement this function by following the flowchart. Then, you will need to use this function as part of your get_ints function. Start Display Prompt, Collect Input, and Store it to a Variable Convert Value to an int Value >= lower bound True Value Core Simulation As described in Part 1, the core simulation is the hardest working part of the program. You implemented a basic level of this functionality in your calc_boarded function. For Part 2, you will expand on the run_sim function to create a more robust and flexible simulation. We will describe additional implementation details in the Implementation section of this document. Your run_sim function header should be modified to look like this: run_sim (iterations, board_rates, exit_rates) The design of this function for Part 2 incorporates the functionality described in Part 1 along with a few additional features: Passengers no longer must exit the bus at the end of the number of stops. Consider the example bus line in Figure 1, which begins to loop back to the initial starting location. Instead of stopping at the last stop. the simulation will run through N iterations, stopping at each stop. Once the bus reaches the last stop, it will return to the starting location with a new set of passengers currently riding the bus. We no longer assume passengers were boarding and departing the bus line at an equal rate across stops. Instead, we will ask the user to provide the boarding rates and exit rates for each stop as two lists. board_rates is a list of boarding rates at each stop, and exit_rates is a list of exit rates at each stop. Note as well, this run_sim function does not have a starting_passengers parameter. You can instead assume at iteration 0 there are 0 starting passengers. When the simulation loops back to the starting location however, this does not reset to 0. Here is an example output generated by run_sim (10, [8, 10, 8], [5, 10, 10]): 1 0 8 0 2 3 4 5 LO 6 8 10 00 10 8 5 11 10 11 10 7 9 5 8 12 10 9 12 10 10 10 8 5 Notice that 10 means run_sim will simulate the bus line 10 stops. [8, 10, 8] means the list of the boarding rates at each bus stop, and [5, 10, 10] means the list of the exit rates at each bus stop. This implies there are in total 3 bus stops in the simulation. Even though there are 3 bus stops on this example bus line, the simulation is able to loop back through and repeat the process over and over again until the number of iterations has been exhausted. Overall Program 00 00 10 8 00 8 10 8 00 00 8 10 8 00 For context, we again describe, from the user's perspective, how the final program will work. Note that this description is expanded from the one given in Part 1. The simulation should prompt the user to enter the number of stops along the bus line's route. The simulation should then prompt the user to enter the board rates for each stop. The simulation should then prompt the user to enter the exit rates for each stop. o Note the board and exit rate lists must be the same size. Finally, the simulation should execute and display a tabular output similar to Part 1. The only difference is that the board and exit rates should reflect the modified inputs for Part 2 and should also include headers for each column. Your program will use your updated csc111_input module. Specifically, you will update the following two functions: get_int (prompt, low, high) - Uses the string stored in the prompt to ask the user for input. If the input is valid (between low and high), the int value is returned. If not, the function will display an appropriate error message to the user and prompt for input again until the user enters a valid integer value. If the user enters anything that cannot be converted to an int, a ValueError will be generated. get_ints (number, prompt, low, high) - Utilizes the get_int function displayed above to collect a list of valid integers one by one. You will need to pass the prompt, low, and high parameters to the get_int function. The parameter number represents the total number of integers the user wants to collect. The function will return a list of collected valid inputs. You need to update your cscll1_input.py created in Project 1 Part 1 with the two functions above. Of course, to use these functions, you will need to import your csc111_input module into your main program script. Main Program Implementation You will implement your simulation program in a file named p2p2_sim.py. Your program must implement and use the following functions: calc_boarded (boarding, departing, current) - This function should be an exact copy of your Project 2 Part 1 implementation. run_sim (iterations, board_rates, exit_rates) - Executes the simulation, calculating and outputting the stop by stop volumes of passengers boarding, departing, and riding the bus line. The arguments represent the total number of iterations the simulation should run, a list of boarding rates at each stop, and a list of exit rates at each stop. The simulation stops after outputting N stops, where N is equal to the number of iterations. The total width of the tabular output from this function shall not exceed a width of 75 character columns on a terminal/command prompt display. print_welcome () - This function neatly prints a welcome message. Your welcome message must include your name, unity ID, and lab section number. The output generated by this function will not exceed a width of 75 character columns on a terminal/command prompt display. The length of the output will not exceed a length of 20 lines. print_goodbye () - This function neatly prints a "goodbye" message to the user. The output generated by this function will not exceed a width of 75 character columns on a terminal/command prompt display. The length of the output will not exceed a length of 5 lines. main () This function will contain your main program loop similar to the output below. The execution of this function must be controlled by the if _name___ == '__main__': statement provided in the course template. You need to call get_int and get_ints functions to collect the number of stops, boarding rate for each stop, exit rate for each stop and the number of iterations to simulate in the main function. Please assume the valid range of inputs for the number of stops is 3-8 (both inclusive), for the board_rate and exit_rate is 0-50 (both inclusive), and for the number of iterations is 10-20 (both inclusive). For reference purposes, a sample run output is provided below, where the text in red is user input. Note that the exact numbers will vary based on the inputs you select. However, this output can serve as a guide for formatting your own program's output. Welcome to my Bus Line Simulator. This program simulates the passenger flow on a single bus route. The simulation will continue to run for a specified number of iterations, showing the number of passengers currently riding, boarding, and departing the bus at each stop. Enter the number of stops on the bus line's route: 3 Enter the boarding rate for each stop (one at a time): 8 Enter the boarding rate for each stop (one at a time): 10 Enter the boarding rate for each stop (one at a time): 8 Enter the exit rate for each stop (one at a time): 5 Enter the exit rate for each stop (one at a time): 10 Enter the exit rate for each stop (one at a time): 10 Enter the number of iterations to simulate: 10 Iterations Current 1 1.000 WNP 2 3 5 6 7 8 9 10 00 O 8 10 8 11 11 9 12 12 10 PRR Boarding Departing 8 10 00 00 8 8 10 00 00 00 00 10 8 10 5 OPPO 00 10 10 5 10 GPP 00 10 5 Thank you for using my bus line simulation program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Certainly Heres an implementation of ... 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

Managerial Accounting

Authors: Ray Garrison, Theresa Libby, Alan Webb

9th canadian edition

1259269477, 978-1259269479, 978-1259024900

More Books

Students also viewed these Programming questions

Question

For the following exercises, graph the inequality. y -log(x) y et

Answered: 1 week ago

Question

In Exercises find dy/dx by implicit differentiation. xy - y = x

Answered: 1 week ago