Starting from:
$35

$29

Laboratory 7 Files and Functions Solution

Learning objectives:

Use Python text files.

Writing functions that accept arguments and return values.

Modifying an object in a parameter




Activities: After the completion of each activity, show your answers to an instructor and switch driver and navigator.







1. Reading and writing text files.

Write a function, numberWords(), that reads the contents of a file rawWords.txt, numbers each word, and writes the number and the word to the file numberedWords.txt. For example, if the input file contains

to be or

not to be




after execution the output file should contain

 
to

 
be

 
or

 
not

 
to

 
be

Create your own text file to test your program. Make a call in main() to test your function.




2. Hourly Raise.

Build a file hourlyWages.txt so that each line of the file contains information about employees for some company in the form:




firstName lastName hourlyWage numberOfHoursWorked




Each employee is getting a raise of $1.65 per hour. Write a function, hourlyWages(infileName, outfileName), that accepts two strings as parameters that represent the names of input and output files respectively. The function should read in each line of the input file and calculate each employee’s new pay for the week. The program should then output each employee’s first and last name and pay for that week to the output file. Each line of the output file should have all of the information for exactly one employee. The output should be formatted to two places after the decimal. Make a call in main() to test your function.




3. Numerology

Create a function nameValue(name)  number. This function accepts as a parameter a string representing a person’s name and returns a number representing their numerological value based on the description in Ch. 5, Ex 5. Make a call in main() to test your function.




4. Chapter 6, Programming Exercise 3

Write two functions sphereArea(radius)  surfaceArea and sphereVolume(radius)  volume as discussed in Ch 6 Ex 3. Add code to main() to test your funcitons.




5. Chapter 6, Programming Exercise 4

Write two functions sumN(n)  total and sumNCubes(n)  total as discussed in Ch 6 Ex 4. Add code to main() to test your funcitons.







Upload the file to both student accounts (don't forget to submit).




Delete any files that you have put on the desktop. Empty the trash. Log off the computer.

More products