My goal is to have one program ask for inputs for test averages and write them to a txt file, and a second program that uses a loop to read and process the tests.txt file from the first program into a two-column table showing the test names and scores accurate to one decimal place.
What would the second program that reads the txt file look like?
Here is my code for the first program:
def main(): outfile =open('test.txt', 'w') print('Entering six tests and scores') num1 = float(input('Enter % score on this test ')) num2 = float(input('Enter % score on this test ')) num3 = float(input('Enter % score on this test ')) num4 = float(input('Enter % score on this test ')) num5 = float(input('Enter % score on this test ')) num6 = float(input('Enter % score on this test ')) outfile.write(str(num1) + '\n') outfile.write(str(num2) + '\n') outfile.write(str(num3) + '\n') outfile.write(str(num4) + '\n') outfile.write(str(num5) + '\n') outfile.write(str(num6) + '\n') outfile.close() main() And my second program:
def main(): infile = open('test.txt' , 'r') line1 = infile.readline() line2 = infile.readline() line3 = infile.readline() line4 = infile.readline() line5 = infile.readline() line6 = infile.readline() infile.close() line1 = line1.rstrip('\n') line2 = line2.rstrip('\n') line3 = line3.rstrip('\n') line4 = line4.rstrip('\n') line5 = line5.rstrip('\n') line6 = line6.rstrip('\n') infile.close() main()
forloop approach as a base, then get more sophisticated (if necessary) from there. DRY: don't repeat yourself.