Case Closed


#1





Error Message :
Oops, try again. Did you call my_file.close() in the event that my_file.closed returns False?


with open("text.txt","w") as my_file:
    my_file.write("Done")
    
if my_file.closed == False :
    my_file.closed()
print my_file.closed

Replace this line with your code.


#2

Here:

It should be close and not closed


#3

>>> with open('student-becomes-teacher.py','r') as f:
    print (f.read())

'''    
lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}
students=[lloyd,alice,tyler]

# Add your function below!
def average(numbers):
    total = sum(numbers)
    return float(total)/len(numbers)
    
def get_average(student):
    homework = 0.1 * average(student["homework"])
    quizzes = 0.3 * average(student["quizzes"])
    tests = 0.6 * average(student["tests"])
    return homework + quizzes + tests
    
def get_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)

def get_letter_grade(score):
    if score >= 90:
      return "A"
    elif score >= 80:
      return "B"
    elif score >= 70:
      return "C"
    elif score >= 60:
      return "D"
    else:
      return "F"


print (get_letter_grade(get_average(lloyd)))
print (get_class_average(students))
'''

>>> with open('student-becomes-teacher.py','r') as f:
    letter_frequency(f.read())

    
e  101   17.353952
t   64   10.996564
r   54    9.278351
s   52    8.934708
u   33    5.670103
a   33    5.670103
n   31    5.326460
l   29    4.982818
o   28    4.810997
d   23    3.951890
g   19    3.264605
i   17    2.920962
m   13    2.233677
z   12    2.061856
c   11    1.890034
f   11    1.890034
v   10    1.718213
y    8    1.374570
w    7    1.202749
h    6    1.030928
q    6    1.030928
k    6    1.030928
b    4    0.687285
p    4    0.687285
j    0    0.000000
x    0    0.000000
>>>

#4

The point is that it is moot when using with..as. This construct closes all file paths at completion.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.