Question

*In Python please***** This program will display some statistics, a table and a histogram of a...

*In Python please*****

This program will display some statistics, a table and a histogram of a set of cities and the population of each city. You will ask the user for all of the information. Using what you learned about incremental development, consider the following approach to create your program:

  1. Prompt the user for information about the table. First, ask for the title of this data set by prompting the user for a title for data, and then output the title as verification to the user.

    Ex:
Enter a title for the data:
Populations of U.S. Cities
You entered: Populations of U.S. Cities
  1. The table will have two columns; one for the cities and one for the populations. Prompt the user for the headers of the two columns of the table, then output the column headers as verification to the user. Be sure to prompt the user for this data, as opposed to hard coding what you see in the example.

    Ex:
Enter the column 1 header:
Name of City
You entered: Name of City

Enter the column 2 header:
Total Population
You entered: Total Population
  1. Prompt the user for data points. Data points must be in this format: string, int, representing the city and the population of that city. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter done when they have finished entering data points. Output the data points as verification, as shown in the example. HInt: Think about how to store the data points. Maybe a list of strings and a separate list of integers? Or one list with a distinct pattern, such as a string, followed by an integer? Think about your implementation options while designing this step.

    Ex:
Enter a data point ('done' to stop input):
New York City, 7623000
City: New York City
Population: 7623000
  1. Perform error checking for the data point entries as follows:
  • If entry has no comma and is not the string 'done'
  • Output: Error: No comma in string.

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 ('done' to stop input):
Seattle 724745
Error: No comma in string.

Enter a data point ('done' to stop input):
Seattle , 724745
City: Seattle 
Population: 724745
  1. Output the minimum, maximum, and mean of the population data. The words Minimum:, Maximum:, and Mean: are right justified with a minimum field width value of 8. The mean value should have a precision of 3.

    Ex:
Population Statistics
Minimum: 48844
Maximum: 8623000
   Mean: 3417614.666
  1. Output the information in a formatted table. The title is right justified with a minimum field width value of 27. Column 1 has a minimum field width value of 16. Column 2 has a minimum field width value of 17.

    Ex:
    Population of US Cities
Name of City    | Total Population
----------------------------------
New York City   |          7623000
Seattle         |           724745
Philadelphia    |          1581000
Minneapolis     |           422331
Cleveland       |           385525
Los Angeles     |          4000000
  1. Output the information as a formatted histogram. Prompt the user for the character that should be used in the histogram. Each city name is right justified with a minimum field width value of 17. Print one user-specified character for every 100,000 people. Make sure to use the user-specified character, as opposed to hard coding the asterisk as shown in the example below.

    Ex:
   New York City ****************************************************************************
         Seattle *******
    Philadelphia ***************
     Minneapolis ****
       Cleveland ***
     Los Angeles ****************************************

For this project, you can assume that the user will not enter duplicate city names.

This project has one hidden test case, that will not give you any type of feedback. If you are not able to pass the hidden test case, make sure that your program is set up to use any input. It is likely you are hard-coding some part of your program if you are not able to pass the hidden test case. Do not get frustrated. Instead, be sure to ask questions in class, in the helproom, or using Piazza.

Here's an example of the full program:

Enter a title for the data:
Populations of U.S. Cities
You entered: Populations of U.S. Cities

Enter the column 1 header:
Name of City
You entered: Name of City

Enter the column 2 header:
Total Population
You entered: Total Population

Enter a data point ('done' to stop input):
New York City, 7623000
City: New York City
Population: 7623000

Enter a data point ('done' to stop input):
Seattle , 724745
City: Seattle
Population: 724745

Enter a data point ('done' to stop input):
Philadelphia 1581000
Error: No comma in string.

Enter a data point ('done' to stop input):
Philadelphia, 1581000
City: Philadelphia
Population: 1581000

Enter a data point ('done' to stop input):
Minneapolis, 382578
City: Minneapolis
Population: 382578

Enter a data point ('done' to stop input):
Cleveland, 396698
City: Cleveland
Population: 396698

Enter a data point ('done' to stop input):
Los Angeles, 3793000
City: Los Angeles
Population: 3793000

Enter a data point ('done' to stop input):
done

Enter the character for the histogram: *

Population Statistics
Minimum: 382578
Maximum: 7623000
   Mean: 2416836.833


    Population of US Cities
Name of City    | Total Population
----------------------------------
New York City   |          7623000
Seattle         |           724745
Philadelphia    |          1581000
Minneapolis     |           382578
Cleveland       |           396698
Los Angeles     |          3793000


   New York City ****************************************************************************
         Seattle *******
    Philadelphia ***************
     Minneapolis ***
       Cleveland ***
     Los Angeles *************************************
0 0
Add a comment Improve this question Transcribed image text
Answer #1

We can decompose the problem to eight functions -

  1. main() - the main function which calls other functions
  2. get_title() - gets the title from the user
  3. get_header() - gets the column headers from the user and returns as a list
  4. get_data() - gets all the data points from the user and does the necessary validations. We can use separate lists for storing cities and populations as this helps in easier calculation of statistics.
  5. get_character() - gets the character used for displaying the histogram from the user.
  6. print_stats(populations) - prints the minimum, maximum and mean
  7. print_table(title, headers, data) - prints the data with title and headers in a table format
  8. print_histogram(data, c) - prints the histogram of the data.

I have shared the code below:

# main function
def main():
   title = get_title()
   headers = get_header()
   data = get_data()
   c = get_character()
   print_stats(data[1])
   print_table(title, headers, data)
   print_histogram(data, c)


# returns title
def get_title():
   title = input("Enter a title for the data:\n")
   print("You entered:", title)
   print()
   return title

# returns all the header columns as a list
def get_header():
   col1 = input("Enter the column 1 header:\n")
   print("You entered:", col1)
   print()
   col2 = input("Enter the column 2 header:\n")
   print("You entered:", col2)
   print()
   return [col1, col2]

# returns the data as a list
def get_data():
   cities = []
   populations = []
   while True:
       line = input("Enter a data point ('done' to stop input):\n")
       if (line.lower() == "done"):
           break
       # validate
       if "," not in line:
           print("Error: No comma in string.")
           print()
           continue
       city, population = line.strip().split(",")
       # add to list
       city = city.strip()
       cities.append(city)
       population = int(population.strip())
       populations.append(population)
       print("City:", city)
       print("Population:", population)
       print()
   return [cities, populations]

# returns the character used for histogram
def get_character():
   print()
   c = input("Enter the character for the histogram: ")
   print()
   return c

# prints the statistics
def print_stats(populations):
   print("Population Statistics")
   print("Minimum:".rjust(8), min(populations))
   print("Maximum:".rjust(8), max(populations))
   print("Mean:".rjust(8), round(sum(populations) / len(populations), 3))
   print()


# prints the data in table format
def print_table(title, headers, data):
   print(title.rjust(27))
   print(headers[0].rjust(16) + "|" + headers[1].rjust(17))
   print("----------------------------------")
   cities = data[0]
   populations = data[1]
   # print data
   for i in range(len(cities)):
       print(cities[i].rjust(16) + "|" + str(populations[i]).rjust(17))
   print()


# displays the histogram of the data using the passed character
def print_histogram(data, c):
   cities = data[0]
   populations = data[1]
   for i in range(len(cities)):
       # the character is multiplied by a factor of population / 100000
       print(cities[i].rjust(16), c * int(round(populations[i]/100000)))

main()

Code Screenshot:

Code Execution:

Add a comment
Know the answer?
Add Answer to:
*In Python please***** This program will display some statistics, a table and a histogram of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • computer science . the programming language is python

    11.9 LAB*: Program: Data visualization(1) Prompt the user for a title for data. Output the title. (1 pt)Ex:Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)Ex:Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels(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...

  • Program: Data visualization

    5.10 LAB*: Program: Data visualization(1) Prompt the user for a title for data. Output the title. (1 pt)Ex:Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)Ex:Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels(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...

  • Need help with this homework, and follow the bolded text required 7.10 LAB: Data Visualization (1) Write a function, get_data_headers(), to prompt the user for a title, and column headers for a table....

    Need help with this homework, and follow the bolded text required 7.10 LAB: Data Visualization (1) Write a function, get_data_headers(), to prompt the user for a title, and column headers for a table. Return a list of three strings, and print the title, and column headers. (2 pt) Ex: Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored Ex: Enter the column 1 header: Author name You entered: Author name Enter the column...

  • In this assignment you are going to handle some basic input operations including validation and manipulation,...

    In this assignment you are going to handle some basic input operations including validation and manipulation, and then some output operations to take some data and format it in a way that's presentable (i.e. readable to human eyes). Functions that you will need to use: getline(istream&, string&) This function allows you to get input for strings, including spaces. It reads characters up to a newline character (for user input, this would be when the "enter" key is pressed). The first...

  • Python 3.1 - 3.5 Certain machine parts are assigned serial numbers. Valid serial numbers are of...

    Python 3.1 - 3.5 Certain machine parts are assigned serial numbers. Valid serial numbers are of the form: SN/nnnn-nnn where ‘n’ represents a digit. Write a function valid_sn that takes a string as a parameter and returns True if the serial number is valid and False otherwise. For example, the input "SN/5467-231" returns True "SN5467-231" returns False "SN/5467-2231" returns False Using this function, write and test another function valid-sn-file that takes three file handles as input parameters. The first handle...

  • PYTHON Text file Seriesworld attached below and it is a list of teams that won from...

    PYTHON Text file Seriesworld attached below and it is a list of teams that won from 1903-2018. First time mentioned won in 1903 and last team mentioned won in 2018. World series wasn’t played in 1904 and 1994 and the file has entries that shows it. Write a python program which will read this file and create 2 dictionaries. The first key of the dictionary should show name of the teams and each keys value that is associated is the...

  • The average number of minutes Americans commute to work is 27.7 minutes (Sterling's Best Places, April...

    The average number of minutes Americans commute to work is 27.7 minutes (Sterling's Best Places, April 13, 2012). The average commute time in minutes for 48 cities are as follows: Click on the datafile logo to reference the data. DATA file Albuquerque Atlanta Austin Baltimore Boston Charlotte Chicago Cincinnati Cleveland Columbus Dallas Denver Detroit El Paso Fresno Indianapolis 23.3 28.3 24.6 32.1 31.7 25.8 38.1 24.9 26.8 23.4 28.5 28.1 29.3 24.4 23.0 24.8 Jacksonville Kansas City Las Vegas Little...

  • **C programming Language 3) Prompt the user for data points. Data points must be in this...

    **C programming Language 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 an array of strings. Store the integer components of the data points in an array of integers....

  • 6.6 Warm up: Parsing strings (Python 3) (1) Prompt the user for a string that contains...

    6.6 Warm up: Parsing strings (Python 3) (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two...

  • Write the program in C The following statistics for cities of the Vege country are stored...

    Write the program in C The following statistics for cities of the Vege country are stored in a file: Name: a string of characters less than 15 characters long. Population: an integer value. Area: square miles of type double Write a program that: asks and gets the name of the input file from the user. Read the contents of the file into an array length 10 of structures -ask the user for the name of a city (less than 15...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT