user8643068
user8643068

Reputation:

Python 3.5 - Generate Text Files

I have to write a program that prompts a user to enter six test names and their scores and writes them to a text file named tests.txt. I must use a loop and each input should be written to its own line in the file. The program should generate a confirmation message when done.

SAMPLE OUTPUT

Entering six tests and scores
Enter test name objects 
Enter % score on this test 88
Enter test name loops 
Enter % score on this test 95
Enter test name selections.   
Enter % score on this test 86 
Enter test name variables
Enter % score on this test 82
Enter test name files
Enter % score on this test 100
Enter test name functions
Enter % score on this test 80
File was created successfully

Then I must write another program that uses a loop to read and process the tests.txt from the previous program. The program must output a two-column table showing the test names and scores. When the table is complete, the average should be displayed accurate to one decimal place.

SAMPLE OUTPUT

Reading six tests and scores
TEST          SCORE
objects       88
loops         95
selections    86
variables     82
files         100
functions     80

Average is 88.5

This is far and beyond anything I’ve had to do. So far my tasks have been quite simple. I’m using Python 3.5 so I can’t use any new functions.

EDIT WITH PROGRESS:

So far I have this for the first program to generate the txt document:

def main():
myfile = open('test.txt', 'w')
test = input('Please enter test name or enter to quit ')

while test != '':
    score = int(input('Enter % score on this test '))
    myfile.write(test + '\n')
    myfile.write(str(score) + '\n')
    test = input('Please enter test name or enter to quit ')

myfile.close()
print('File was created successfully')

main()

The second program looks like this and is in rough shape:

def main():
f = open('test.txt', 'r'); 
text = f.read(); 
f.close()

main()

Can someone offer me some direction? Thanks!

Upvotes: 0

Views: 129

Answers (1)

hyperneutrino
hyperneutrino

Reputation: 5425

Part 1

You can write to a file like so:

with open('/path/to/your/file.name', 'w') as f:
    f.write('text')

which will create the file if it doesn't exist and overwrite all content. If you replace 'w' with 'a' it will append to the end of the file (you need to add newlines yourself).

If at the beginning of your program you do f = open('/path/to/your/file.name', 'w') and then at the end do f.close(), all content in between will be written (that is, it will not overwrite previous write operations until the output stream is flushed and closed).

For example:

f = open('file.txt', 'w')
f.write('hello, ')
f.write('world!\n')
f.close()

will set the content of file.txt to hello, world!

Part 2

The average of a list is the sum of its elements divided by the number of elements. Literally just lambda array: sum(array) / len(array)...

Part 3

To read from a file, you can do f = open('file.txt', 'r'); text = f.read(); f.close(). readline reads a line instead of the whole file.

Writing the actual program itself should be fairly trivial from this point on so I will leave the actual coding to you since this looks like a homework problem to me.

Upvotes: 1

Related Questions