代做CS152 Lab Exercise 3: Modular Design and Lists帮做Java编程

CS152

Lab Exercise 3: Modular Design and Lists

The goal of this week's lab/project pair is to learn how to organize our Python programs in such a way that the functions inside of one Python file can be used by other Python programs. The general term for this is code reuse and it is a central idea in software engineering. After all, what’s the benefit of spending a lot of time working on a program if it can only be used once and only to solve one specific problem. But in order to achieve efficient code reuse, we will need to learn how to structure our programs in a particular way. This organizational model is commonly   referred to as modular design and means that we will build more complex programs by reusing modules (collections of functions) also known as Python programs.

This lab will prepare you for Project 3 by introducing more details on lists, uses of the main function, more file processing techniques, and command-line arguments.

So, let’s get started!

Lab Tasks (L1-L7)

L1. Set up your workspace

Make a folder and name it Lab03. Open VS Code and then open this folder from the File menu. Create a new file, list_practice.py.

L2. Working with lists

Lists are an important data structure in Python. A list is an ordered sequence of values. In Python the values of a list can be of any type, including other lists, and the list does not have to all be of the same type (unlike in other programming languages--we’re looking at you Java!). But for this assignment, we'll mostly be dealing with lists of numbers.

Put the following assignment into your list_practice.py file. It assigns to the variable numbers a new list with five values:

numbers = [5, 3, 6, 1, 2]

In Python a list is indicated by square brackets, and the elements of the list are separated by commas.

We can make an empty list like this:

my_empty_list = []


To access one element of a list, we use index notation with square brackets like this:

first_number = numbers[0]

Note: In Python we start counting indices at zero.

Add two more lines to your file to print out the 5 and the 1 (the first element in the list and the fourth element in the list--be sure to check how you count!).

To add items to the end of a list, use the append(item to append to end of list) method.

To call a method on a list we combine the name of the list, the “dot” operator, and the method name as follows:

numbers.append(7)

print( numbers )

You should now see a 7 at the end of the list.

Just like with accessing list elements, we can replace elements in a list using bracket notation. For example, the following changes the first element of the list to a 4 and then prints the

updated list:

numbers[0] = 4

Modify the value of two other locations in the list in your Python file, then print out the list and make sure it did what you think it should have.

L3Building lists

So far, all of our programs have processed data while we read it from a file (Ok, technically we   created a list using the split method, but we didn’t keep that list around for later processing). For this project, we want to read the file and build a data list that stores the incoming data in a list and then manipulate the list. The concept is straightforward: start with an empty list and as your program reads through the file append each value to the list.

A. Download the file hurricanes .csv  from Google Classroom assignment and save it in your Lab03 folder.



B.  Create a new Python file called analyze.py. Add a file header (module string) at the top that contains your name, today’s date, and the section of CS 152 you are in. Then copy and paste the following pseudocode. Convert each line of pseudocode into actual Python code. Each comment will correspond to one line of code.

def main ():

# assign to fp the result of opening the file hurricanes.csv for reading

# assign to line the first line of the data file

# assign to headers the result of splitting the line using commas

# print headers

# assign to a list variable named data an empty list

# for each line in the file

# assign to items the result of splitting the line using commas

# append the second item to data (be careful, which index is that?)

# close the data file

# print data

if __name__ == "__main__":

main()

Program Output:

[ 'Year ', 'Number ', 'Damage\n ']

[15, 5, 6, 8, 3, 12, 7, 10, 2, 6, 4, 7, 10, 8]

L4. Work with command-line arguments

One way to get information from the user into a Python program is to use the input(“prompt string”) technique. But there is a second way to do this--command-line arguments. Information can be entered on the command line when the program is started and Python stores that information in a special variable called sys.argv where argv stands for the list of arguments read from the command line

For example, it would be nice to modify our analyze.py program so that we could use it with any data file and any column. The following steps will show you how to get access to the command-line information.

A. Import the sys module

Create a new file com.py. Add a file header (module string) at the top that contains your name, today’s date, and the section of CS 152 you are in. Add the following statement below your header information:



import sys

B. Find out what the sys package can do

Put the following line of code in the com.py file:

print(sys.argv)

Save your file and run the com.py file. What do you see?

Rerun the last command line, but add additional data after python3 com.py. For example, try:

python3 com.py hello world 1 2 3

Now, what do you see?

The sys package gives you the ability to see what the user has typed on the command line.

Each individual string (separated by spaces) from the command line is an entry in the argv list.

C. Access individual strings from the command line

Add the following three lines to your com.py file:


print("Running program", sys.argv[0])

print("I'm going to open the file", sys.argv[1])

print("I'm going to extract column", int(sys.argv[2]))


Run the program using the following command.

python3 com.py hurricanes.csv 1

Do you begin to see how the command-line arguments can control both the file and the column of data to extract without the need to edit the Python file each time?


L5. Get user input from the command-line

Edit your analyze.py file so that the csv filename and column index are given on the command line.

A. Add the import sys statement to the top of the file.

B. Change the definition of main() so that it has two parameters: filenamecolumn_idC. Change the hardcoded value, hurricanes.csv, with the filename parameter.

D. On the line that appends the data to your list, replace the index with the column_id parameter.

E. When the main function is called from the conditional block at the bottom of the file, use sys.argv[1] and int(sys.argv[2]) as arguments to be passed into the main function:

main(sys.argv[1], int(sys.argv[2])

And add two parameters to the definition of the main function:

def main(filename, column_id):

Run your program using:

python3 analyze.py hurricanes.csv 1

The result should be identical to the prior case. Try changing the 1 to a 2 and see what happens.

What happens if youtube        forget the extra data on the command line?

L6. A Library of Useful Functions

In the next task we're going to create a file (module) that holds a library of useful functions. In Python, a  library, or package, or module, is a file that contains functions. When you import the module into another Python file, you can use those functions. We've already done this by importing the sys package into our program. Now we're going to create our own module and then import it into other Python files.


Create a new file called stats.py. Add a file header (module string) at the top that contains your name, today’s date, and the section of CS 152 you are in.

A. Write a function to compute the sum of a list of numbers

Create a new function called sum(numbers) that takes one argument. You can assume that the argument will be a list of numbers. This function should add together all of the values in the list and return the sum. The algorithm is as follows:

# Create a variable to hold the sum

# Initialize the variable to 0.0 (explicitly make it a floating point number).

# Define a for loop to iterate over the list passed in as the function parameter. # On each iteration, add the current number to the variable holding the sum.   # Once the loop completes, return the sum.

B. Write a test function

To test your function, make a second function called test at the bottom of your stats.py file. The function does not require any arguments. As the first instruction in the test function, assign  to a variable the list [1, 2, 3, 4]. For the second instruction assign to a second variable the result of calling sum with the list as the argument. For the third instruction, print out the variable

holding the result.

Put the following at the end of your stats.py file (just below your test function) to call the test function when stats.py is run directly (i.e., not imported). The test function will not run when

stats.py is imported into another Python file.


if __name__ == "__main__":

test()

Run your program and make sure you get the value 10.0 as an output.

L7. Using a Library

The last step in the lab is to import your stats library into another file and use a function in that  module. Reopen your analyze.py file. At the top, after the import sys, and add the following line of code:

import stats


To call a function in your stats library, all you have to do is put stats. in front of the name of the function you want to use (e.g., stats.sum(numbers) )

Edit your main function so it uses the stats.sum(numbers) function to compute the sum of the values stored in the data list and prints out the sum.

If you run your analyze.py program using the hurricanes.csv file and the index of the second column, you should get a sum of 103 as the answer.

When you are done with the lab exercises, begin the project.


热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图