Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your task in this challenge is to write a Python program that reads in a flowchart from a file and plays it to the user.

Your task in this challenge is to write a Python program that reads in a flowchart from a file and plays it to the user. The flowchart file format is simple and can be explained with an example.

Shorts Flowcharts have questions (that end in a question mark) and end states. Questions are answered as either yes or no, and flow to other questions or end states. End states stop the processing of a flowchart. The flowchart's hierarchy is expressed using tabs to denote flow. The first question, Is it cold outside? is aligned with the left margin of the file (i.e., there are no tabs that precede it). Its yes and no paths are indented with a single tab. The next line in the file always corresponds to the yes path. Here, the yes path flows to the question, Will you be outside for long?, while the no path flows to the end state, Shorts. The question, Will you be outside for long? results in two separate end states, Jacket and Sweater, each of which is indented an additional tab (i.e., two tabs). The file can be visually represented as follows:

Is it cold outside?

[tab]Will you be outside for long?

[tab][tab]Jacket

[tab][tab]Sweater

[tab]Shorts

Due to the nature of this challenge, you will not be able to test your program in IDLE. This is because a command line parameter (the flowchart file) must be provided for the program to run properly. Therefore, you will need to run your program from the command line (terminal).

Here's an example of several runs of a correct solution to this challenge (note that the flowchart file is named simple_example.txt):

jgourd@pi$ python flowchart.py

Usage: python flowchart.py

In the previous example, no flowchart file was specified; therefore, a usage statement was displayed. The remaining examples go through all possible responses to the questions in the flowchart (with input highlighted in red): jgourd@pi$ python flowchart.py simple_example.txt

Is it cold outside? (y/n): n

You should go with Shorts

jgourd@pi$ python flowchart.py simple_example.txt

Is it cold outside? (y/n): y

Will you be outside for long? (y/n): y

You should go with Jacket

gourd@pi$ python flowchart.py simple_example.txt

Is it cold outside? (y/n): y

Will you be outside for long? (y/n): n

You should go with Sweater

A correct program will allow the user to answer yes (y) or no (n) to any question, subsequently flowing to the appropriate path in the flowchart. Once an end state is reached, your program should terminate. You may safely assume that I will not answer a question with something invalid (i.e., not y or n) when testing. To help clarify, here are some specifics and/or constraints:

(1) Your output should be exactly like the sample runs shown above (of course, actual input values and the resulting path flow will vary depending on the provided inputs);

(2) You must include a meaningful header, use good coding style, use meaningful variable names, and comment your source code where appropriate;

(3) You must use the provided source code template; and

(4) You must submit your source code as a single .py file

This is the provided template:

import sys

# processes/"plays" the flowchart # *****this is the only part of the code that you should modify/add to***** def play(entities): # pass is a reserved word that simply means "do nothing" # it is used when Python expects code in a block (e.g., a function) but none is provided at the time # it usually means that code will be provided at a later time pass ######## # MAIN # ######## # make sure that a flowchart file is specified at the command line if (len(sys.argv) < 2): print "Usage: python {} ".format(sys.argv[0]) exit(0)

# open the flowchart file, read it into a list, and close it file = open(sys.argv[1], "r") entities = [] for entity in file: entities.append(entity.rstrip()) file.close()

# "play" the flowchart play(entities)

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

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions