Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CS0011 Introduction to Computing for Scientists Project 1 - Lab 2

 

Part 1: Concise output

In this section, we will modify the program in Lab 1 to produce more concise output. In CSV (comma-separated values) format, values are separated by commas (hence the name). Modify your program to output the results in CSV format. You should have one line with header labels and one line with the corresponding values.

Here is the output from a run ofthe desired program:

Fuel,Velocity,Duration,Fuel cost,Losses,Value lost                                      10000,953.1017980432493,910.768983268842,10000000,109292277.99226104,119292277.99226104

CSV formatted data is meant to be used by programs on the computer (e.g., you can input CSV formatted data into Excel as a spreadsheet), it is not meant to be read by users. As such, there is no need to format (e.g., limit the number of displayed decimal places) or align the values before printing them. There is also no need to include units (e.g., m/s, USD). We will be learning how to create .csv files formatted in this manner soon, but for now, we will just print out the CSV formatted data to the terminal.

Part 2: User-friendly output

Since CSV is not very user-friendly, we will now write some additional code to output easily human-readable tables of results (be sure not to delete your code that outputs in CSV! We will still be using that in later parts of the project). For more user-friendly output, you will use Python’s print and format, as discussed in lecture.

For each value (Fuel, Velocity, Duration, etc.), you will need to figure out how many characters will be needed to display each value in order to determine how wide each column of our table needs to be. In the following example, representing the total value lost (119,292,278) requires 11 characters (nine digits and two commas). You will need to experiment with different amounts of fuel to determine how large each value can get and set the appropriate width value in format.

Do not forget to account for units (e.g., m/s, USD) in determining your column sizes! Here is the output from a run ofthe desired program:

 

Part 3: Choosing between the two output formats

Modify your program so that it asks the user whether they would like to output in human -readable or CSV format. You should present the user with both of these options and allow them to select one or the other, the print out in the corresponding format.

Here is the output from several runs ofthe desired program:

 

1.    If user enters 10,000 for fuel and selects 1:

Enter value for fuel in kilograms [0-100,000kg]: 10000

How would you like to format the output?

1: CSV

2: Human-readable

Enter 1 or 2: 1

Fuel,Velocity,Duration,Fuel cost,Losses,Value lost                                      10000,953.1017980432493,910.768983268842,10000000,109292277.99226104,119292277.99226104

 

2.    If user enters 10,000 for fuel and selects 2:

Enter value for fuel in kilograms [0-100,000kg]: 10000

How would you like to format the output?

1: CSV

2: Human-readable

Enter 1 or 2: 2