views:

106

answers:

1

I need some help to print out the line with the longest length, the line with the highest sum of ASCII values, or the line with the greatest number of words from a text file. This is my first time programming and I'm really struggling with python and don't know how to calculate want is required for my lab this week. I 've try to work it out but have had no luck so far. CAN ANYONE PLEASE HELP ME?

+4  A: 

First work out how to open the file and read a line of text from the file to a string.

Read one line inside a loop and each time you loop work out the length of the string (easy), the number of words (split the string by the ' ' (space) character and count how many words you get) and the sum of the ASCII values (loop through each character in the string keeping a running total of the ascii value of each character).

Once you have your 3 values for the line you can see if they are bigger than any previously found values. You can do that by declaring some variables before the loop to hold the maximum value found so far, and then updating those variables whenever you find a bigger value. You will also need 3 variables to hold the strings which you have found to have the highest of those values.

When your loop finishes you will have read the whole file and found the 3 strings. Print them out.

Matt Howells