7.7 Ch 7 Program: Data visualization (Python 3) (1) Prompt the user for a title
ID: 3859516 • Letter: 7
Question
7.7 Ch 7 Program: Data visualization (Python 3)
(1) Prompt the user for a title for data. Output the title. (1 pt)
Ex:
(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)
Ex:
(3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they have finished entering data points. Output the data points. Store the string components of the data points in a list of strings. Store the integer components of the data points in a list of integers. (4 pts)
Ex:
(4) Perform error checking for the data point entries. If any of the following errors occurs, output the appropriate error message and prompt again for a valid data point.
If entry has no comma
Output: Error: No comma in string. (1 pt)
If entry has more than one comma
Output: Error: Too many commas in input. (1 pt)
If entry after the comma is not an integer
Output: Error: Comma not followed by an integer. (2 pts)
Ex:
(5) Output the information in a formatted table. The title is right justified with a minimum field width value of 33. Column 1 has a minimum field width value of 20. Column 2 has a minimum field width value of 23. (3 pts)
Ex:
(6) Output the information as a formatted histogram. Each name is right justified with a minimum field width value of 20. (4 pts)
Ex:
Explanation / Answer
def outputTable(title, header1, header2, strList, intList):
"""
Function that prints the contents in tabular format
"""
# Printing title
print(" %33s " %(title));
# Printing header
print(" %-20s %-10s %-23s " %(header1, "|", header2));
print(" -------------------------------------------- ");
# Printing formatted output
for i in range(0, len(strList)):
print(" %-20s %-10s %-23d " %(strList[i], "|", intList[i]));
print(" ");
def outputHistogram(strList, intList):
"""
Function that prints the contents in tabular format
"""
print(" ");
# Printing names
for i in range(0, len(strList)):
print(" %20s %-5s " %(strList[i], " "), end = "");
# Printing *'s
for j in range(0, intList[i]):
print("*", end="");
print(" ");
def chkCommas(dataPoint):
"""
Function that validates commas error checking
"""
# Checking for non-existence of comma
if ',' not in dataPoint:
print(' Error: No comma in string. ');
return False;
# Checking for more than comma
elif dataPoint.count(",") > 1:
print(' Error: Too many commas in input. ');
return False;
# Checking for integer after comma
else:
# Fetching integer after comma
index = dataPoint.find(",");
name = dataPoint.split(",")[0];
integer = (dataPoint.split(",")[1]).strip();
# Validating integer
if not (integer.isdigit()):
print(' Comma not followed by an integer. ');
return False;
else:
return True;
def main():
"""
Main function
"""
# List's to hold data
strList = [];
intList = [];
# Reading title from user
title = input(' Enter a title for the data: ');
print(" You entered: " + title);
# Reading header1 from user
header1 = input(' Enter the column 1 header: ');
print(" You entered: " + header1);
# Reading header2 from user
header2 = input(' Enter the column 2 header: ');
print(" You entered: " + header2);
# Reading dataPoints
dataPoint = input(" Enter a data point (-1 to stop input): ");
# Reading data points
while dataPoint != "-1":
# Validating commas
commas = chkCommas(dataPoint);
# If there commas are OK
if commas:
# Splitting data point to name and integer
name = dataPoint.split(",")[0];
integer = int((dataPoint.split(",")[1]).strip());
# Printing to console
print(" Data string: " + name);
print(" Data integer: " + str(integer));
# Storing in list
strList.append(name);
intList.append(integer);
# Reading dataPoints
dataPoint = input(" Enter a data point (-1 to stop input): ");
# Printing result in tabular format
outputTable(title, header1, header2, strList, intList);
# Printing result in histogram
outputHistogram(strList, intList);
# Calling main function
main();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.