MS. HOLT'S ANNOUNCEMENTS - Home



Procedural ProgrammingTips for 04.04 Data Exercise Assessment – Option 1The first example on page 4 of section 3 shows how to import the csv module, open a csv file, print the rows of data in the file, and then close the file. You can run this through IDLE to view the contents of the sun data file you will use for the assignment. Notice how you just need to change the name of the file from students.csv to sun_data.csv. You will need to know the data that is included in the csv file so that you can input a time to check your program. def main(): import csv # import the CSV module with open("sun_data.csv") as csvfile: # searches for the file and opens it readCSV = csv.reader(csvfile, delimiter=',') #extracts data from the file for row in readCSV: print(row)csvfile.close() main()This is the output when run through IDLE:['Date', 'Jan', 'Feb', 'March ', 'April', 'May']['1', '344', '703', '727', '259', '428']['2', '410', '446', '315', '409', '263']['3', '800', '649', '620', '538', '502']['4', '456', '650', '618', '537', '501']['5', '338', '756', '500', '509', '659']['6', '755', '647', '616', '534', '459']['7', '622', '646', '614', '504', '458']['8', '543', '645', '613', '531', '556']['9', '700', '644', '612', '530', '457']['10', '648', '643', '610', '529', '506']['11', '728', '642', '609', '527', '455']['12', '655', '641', '608', '526', '454']['13', '805', '640', '606', '525', '435']['14', '557', '639', '605', '524', '453']['15', '535', '638', '604', '522', '452']['16', '447', '637', '602', '521', '451']['17', '431', '636', '601', '520', '450']['18', '503', '635', '600', '519', '439']['19', '533', '634', '558', '517', '449'][]Do I understand why numbers like 19 are included as times in the database? Absolutely not. We’ll just go with it. It used to include numbers 6543 and 6785 so we’re getting closer! Maybe it’s a row number – but I’m not sure.Ok, now that you know what data is included in the csv file you can write your program for the assignment.You want to create a function to use as a greeting and explain to the user what the program will do. Example:def greet(): print (“Welcome to the 5k Race for Charity!”)What else can you include in your greeting? Use the example output in the directions to create your greeting.Remember to call the greeting:greet()Next, define the main function and import the csv module.Create an input statement asking the user to input a start time they would like to race. Remember, it is a sunrise race so it will be an early time (like 659). You can either just ask a user for a time or you can make it easier on them and suggest certain times in your prompt. It’s not required – but you can if you like to make the prompts more user friendly. time = input("Please enter a time to search. Some available times are 530, 557, 655, 659")Next, initialize a boolean variable named found and set it to False. Page 4 of section 4 demonstrates how to do this. You want to set the initial value as False. Then once the condition becomes true, you can create an if statement to display the results.Now you need to add the code to have the csv file checked row by row for the time the user inputs. You do not need to do yours exactly like this but you want to make sure you include the with open as f code with the name of the sun_data csv file. You also want the program to check the fields in each row to see if they match. Notice the code below includes a found = True check. found = Falsewith open('sun_data.csv', 'rt') as f: reader = csv.reader(f, delimiter=',') for row in reader: for field in row: if field == time: found = TrueLastly, use an if-else statement to display the results. If the time is found you want to create a print statement that displays that. Check the example output on the assignment directions to see what to print if the time is found. If the time is not found, have the else statement display that the time was not found.Be sure to use the PGAs and other parts of the lesson to learn how to create a program using csv files. There is not one example that shows you exactly how to do this assignment. You’ll need to use skills you’ve learned through-out the module. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download