Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3.6 Write code that does the following: opens an output file number list.

ID: 3848494 • Letter: P

Question

Python 3.6

Write code that does the following: opens an output file number list.txt, uses a loop to write the numbers 1 through 100 to the file and then closes the file. Write code that does the following: opens the number list.txt file that was created by the code you wrote in question 1, reads all of the numbers from the file and displays them, and then closes the file. Modify the code that you wrote in question 2 so it adds all of the numbers read from the file and displays their total Write code that opens an output file with the filename number list.txt, but does not erase the file's contents if it already exists. A file exists on the disk named students.txt. The file contains several records, and each record contains two fields: (1) the student's name, and (2) the student's score for the final exam. Write code that deletes the record containing "John Perz" as the student name A file exists on the disk named students txt. The file contains several records, and each record contains two fields: (1) the student's name, and (2) the student's score for the final exam. Write code that changes Julie Milan's score to 100.

Explanation / Answer

1.

file = open("number_list.txt","w")

for i in range(1,101):
   file.write(str(i)+" ")

file.close()

2.

with open("number_list.txt") as f:
for line in f:
print(line)


3.

sum=0
with open("number_list.txt") as f:
for line in f:
sum=sum+int(line)
print("Sum of numbers: ",sum)


4.

file = open("number_list.txt","a+")
file.close()


5.

f = open("students.txt","r")
lines = f.readlines()
f.close()

f = open("students.txt","w")
data=[]
for line in lines:
data=line.split()
print(data)
if(data[0]!="John Perz"):
f.write(line)

f.close()


6.

f = open("students.txt","r")
lines = f.readlines()
f.close()

f = open("students.txt","w")
data=[]
for line in lines:
data=line.split()
if(data[0]=="JulieMilan"):
data[1]=100
f.write('{} {} '.format(data[0],data[1]))

f.close()