Answer:
The solution is written in Python 3
- def maxTemp(fileName):
- try:
- with open(fileName) as file:
- raw_data = file.readlines()
- highest = 0
- for row in raw_data:
- if(row == "\\"):
- continue
-
- data = float(row)
- if(highest < data):
- highest = data
-
- return highest
- except FileNotFoundError as found_error:
- return -1
-
- higestTemp = maxTemp("text1.txt")
- print(higestTemp)
Step-by-step explanation:
Firstly, create a function maxTemp that take one input file name (Line 1). Next create an exception handling try and except block to handle the situation when the read file is not found or cannot be open, it shall return -1 (Line 2, Line 15 -16).
Next use the readlines method to read the temperature data from the input file (Line 4). Before we traverse the read data using for loop, we create a variable highest to hold the value of max temperature (Line 5). Let's set it as zero in the first place.
Then use for loop to read the temp data line by line (Line 6) and if there is any empty line (Line 7) just ignore and proceed to the next iteration (Line 7 -8). Otherwise, the current row reading will be parsed to float type (Line 10) and the parsed data will be check with the current highest value. If current highest value is bigger than the current parsed data (), the program will set the parse data to highest variable (Line 11 -12).
After finishing the loop, return the highest as output (Line 14).