Python Homework

profileatamakuri14307

1.Please submit only one zip file including only Python codes with .py extension.

There will be a separate .py file for each question in the zip file. Submissions in

other formats will not be accepted.

2. Codes are expected to be clear, legible and compact with minimum number of

lines and variables, and proper indentation. Variable names are expected to be

appropriate, meaningful and self-explanatory. Codes will be graded based on

these criteria. A correctly working code does not guarantee full credit.

Que 1. (55 points) Write a Python code that gets a file name from a user using the input

statement and reads it to obtain information about students. Each line of the file

includes information about a different student and has two items separated by a

space character. A file, grades.dat, that includes the names and grades of 10

students as shown below is given as an example. The first item is the student name

and the second item is the grade of that student. The code will read the information

in the file in a dictionary where student names are keys and their grades are values

using a for loop. The second item in each line that represents a grade will be

converted to float when it is read as a value in the dictionary. The code is expected

to print the name and grade of the student that are read in each execution of the

for loop. A grade is considered success if it is 70 or above, and failure if it is below

70. The code will count the number of successes, numSuccesses, and the number

of failures, numFailures. It will print successFailureRatio, which is numSuccesses

divided by numFailures.

Barbara 62.5

John 85.

Marvin 72.5

Lewis 95.

William 65.

Mary 87.5

Sandra 92.5

Jacob 60.

Leigh 75.

Pamela 67.5

Que 2. (45 points) Extend the Python code written in question 1 to raise exceptions for the

following three cases using try and except statements so that it will not crash. The

code is expected to have a separate except statement for each case. The code is

expected to print a relevant error message under each except statement:

a. (15 points) IOError if the file does not exist.

b. (15 points) ValueError if the second item in a line cannot be converted to

float. Assume that each line in the file has definitely two items separated by

a space character, but the second item may not be a number by mistake.

c. (15 points) ZeroDivisionError if there are no failures, which happens when

all grades are 70 or above.

  • 6 years ago
  • 30
Answer(0)