In: Computer Science
23.2 PROJECT 2 : Data Visualization using LOOPS (python)
This program will display a set of authors and the number of novels written by each author in both a table and a histogram. You will ask the user for all of the information. Using what you learned about incremental development, use the following approach to create your program:
Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored
Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels
Enter a data point (-1 to stop input): Jane Austen, 6 Author: Jane Austen Number of Novel(s): 6
If the error occurs, output the appropriate error message and
prompt again for a valid data point. You can assume that if a comma
is present, then the data point is entered correctly.
Ex:
Enter a data point (-1 to stop input): Ernest Hemingway 9 Error: No comma in string. Enter a data point (-1 to stop input): Ernest Hemingway, 9 Author: Ernest Hemingway Number of Novel(s): 9
Number of Novels Authored Author name | Number of novels -------------------------------------------- Jane Austen | 6 Charles Dickens | 20 Ernest Hemingway | 9 Jack Kerouac | 22 F. Scott Fitzgerald | 8 Mary Shelley | 7 Charlotte Bronte | 5 Mark Twain | 11 Agatha Christie | 73 Ian Flemming | 14 J.K. Rowling | 14 Stephen King | 54 Oscar Wilde | 1
Jane Austen ****** Charles Dickens ******************** Ernest Hemingway ********* Jack Kerouac ********************** F. Scott Fitzgerald ******** Mary Shelley ******* Charlotte Bronte ***** Mark Twain *********** Agatha Christie ************************************************************************* Ian Flemming ************** J.K. Rowling ************** Stephen King ****************************************************** Oscar Wilde *
For this project, you can assume that the user will not enter duplicate author names.
PYTHON CODE:
# getting title from the user
title = input('Enter a title for the data:\n')
print('You entered: %s' % title)
# getting header1
header1 = input('\nEnter the column1 header:\n')
print('You entered: %s' % header1)
# getting header2
header2 = input('\nEnter the column2 header:\n')
print('You entered: %s' % header2)
# list to store the authors and number of novels
authors=[]
novels=[]
# infinite looping
while True:
# getting input from the user
data = input('\nEnter a data point(-1 to stop
input):\n')
# checking for valid input
if not ',' in data and data != '-1':
print('\nOutput: Error.
No comma in string.\n')
continue
# breaking from the loop
if data == '-1':
break
# printing the user input
print(data)
t=data.split(',')
print('Author: %s' % t[0])
print('Number of Novel(s): %s' % t[1])
# adding the details to the list
authors.append(t[0])
novels.append(int(t[1]))
# printing the table headers
print("{0:>33s}".format(title))
print('{0:<20s}|{1:>23s}'.format(header1,header2))
# printing the table
for author,novel in zip(authors,novels):
print('{0:<20s}|{1:>23d}'.format(author,novel))
print('\n')
# printing the histogram
for author,novel in zip(authors,novels):
print('{0:>20s} '.format(author),end='')
for i in range(novel):
print('*',end='')
print()
SCREENSHOT FOR CODING:
SCREENSHOT FOR OUTPUT: