Question: SOS HELP!!! Please use Python code to do. Thank you so much. Q1: Write a program that asks a user to read in a file

SOS HELP!!! Please use Python code to do. Thank you so much.

Q1:

Write a program that asks a user to read in a file name and then open the file. If filename is wrong, user could re-enter (no limit). Once a file opened, read in line by line to a list. Print the list and then close the file. For example, if the file contains

Hello All

This is our last test

Good luck

Your program will print out ["Hello All", "This is our last test", "Good luck"]

Q2:

Given the code below, answer questions (a) to (d)

try :

inFile = open("test.txt", "r")

line = inFile.readline() #line A

value = float(line) #line B

print("A")

except IOError :

print("B")

except ValueError :

print("C")

else:

print("D")

finally:

print("E")

  1. What output is generated when this program runs if test.txt is not opened successfully?

  1. What output is generated when this program runs if test.txt is opened successfully and no additional exception occurs?

  1. What output is generated when either line A or line B would have exceptions?

  1. What output is generated when only line B raises exception?

Q3:

We have a list of scores, say [50, 35, 45, ...], write code to store the scores in a text file "scores.txt", one score per line, e.g.

50

35

45

...

(no need to handle exception. If scores.txt aleady exist, you may wipe out the content,.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!